Demos: ensure all use fun main() = application {

- Adjust some demo window sizes.
- Replace Random.double by Double.uniform
- Tweak some demos so screenshots look more interesting
This commit is contained in:
Abe Pazos
2025-01-26 20:57:04 +01:00
parent 1975a820fc
commit c8f7dd52c6
116 changed files with 2889 additions and 2942 deletions

View File

@@ -13,98 +13,96 @@ import kotlin.random.Random
* Hold the mouse button to randomize the frequencies.
* Press keys 'a' or 'b' for less random frequencies.
*/
fun main() {
application {
program {
val minim = minim()
val out = minim.lineOut
fun main() = application {
program {
val minim = minim()
val out = minim.lineOut
if (out == null) {
application.exit()
if (out == null) {
application.exit()
}
// generates a random frequency value biased down
fun randomFreq() = 20f + Random.nextFloat().pow(3) * 1000
// If one didn't want to visualize or control the synths we
// wouldn't need a data structure to store them. Here we store
// Pairs, so we have access both to the frequency of the wave
// and the current amplitude defined by the lfo (low frequency
// oscillator).
val synths = List(20) {
// By default, Oscil creates sine waves, but it can be changed.
val lfo = Oscil(
Random.nextFloat() * 0.1f + 0.005f,
0.05f
).apply {
// Here we set the center of the lfo to 0.05f.
// Since the amplitude is also 0.05f, it moves between
// 0.00f and 0.10f.
offset.lastValue = 0.05f
// Have the sine waves to not start in sync.
//phase.lastValue = Random.nextFloat() * 6.28f
}
val wave = Oscil(randomFreq(), 0f)
// The `lfo` Oscil controls the `wave` Oscil's amplitude.
lfo.patch(wave.amplitude)
// Random pan to avoid a mono sound.
val pan = Pan(Random.nextFloat() * 2 - 1)
wave.patch(pan)
pan.patch(out)
// Store a [Pair] in `synths`.
Pair(wave, lfo)
}
val bgColor = rgb(0.094, 0.188, 0.349)
val lineColor = rgb(0.992, 0.918, 0.671)
val mouseTracker = MouseTracker(mouse)
// generates a random frequency value biased down
fun randomFreq() = 20f + Random.nextFloat().pow(3) * 1000
// If one didn't want to visualize or control the synths we
// wouldn't need a data structure to store them. Here we store
// Pairs, so we have access both to the frequency of the wave
// and the current amplitude defined by the lfo (low frequency
// oscillator).
val synths = List(20) {
// By default, Oscil creates sine waves, but it can be changed.
val lfo = Oscil(
Random.nextFloat() * 0.1f + 0.005f,
0.05f
).apply {
// Here we set the center of the lfo to 0.05f.
// Since the amplitude is also 0.05f, it moves between
// 0.00f and 0.10f.
offset.lastValue = 0.05f
// Have the sine waves to not start in sync.
//phase.lastValue = Random.nextFloat() * 6.28f
extend {
drawer.clear(bgColor)
drawer.translate(drawer.bounds.center)
drawer.rotate(seconds)
// A CircleBatchBuilder for faster drawing of circles.
drawer.circles {
// For each synth draw a circle.
synths.forEachIndexed { i, (wave, lfo) ->
stroke = lineColor.opacify(Random.nextDouble(0.4) + 0.6)
fill = lineColor.opacify(Random.nextDouble() * 0.04)
// A Polar arrangement centered on the screen.
// Higher pitch circles are farther away from the center.
val pos = Polar(
360.0 * i / synths.size,
50.0 + wave.frequency.lastValue * 0.2
).cartesian
// The size of the circle depends on the current volume
// set by the lfo.
circle(pos, 500 * lfo.lastValues.last().toDouble())
}
val wave = Oscil(randomFreq(), 0f)
// The `lfo` Oscil controls the `wave` Oscil's amplitude.
lfo.patch(wave.amplitude)
// Random pan to avoid a mono sound.
val pan = Pan(Random.nextFloat() * 2 - 1)
wave.patch(pan)
pan.patch(out)
// Store a [Pair] in `synths`.
Pair(wave, lfo)
}
val bgColor = rgb(0.094, 0.188, 0.349)
val lineColor = rgb(0.992, 0.918, 0.671)
val mouseTracker = MouseTracker(mouse)
extend {
drawer.clear(bgColor)
drawer.translate(drawer.bounds.center)
drawer.rotate(seconds)
// A CircleBatchBuilder for faster drawing of circles.
drawer.circles {
// For each synth draw a circle.
synths.forEachIndexed { i, (wave, lfo) ->
stroke = lineColor.opacify(Random.nextDouble(0.4) + 0.6)
fill = lineColor.opacify(Random.nextDouble() * 0.04)
// A Polar arrangement centered on the screen.
// Higher pitch circles are farther away from the center.
val pos = Polar(
360.0 * i / synths.size,
50.0 + wave.frequency.lastValue * 0.2
).cartesian
// The size of the circle depends on the current volume
// set by the lfo.
circle(pos, 500 * lfo.lastValues.last().toDouble())
if (mouseTracker.pressedButtons.isNotEmpty()) {
synths.random().first.setFrequency(randomFreq())
}
}
keyboard.keyDown.listen { key ->
when (key.name) {
"a" -> {
// make all frequencies close to a base frequency
// (circular arrangement)
val baseFreq = 20 + Random.nextFloat() * 200
synths.forEach {
it.first.setFrequency(baseFreq + Random.nextFloat() * 20)
}
}
if (mouseTracker.pressedButtons.isNotEmpty()) {
synths.random().first.setFrequency(randomFreq())
}
}
keyboard.keyDown.listen { key ->
when (key.name) {
"a" -> {
// make all frequencies close to a base frequency
// (circular arrangement)
val baseFreq = 20 + Random.nextFloat() * 200
synths.forEach {
it.first.setFrequency(baseFreq + Random.nextFloat() * 20)
}
}
"b" -> {
// make all frequencies follow an exponential series
// (spiral arrangement)
val inc = Random.nextFloat() * 0.1f
synths.forEachIndexed { i, (wave, _) ->
wave.setFrequency(25f.pow(1f + i * inc))
}
"b" -> {
// make all frequencies follow an exponential series
// (spiral arrangement)
val inc = Random.nextFloat() * 0.1f
synths.forEachIndexed { i, (wave, _) ->
wave.setFrequency(25f.pow(1f + i * inc))
}
}
}
}
}
}
}

View File

@@ -1,38 +1,35 @@
import ddf.minim.Minim
import ddf.minim.analysis.FFT
import ddf.minim.analysis.LanczosWindow
import org.openrndr.application
import org.openrndr.extra.minim.minim
import org.openrndr.math.map
import kotlin.math.ln
fun main() {
application {
configure {
width = 1280
height = 720
fun main() = application {
configure {
width = 1280
height = 720
}
program {
val minim = minim()
if (minim.lineOut == null) {
application.exit()
}
program {
val minim = minim()
if (minim.lineOut == null) {
application.exit()
}
val lineIn = minim.getLineIn(Minim.MONO, 2048, 48000f)
if (lineIn == null) {
application.exit()
}
val fft = FFT(lineIn.bufferSize(), lineIn.sampleRate())
fft.window(LanczosWindow())
extend {
fft.forward(lineIn.mix)
for (i in 0 until 200) {
val bandDB = 20.0 * ln(2.0 * fft.getBand(i) / fft.timeSize())
drawer.rectangle(i * 5.0, height / 2.0, 5.0, bandDB.map(0.0, -150.0, 0.0, -height / 8.0))
}
val lineIn = minim.getLineIn(Minim.MONO, 2048, 48000f)
if (lineIn == null) {
application.exit()
}
val fft = FFT(lineIn.bufferSize(), lineIn.sampleRate())
fft.window(LanczosWindow())
extend {
fft.forward(lineIn.mix)
for (i in 0 until 200) {
val bandDB = 20.0 * ln(2.0 * fft.getBand(i) / fft.timeSize())
drawer.rectangle(i * 5.0, height / 2.0, 5.0, bandDB.map(0.0, -150.0, 0.0, -height / 8.0))
}
}
}
}
}

View File

@@ -1,28 +1,26 @@
import org.openrndr.application
import org.openrndr.extra.minim.minim
fun main() {
application {
program {
val minim = minim()
if (minim.lineOut == null) {
application.exit()
}
fun main() = application {
program {
val minim = minim()
if (minim.lineOut == null) {
application.exit()
}
val player = minim.loadFile(
"demo-data/sounds/26777__junggle__btn402.mp3"
)
val player = minim.loadFile(
"demo-data/sounds/26777__junggle__btn402.mp3"
)
// fade gain to -40dB in 15 seconds
player.shiftGain(player.gain, -40f, 15000)
// fade gain to -40dB in 15 seconds
player.shiftGain(player.gain, -40f, 15000)
extend {
if(frameCount % 30 == 0) {
player.rewind()
//player.gain = Random.nextDouble(-20.0, 0.0).toFloat()
player.play()
}
extend {
if (frameCount % 30 == 0) {
player.rewind()
//player.gain = Random.nextDouble(-20.0, 0.0).toFloat()
player.play()
}
}
}
}
}