Skip to content

Commit

Permalink
harmonic series init properly, fixed rare null pointer exception
Browse files Browse the repository at this point in the history
  • Loading branch information
mktwohy committed Dec 7, 2021
1 parent 62a5b4e commit f2c9502
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
16 changes: 7 additions & 9 deletions SignalLib/src/main/java/com/example/signallib/SignalEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,12 @@ class SignalEngine(
audioTrack.play()

while (runMainLoop) {
// poll from realtime input queues
if(noteQueue.isNotEmpty())
currentNotes.replaceAll(noteQueue.poll()!!)
// poll from realtime input queues. if not null, update property
noteQueue.poll()?.also { currentNotes.replaceAll(it) }

if(pitchBendQueue.isNotEmpty())
pitchBend = pitchBendQueue.poll()!!

if(ampQueue.isNotEmpty())
amp = ampQueue.poll()!!
pitchBendQueue.poll()?.also { pitchBend = it }

ampQueue.poll()?.also { amp = it }

// check if audio buffer needs to be updated
// (ensures silent audio doesn't get rendered)
Expand All @@ -102,7 +98,9 @@ class SignalEngine(
amp = amp
)

afterBufferWriteOneTimeListeners.forEach { it.invoke(audioBuffer) }
val removedNotes = prevNotes.filter{ it !in currentNotes }
signalManager.resetSignals(removedNotes.toSet())

prevNotes.replaceAll(currentNotes)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class SignalManager(val signalSettings: SignalSettings) {
}
}

fun resetSignals(notes: Set<Note>){
notes.forEach{ noteToSignal[it]?.reset() }
}

fun renderToBuffer(buffer: FloatArray, notes: Set<Note>, pitchBend: Float, amp: Float){
// assign pitch bend to appropriate signals
for(note in notes) {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/example/synth/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ class MainActivity : ComponentActivity() {
signalEngine.registerAfterBufferWriteCallback {
currentAudio = it.toList()
}
signalSettings.harmonicSeries[1] = 1f

signalEngine.play()
}
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/example/synth/ViewModels.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.example.signallib.Note.Companion.nextWhiteNote
import com.example.signallib.Note.Companion.prevWhiteNote
import java.util.*
import kotlin.math.abs
import kotlin.math.sign

class SignalPlotViewModel(
val signalSettings: SignalSettings,
Expand Down Expand Up @@ -106,15 +107,15 @@ class HarmonicSeriesViewModel(
val harmonicSeriesUpdateQueue: Queue<Pair<Int, Float>> = LinkedList()

init {
repeat(signalSettings.harmonicSeries.numHarmonics){
sliderState.add(0f)
with(signalSettings.harmonicSeries){
for (i in 1..numHarmonics){
sliderState.add(this[i]) }
}
AppModel.signalEngine.registerAfterBufferWriteCallback {
if (harmonicSeriesUpdateQueue.isNotEmpty()){
val (sliderIndex, sliderValue) = harmonicSeriesUpdateQueue.poll()!!
signalSettings.harmonicSeries[sliderIndex+1] = volumeToAmplitude(sliderValue)
}
// logd("queue size: ${harmonicSeriesUpdateQueue.size}")
}
}

Expand Down

0 comments on commit f2c9502

Please sign in to comment.