Author |
Message |
Horlepiep
Joined: Dec 17, 2021 Posts: 10 Location: Netherlands
|
Posted: Fri Dec 17, 2021 1:14 pm Post subject:
While question Subject description: Problem with the combination of a while-loop and MIDI input |
 |
|
As an absolute beginner I'm trying to write a program in ChucK that should accept MIDI-input from my MIDI-keyboard to control oscillators. A sort of primitive synthesizer. Now I already have the following working program to get the MIDI messages inside ChucK:
Code: | // open MIDI kanaal naar Q25
MidiIn min;
MidiMsg msg;
min.open(1);
while(true)
{
// wacht op midi-on event
min => now;
// receive midimsg(s)
if( min.recv( msg ) )
{
// print MIDI en frequentie
<<< msg.data1, msg.data2, msg.data3 >>>;
<<< Std.mtof(msg.data2) >>>;
// als MIDI On message print dat dan
if( msg.data1 == 144 )
{
<<< "MIDI On gesignaleerd" >>>;
}
}
// wacht op midi-off event
min => now;
// receive midimsg(s)
if( min.recv( msg ) )
{
// print MIDI en frequentie
<<< msg.data1, msg.data2, msg.data3 >>>;
<<< Std.mtof(msg.data2) >>>;
// als MIDI Off message print dat dan
if( msg.data1 == 128 )
{
<<< "MIDI Off gesignaleerd" >>>;
}
}
} |
But when I add an oscillator to make the tones they start but will not stop. See:
Code: |
// open MIDI kanaal naar Q25
MidiIn min;
MidiMsg msg;
min.open(1);
while(true)
{
// wacht op midi-on event
min => now;
// receive midimsg(s)
if( min.recv( msg ) )
{
// print MIDI en frequentie
<<< msg.data1, msg.data2, msg.data3 >>>;
<<< Std.mtof(msg.data2) >>>;
// als MIDI On message print dat dan
if( msg.data1 == 144 )
{
<<< "MIDI On gesignaleerd" >>>;
// toon spelen
SawOsc s => dac;
Std.mtof(msg.data2) => s.freq;
0.5 => s.gain;
0.5::second => now;
}
}
// wacht op midi-off event
min => now;
// receive midimsg(s)
if( min.recv( msg ) )
{
// print MIDI en frequentie
<<< msg.data1, msg.data2, msg.data3 >>>;
<<< Std.mtof(msg.data2) >>>;
// als MIDI Off message print dat dan
if( msg.data1 == 128 )
{
<<< "MIDI Off gesignaleerd" >>>;
}
}
}
|
What is wrong here? |
|
Back to top
|
|
 |
wakyct
Joined: Dec 30, 2020 Posts: 105 Location: USA
Audio files: 12
|
|
Back to top
|
|
 |
Horlepiep
Joined: Dec 17, 2021 Posts: 10 Location: Netherlands
|
Posted: Fri Dec 17, 2021 5:15 pm Post subject:
|
 |
|
Code: | SawOsc s => dac;
Std.mtof(msg.data2) => s.freq;
0.5 => s.gain;
0.5::second => now; |
My idea was that the above part of my code would make the note sound only for 0.5 seconds. But apparently that doesn't work. Although something similar does work in this code example:
Code: |
// Synthesis patch
SinOsc foo => dac;
// Infinite time loop
while( true )
{
// Randomly choose a frequency
Math.random2f( 30, 1000 ) => foo.freq;
// Advance time
100::ms => now;
}
|
How can I stop the SawOsc? |
|
Back to top
|
|
 |
wakyct
Joined: Dec 30, 2020 Posts: 105 Location: USA
Audio files: 12
|
Posted: Fri Dec 17, 2021 5:33 pm Post subject:
|
 |
|
You may have seen the Chuck tutorial,
Quote: | But for now, let's generate our sine wave and hear it by adding one more line:
// connect sine oscillator to D/A convertor (sound card)
SinOsc s => dac;
// allow 2 seconds to pass
2::second => now;
Let's now run this (assuming you saved the file as 'foo.ck'):
%> chuck foo.ck
This would cause the sound to play for 2 seconds, during which time audio data is processed (and heard), afterwhich the program exits (since it has reached the end). For now, we can just take the second line of code to mean "let time pass for 2 seconds (and let audio compute during that time)". If you want to play it indefinitely, we could write a loop:
// connect sine oscillator to D/A convertor (sound card)
SinOsc s => dac;
// loop in time
while( true ) {
2::second => now;
}
In ChucK, this is called a 'time-loop' (in fact this is an 'infinite time loop'). This program executes (and generate/process audio) indefinitely. Try runnig this program. |
Because your code is in the while loop, it creates the oscillator over and over. Therefore I think you'd have to handle the note off like in the polyphonic example. |
|
Back to top
|
|
 |
Horlepiep
Joined: Dec 17, 2021 Posts: 10 Location: Netherlands
|
Posted: Fri Dec 17, 2021 5:50 pm Post subject:
|
 |
|
Thank you, that must be it. |
|
Back to top
|
|
 |
|