electro-music.com   Dedicated to experimental electro-acoustic
and electronic music
 
    Front Page  |  Radio
 |  Media  |  Forum  |  Wiki  |  Links
Forum with support of Syndicator RSS
 FAQFAQ   CalendarCalendar   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   LinksLinks
 RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in  Chat RoomChat Room 
 Forum index » DIY Hardware and Software » ChucK programming language
While question
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
Author Message
Horlepiep



Joined: Dec 17, 2021
Posts: 10
Location: Netherlands

PostPosted: Fri Dec 17, 2021 1:14 pm    Post subject: While question
Subject description: Problem with the combination of a while-loop and MIDI input
Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
wakyct



Joined: Dec 30, 2020
Posts: 105
Location: USA
Audio files: 12

PostPosted: Fri Dec 17, 2021 4:52 pm    Post subject: Reply with quote  Mark this post and the followings unread

If you look at and compare with this example, https://chuck.cs.princeton.edu/doc/examples/midi/polyfony2.ck, it seems like your code is not handling the note off for the oscillator specifically, just in general.
Back to top
View user's profile Send private message
Horlepiep



Joined: Dec 17, 2021
Posts: 10
Location: Netherlands

PostPosted: Fri Dec 17, 2021 5:15 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
wakyct



Joined: Dec 30, 2020
Posts: 105
Location: USA
Audio files: 12

PostPosted: Fri Dec 17, 2021 5:33 pm    Post subject: Reply with quote  Mark this post and the followings unread

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
View user's profile Send private message
Horlepiep



Joined: Dec 17, 2021
Posts: 10
Location: Netherlands

PostPosted: Fri Dec 17, 2021 5:50 pm    Post subject: Reply with quote  Mark this post and the followings unread

Thank you, that must be it.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Moderators: Kassen
Page 1 of 1 [5 Posts]
View unread posts
View new posts in the last week
Mark the topic unread :: View previous topic :: View next topic
 Forum index » DIY Hardware and Software » ChucK programming language
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Forum with support of Syndicator RSS
Powered by phpBB © 2001, 2005 phpBB Group
Copyright © 2003 through 2009 by electro-music.com - Conditions Of Use