From 39089cea403ca4454b4c4586f8f4a3785cf845ce Mon Sep 17 00:00:00 2001 From: Emilien Bauer Date: Sat, 21 Jan 2023 14:23:03 +0000 Subject: [PATCH] RandomGenerator --- .idea/.name | 1 + .idea/codeStyles/Project.xml | 10 ++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 +++++ src/main/kotlin/AudioSink.kt | 1 + src/main/kotlin/Main.kt | 22 ++++++++++++++++++++-- 5 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 .idea/.name create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..efe2e14 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +procedural-musix \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/src/main/kotlin/AudioSink.kt b/src/main/kotlin/AudioSink.kt index c3165c6..b834c41 100644 --- a/src/main/kotlin/AudioSink.kt +++ b/src/main/kotlin/AudioSink.kt @@ -18,6 +18,7 @@ class AudioSink : Thread() { get() = chunks.size fun enqueueChunk(chunk: AudioChunk) { + println(chunk.tick) chunks.add(chunk.toBytes()) } diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 047ec45..c9140e6 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -3,6 +3,7 @@ import Params.TWO_PI import Params.as_samples import filter.Compressor import filter.Limiter +import java.lang.StrictMath.pow import kotlin.math.cos import kotlin.math.pow import kotlin.math.sin @@ -35,7 +36,24 @@ class SineWaveGenerator(var tone: Float) : StepGenerator() { } } - +class RandomGenerator(var tone: Float) :StepGenerator() { + val N = 200 + val loudness = fun(i:Int): Double { + return i.toDouble().pow(-0) + } + var loudnessSum = 0.0 + init { + for (i in 1 .. N) + loudnessSum+=loudness(i) + } + override fun generateSingle(time: Float): Float { + var p : Double = 0.0 + for (i in 1 .. N) { + p += sin(time * toFrequency(tone) * TWO_PI * i) * loudness(i) + } + return (p/loudnessSum).toFloat() + } +} fun main(args: Array) { val sink = AudioSink() @@ -46,7 +64,7 @@ fun main(args: Array) { gen.globalEffects.add(Compressor(as_samples(.4f).toInt())::apply) gen.globalEffects.add(Limiter(.9f)::apply) - gen.generators.add(Chain(SineWaveGenerator(0f)::generate, ArrayList())) + gen.generators.add(Chain(RandomGenerator(0f)::generate, ArrayList())) sink.enqueueChunk(gen.nextChunk())