beeper update: proper equation. Need more work for low freqs

Former-commit-id: a98210ef06bea8f3a85af134fac8848225728c65
Former-commit-id: 901f266d529af99133f0605d2ce76c8cb52c7d17
This commit is contained in:
Song Minjae
2016-09-27 00:51:14 +09:00
parent bd4c95b321
commit 8c014cf4c8
9 changed files with 63 additions and 29 deletions

View File

@@ -954,7 +954,7 @@ _G._VERSION = "Luaj-jse 3.0.1 (Lua 5.2.3)"
_G.EMDASH = string.char(0xC4)
_G.UNCHECKED = string.char(0x9C) -- box unchecked
_G.CHECKED = string.char(0x9D) -- box checked
_G.MONEYSYM = string.char(0x9E) -- currency sign
_G.MONEY = string.char(0x9E) -- currency sign
_G.MIDDOT = string.char(0xFA) -- middle dot sign
_G.DC1 = string.char(17) -- black
_G.DC2 = string.char(18) -- white
@@ -988,6 +988,7 @@ computer.verbose = true -- print debug info
computer.loadedCLayer = {} -- list of loaded compatibility layers
computer.bootloader = "/boot/efi"
computer.OEM = ""
computer.beep = emittone
computer.totalMemory = _G.totalMemory
local getMemory = function()
collectgarbage()

View File

@@ -131,6 +131,9 @@ class BaseTerrarumComputer() {
// computer-related global functions
luaJ_globals["totalMemory"] = LuaFunGetTotalMem(this)
luaJ_globals["computer"] = LuaTable()
luaJ_globals["emittone"] = ComputerEmitTone(this)
}
var threadTimer = 0
@@ -233,7 +236,7 @@ class BaseTerrarumComputer() {
}
}
class EmitTone(val computer: BaseTerrarumComputer) : TwoArgFunction() {
class ComputerEmitTone(val computer: BaseTerrarumComputer) : TwoArgFunction() {
override fun call(millisec: LuaValue, freq: LuaValue): LuaValue {
computer.playTone(millisec.toint(), freq.tofloat())
return LuaValue.NONE
@@ -251,18 +254,27 @@ class BaseTerrarumComputer() {
/**
* @param duration : milliseconds
* @param rampUp
* @param rampDown
*
* ,---. (true, true) ,---- (true, false) ----. (false, true) ----- (false, false)
*/
private fun makeAudioData(duration: Int, freq: Float): ByteBuffer {
private fun makeAudioData(duration: Int, freq: Float,
rampUp: Boolean = true, rampDown: Boolean = true): ByteBuffer {
val audioData = BufferUtils.createByteBuffer(duration.times(sampleRate).div(1000))
val realDuration = duration * sampleRate / 1000
val chopSize = freq * 2f / sampleRate
val chopSize = freq / sampleRate
val amp = Math.max(4600f / freq, 1f)
val nHarmonics = 2
val nHarmonics = if (freq >= 5400) 1
else if (freq >= 3600) 2
else if (freq >= 1800) 3
else 4
val transitionThre = 2300f
val transitionThre = 1150f
// TODO volume ramping?
if (freq == 0f) {
for (x in 0..realDuration - 1) {
audioData.put(0x00.toByte())
@@ -270,7 +282,7 @@ class BaseTerrarumComputer() {
}
else if (freq < transitionThre) { // chopper generator (for low freq)
for (x in 0..realDuration - 1) {
var sine: Float = amp * FastMath.cos(FastMath.PI * x * chopSize)
var sine: Float = amp * FastMath.cos(FastMath.TWO_PI * x * chopSize)
if (sine > 1f) sine = 1f
else if (sine < -1f) sine = -1f
audioData.put(
@@ -281,9 +293,8 @@ class BaseTerrarumComputer() {
else { // harmonics generator (for high freq)
for (x in 0..realDuration - 1) {
var sine: Float = 0f
for (k in 0..nHarmonics) { // mix only odd harmonics to make squarewave
sine += (1f / (2*k + 1)) *
FastMath.sin((2 * k + 1) * FastMath.PI * x * chopSize)
for (k in 1..nHarmonics) { // mix only odd harmonics in order to make a squarewave
sine += FastMath.sin(FastMath.TWO_PI * (2*k - 1) * chopSize * x) / (2*k - 1)
}
audioData.put(
(0.5f + 0.5f * sine).times(0xFF).toByte()

View File

@@ -124,7 +124,7 @@ open class SimpleTextTerminal(
// complete beep queue
if (beepCursor >= beepQueue.size) {
clearBeepQueue()
println("!! Beep queue clear")
// println("!! Beep queue clear")
}
// actually play queue
@@ -345,7 +345,7 @@ open class SimpleTextTerminal(
* @param freg: Frequency (float)
*/
override fun beep(duration: Int, freq: Float) {
println("!! Beep playing row $beepCursor, d ${Math.min(duration, maxDuration)} f $freq")
// println("!! Beep playing row $beepCursor, d ${Math.min(duration, maxDuration)} f $freq")
host.playTone(Math.min(duration, maxDuration), freq)
}