Remember voice states SATB
This commit is contained in:
parent
4e8866a4e7
commit
64c94b4064
1 changed files with 21 additions and 24 deletions
|
|
@ -6,15 +6,14 @@ import kotlinx.coroutines.launch
|
||||||
import mg.dot.feufaro.FileRepository
|
import mg.dot.feufaro.FileRepository
|
||||||
import java.io.ByteArrayInputStream
|
import java.io.ByteArrayInputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.util.prefs.Preferences
|
||||||
import javax.sound.midi.MidiSystem
|
import javax.sound.midi.MidiSystem
|
||||||
import javax.sound.midi.Sequencer
|
import javax.sound.midi.Sequencer
|
||||||
import javax.sound.midi.Synthesizer //import javax.sound.midi.ShortMessage
|
import javax.sound.midi.Synthesizer
|
||||||
//import javax.sound.midi.Synthesizer
|
|
||||||
import javax.sound.sampled.AudioFormat
|
import javax.sound.sampled.AudioFormat
|
||||||
import javax.sound.sampled.AudioSystem
|
import javax.sound.sampled.AudioSystem
|
||||||
import javax.sound.sampled.FloatControl
|
import javax.sound.sampled.FloatControl
|
||||||
|
|
||||||
//private var sequencer: javax.sound.midi.Sequencer?= null
|
|
||||||
actual class MediaPlayer actual constructor(
|
actual class MediaPlayer actual constructor(
|
||||||
private val filename: String,
|
private val filename: String,
|
||||||
private val onFinished: () -> Unit
|
private val onFinished: () -> Unit
|
||||||
|
|
@ -27,6 +26,7 @@ actual class MediaPlayer actual constructor(
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val prefs = Preferences.userRoot().node("mg.dot.feufaro")
|
||||||
private var synthetizer = MidiSystem.getSynthesizer() as Synthesizer?
|
private var synthetizer = MidiSystem.getSynthesizer() as Synthesizer?
|
||||||
|
|
||||||
private var pointA: Long = -1L
|
private var pointA: Long = -1L
|
||||||
|
|
@ -53,6 +53,7 @@ actual class MediaPlayer actual constructor(
|
||||||
val file = File(filename)
|
val file = File(filename)
|
||||||
if (file.exists()){
|
if (file.exists()){
|
||||||
sequencer?.sequence = MidiSystem.getSequence(file)
|
sequencer?.sequence = MidiSystem.getSequence(file)
|
||||||
|
loadVoiceStates()
|
||||||
applyVoiceStates()
|
applyVoiceStates()
|
||||||
sequencer?.addMetaEventListener { meta ->
|
sequencer?.addMetaEventListener { meta ->
|
||||||
if(meta.type == 47){
|
if(meta.type == 47){
|
||||||
|
|
@ -103,16 +104,6 @@ actual class MediaPlayer actual constructor(
|
||||||
channel?.controlChange(7, volumeInt)
|
channel?.controlChange(7, volumeInt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*val logVolume = if (level > 0 ){
|
|
||||||
(Math.log10(level.toDouble() * 9 + 1) / Math.log10(10.0)).toFloat()
|
|
||||||
} else {
|
|
||||||
0f
|
|
||||||
}
|
|
||||||
val volumeInt = (logVolume * 127).toInt().coerceIn(0, 127)
|
|
||||||
|
|
||||||
synthetizer?.channels?.forEach { it?.controlChange(7, volumeInt) }
|
|
||||||
synthetizer?.channels?.forEach { it?.controlChange(11, volumeInt) }
|
|
||||||
*/
|
|
||||||
val mixer = AudioSystem.getMixer(null)
|
val mixer = AudioSystem.getMixer(null)
|
||||||
val lines = mixer.sourceLines
|
val lines = mixer.sourceLines
|
||||||
for (line in lines) {
|
for (line in lines) {
|
||||||
|
|
@ -124,14 +115,6 @@ actual class MediaPlayer actual constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// val msg = ShortMessage()
|
|
||||||
// val receiver = sequencer?.receiver
|
|
||||||
// for (i in 0 until 16) {
|
|
||||||
// val msg = ShortMessage()
|
|
||||||
// msg.setMessage(ShortMessage.CONTROL_CHANGE, i, 7, volumeInt)
|
|
||||||
// receiver?.send(msg, -1)
|
|
||||||
// }
|
|
||||||
|
|
||||||
} catch (e: Exception){
|
} catch (e: Exception){
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
|
@ -170,6 +153,7 @@ actual class MediaPlayer actual constructor(
|
||||||
|
|
||||||
actual fun toggleVoice(index: Int) {
|
actual fun toggleVoice(index: Int) {
|
||||||
voiceStates[index] = !voiceStates[index]
|
voiceStates[index] = !voiceStates[index]
|
||||||
|
saveVoiceStates()
|
||||||
applyVoiceStates()
|
applyVoiceStates()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -204,11 +188,8 @@ actual class MediaPlayer actual constructor(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
actual fun getCurrentBPM(): Float {
|
actual fun getCurrentBPM(): Float {
|
||||||
// return sequencer?.tempoInBPM?.toInt() ?: 0
|
|
||||||
val currentFactor = sequencer?.tempoFactor ?: 1.0f
|
val currentFactor = sequencer?.tempoFactor ?: 1.0f
|
||||||
// val currentBPM = sequencer?.tempoInBPM ?: 120.0f
|
|
||||||
val currentBPM = (120f * currentFactor)
|
val currentBPM = (120f * currentFactor)
|
||||||
// println("202: ${sequencer?.tempoInBPM} ${sequencer?.tempoFactor} ${sequencer?.tempoInMPQ}")
|
|
||||||
return currentBPM
|
return currentBPM
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -231,4 +212,20 @@ actual class MediaPlayer actual constructor(
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun saveVoiceStates() {
|
||||||
|
val data = voiceStates.joinToString(",")
|
||||||
|
prefs.put("voice_states", data)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun loadVoiceStates() {
|
||||||
|
val defaultValue = "true,true,true,true"
|
||||||
|
val savedData = prefs.get("voice_states", defaultValue)
|
||||||
|
|
||||||
|
val states = savedData.split(",").map { it.toBoolean() }
|
||||||
|
for (i in 0 until 4) {
|
||||||
|
if (i < states.size) voiceStates[i] = states[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Reference in a new issue