Skip to content

Instantly share code, notes, and snippets.

@antonarhipov
Last active October 24, 2024 12:55
Show Gist options
  • Save antonarhipov/f763f9e8d4ba59df0d234d56425e0efa to your computer and use it in GitHub Desktop.
Save antonarhipov/f763f9e8d4ba59df0d234d56425e0efa to your computer and use it in GitHub Desktop.
Sonic Pi Dark House Music Generator in Kotlin
import kotlin.random.Random
data class Note(val pitch: Int, val duration: Double)
class SonicPiDarkHouseGenerator {
private val scale = listOf(60, 63, 65, 67, 70, 72) // C minor pentatonic scale
private val rhythms = listOf(0.25, 0.5, 1.0) // Sixteenth, eighth, and quarter notes
fun generateMelody(length: Int): List<Note> =
List(length) {
Note(
pitch = scale.random(),
duration = rhythms.random()
)
}
fun generateBassline(length: Int): List<Note> {
val bassNotes = listOf(36, 39, 41) // C, Eb, F in the lower octave
return List(length) {
Note(
pitch = bassNotes.random(),
duration = 1.0 // Quarter notes for the bassline
)
}
}
fun generateSonicPiCode(): String {
val bassline = generateBassline(4)
val melody = generateMelody(8)
return buildString {
appendLine("use_bpm 120") // Slower tempo for dark house
appendLine("""
## Atmospheric pad
live_loop :atmos_pad do
use_synth :dark_ambience
play chord(:C3, :minor), release: 8, amp: 0.4
sleep 8
end
## Deep kick and percussion
live_loop :drums do
sample :bd_tek, rate: 0.8, amp: 1.2
sleep 1
sample :drum_cymbal_closed, rate: 0.8, amp: 0.4
sleep 0.5
sample :drum_snare_soft, rate: 0.7, amp: 0.6
sleep 0.5
end
## Dark bassline
live_loop :bassline do
use_synth :tb303
""".trimIndent())
bassline.forEach { note ->
appendLine(" play ${note.pitch}, release: 0.8, cutoff: 70, res: 0.8, wave: 1, amp: 0.7")
appendLine(" sleep ${note.duration}")
}
appendLine("""
end
## Subtle melody
live_loop :melody do
use_synth :prophet
""".trimIndent())
melody.forEach { note ->
appendLine(" play ${note.pitch}, release: ${note.duration * 2}, cutoff: rrand(70, 100), amp: 0.3")
appendLine(" sleep ${note.duration}")
}
appendLine("""
end
## FX loop for atmosphere
live_loop :fx do
sample :ambi_lunar_land, rate: 0.5, amp: 0.2
sleep 16
end
""".trimIndent())
}
}
}
fun main() {
val generator = SonicPiDarkHouseGenerator()
val sonicPiCode = generator.generateSonicPiCode()
println("Generated Sonic Pi code for Dark House:")
println(sonicPiCode)
// Optionally, you can save this to a file
// File("dark_house_track.rb").writeText(sonicPiCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment