fixed crude implementation of speaker.retune

Former-commit-id: eb16dd94b10cc75150508b575c5c0210d18a46cd
Former-commit-id: 84585ddf7e9e839cdfebed086bc671f6b956a462
This commit is contained in:
Song Minjae
2016-09-30 21:01:25 +09:00
parent a25983f513
commit 0859544bd6
12 changed files with 167 additions and 95 deletions

View File

@@ -6,6 +6,7 @@ import org.luaj.vm2.LuaValue
import org.luaj.vm2.lib.TwoArgFunction
import org.luaj.vm2.lib.ZeroArgFunction
import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
import org.luaj.vm2.LuaFunction
import org.luaj.vm2.lib.OneArgFunction
/**
@@ -90,7 +91,7 @@ class PcSpeakerDriver(val globals: Globals, host: BaseTerrarumComputer) {
}
}
class Retune(val globals: Globals) : OneArgFunction() {
class Retune(val globals: Globals) : LuaFunction() {
/**
* Examples: C256, A440, A#440, ...
*/
@@ -100,18 +101,24 @@ class PcSpeakerDriver(val globals: Globals, host: BaseTerrarumComputer) {
val baseNote = if (tuneName.contains("#") || tuneName.contains("b")) tuneName.substring(0, 2) else tuneName.substring(0, 1)
val freq = tuneName.replace(Regex("""[^0-9]"""), "").toInt()
// we're assuming C4, C#4, ... A4, A#4, B4
val diffPivot = arrayOf(11, 12, 13, 14, 3, 4, 5, 6, 7, 8, 9, 10) // 2^(12 / n)
// we're assuming the input to be C4, C#4, ... A4, A#4, B4
// diffPivot corsp. to G#4, A4, ...
val diffPivot = arrayOf(-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) // 2^(12 / n)
var diff = diffPivot[NOTE_NAMES.indexOf(baseNote)]
if (diff < 0) diff = diffPivot[NOTE_NAMES_ALT.indexOf(baseNote)] // search again
if (diff < 0) throw IllegalArgumentException("Unknown note: $baseNote") // failed to search
val exp = 12.0 / diff
val basefreq = freq * Math.pow(2.0, exp) / 32.0 // converts whatever baseNote to A0
val exp = -diff / 12.0
val basefreq = freq * Math.pow(2.0, exp) / if (diff >= 3) 8.0 else 16.0 // converts whatever baseNote to A0
globals["speaker"]["__basefreq__"] = basefreq
return LuaValue.NONE
}
override fun call(): LuaValue {
globals["speaker"]["__basefreq__"] = LuaValue.valueOf(BASE_FREQ)
return LuaValue.NONE
}
}
class ResetTune(val globals: Globals) : ZeroArgFunction() {