1
0
Fork 0

Play sample sound when sender settings have changed

This commit is contained in:
Eoin Mcloughlin 2020-05-18 19:11:03 +01:00
parent bec54fb0e5
commit c59f347e09
4 changed files with 51 additions and 19 deletions

View file

@ -351,19 +351,20 @@ fun KeycodeToSoundSequence(keycode : Int) : List<SoundTypes> {
}
}
class DitDahGeneratorSettings
{
fun initFromPreferences(ctx : Context) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx)
toneFrequency = sharedPreferences.getInt(ctx.getString(R.string.setting_tone_key), toneFrequency)
wordsPerMinute = sharedPreferences.getInt(ctx.getString(R.string.setting_wpm_key), wordsPerMinute)
farnsworthWordsPerMinute = sharedPreferences.getInt(ctx.getString(R.string.setting_effective_wpm_key), farnsworthWordsPerMinute)
}
data class DitDahGeneratorSettings(var context : Context? = null) {
//TODO These values are duplicated in the settings fragment
var toneFrequency = 650
var wordsPerMinute = 20
var farnsworthWordsPerMinute = 20
init {
if(context != null) {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
toneFrequency = sharedPreferences.getInt(context!!.getString(R.string.setting_tone_key), toneFrequency)
wordsPerMinute = sharedPreferences.getInt(context!!.getString(R.string.setting_wpm_key), wordsPerMinute)
farnsworthWordsPerMinute = sharedPreferences.getInt(context!!.getString(R.string.setting_effective_wpm_key), farnsworthWordsPerMinute)
}
}
}
class DitDahSoundStream : AudioTrack.OnPlaybackPositionUpdateListener {
@ -371,7 +372,6 @@ class DitDahSoundStream : AudioTrack.OnPlaybackPositionUpdateListener {
fun streamFinished(stream : DitDahSoundStream)
}
constructor(config : DitDahGeneratorSettings) {
// Farnsworth timing calculations: https://morsecode.world/international/timing.html

View file

@ -1,12 +1,14 @@
package es.eoinrul.ecwt
import androidx.appcompat.app.AppCompatActivity
import android.content.SharedPreferences
import android.os.Bundle
import es.eoinrul.ecwt.R
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
//const val PREFERENCES_FILE = "es.eoinrul.ecwt.PREFERENCES"
class SettingsActivity : AppCompatActivity() {
class SettingsActivity : AppCompatActivity(),
SharedPreferences.OnSharedPreferenceChangeListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -15,5 +17,39 @@ class SettingsActivity : AppCompatActivity() {
// Sounds: WPM; Farnsworth; (character speed versus word speed) Tone freq
// Koch: Lesson duration; word size
//var preferences = getSharedPreferences(PREFERENCES_FILE, Context.MODE_PRIVATE)
mLastSenderSettings = DitDahGeneratorSettings(this)
}
override fun onResume() {
super.onResume()
var sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
sharedPrefs.registerOnSharedPreferenceChangeListener(this)
}
// Unhook our listener, and shut down any sound sample stream we might have made
override fun onPause() {
var sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this)
sharedPrefs.unregisterOnSharedPreferenceChangeListener(this)
mSampleOutputStream?.quit()
mSampleOutputStream = null
super.onPause()
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
var newSettings = DitDahGeneratorSettings(this)
// Play a sample sound to demo new settings if the relevant ones have changed
if(newSettings.toneFrequency != mLastSenderSettings.toneFrequency
|| newSettings.wordsPerMinute != mLastSenderSettings.wordsPerMinute
|| newSettings.farnsworthWordsPerMinute != mLastSenderSettings.farnsworthWordsPerMinute) {
mSampleOutputStream?.quit()
var sampleOutputStream = DitDahSoundStream(newSettings)
sampleOutputStream.enqueue(StringToSoundSequence("CQ"))
mSampleOutputStream = sampleOutputStream
mLastSenderSettings = newSettings
}
}
var mLastSenderSettings = DitDahGeneratorSettings()
var mSampleOutputStream : DitDahSoundStream? = null
}

View file

@ -48,9 +48,7 @@ class SounderActivity : AppCompatActivity() {
}
private fun initSoundPlayer() {
val generatorSettings = DitDahGeneratorSettings()
generatorSettings.initFromPreferences(this)
val generatorSettings = DitDahGeneratorSettings(this)
mSoundPlayer = DitDahSoundStream(generatorSettings)
}

View file

@ -27,9 +27,7 @@ class TrainingActivity : AppCompatActivity(),
}
private fun initSoundPlayer() {
val generatorSettings = DitDahGeneratorSettings()
generatorSettings.initFromPreferences(this)
val generatorSettings = DitDahGeneratorSettings(this)
mSoundPlayer = DitDahSoundStream(generatorSettings)
mSoundPlayer!!.streamNotificationListener = this
}