Author |
Message |
Cynosure
Site Admin
Joined: Dec 11, 2010 Posts: 966 Location: Toronto, Ontario - Canada
Audio files: 82
|
Posted: Tue Jul 03, 2012 4:39 pm Post subject:
Dual Loops? |
|
|
Is it possible to run 2 loops at the same time?
I am currently using the void loop() command to listen to midi notes. I want it to change the delay time for a second loop that is making a squarewave on one of the logic outputs.
Or, is there a better method for sending out the correct note according to the midi signal that is received? _________________ JacobWatters.com |
|
Back to top
|
|
|
mosc
Site Admin
Joined: Jan 31, 2003 Posts: 18195 Location: Durham, NC
Audio files: 211
G2 patch files: 60
|
Posted: Tue Jul 03, 2012 4:54 pm Post subject:
|
|
|
Congrats, you are the first to post in this Arduino forum. I have no experience with these cool little processors, but I'm hoping they will continue to take off in the DIY music community. _________________ --Howard
my music and other stuff |
|
Back to top
|
|
|
MusicMan11712
Joined: Aug 08, 2009 Posts: 1082 Location: Out scouting . . .
|
Posted: Tue Jul 03, 2012 6:59 pm Post subject:
|
|
|
I would think that you would need some midi handling routines.
(1) get midi data, filter it, put it in a buffer
(2) process any midi data on the buffer.
I am not sure if the arduino midi library has that already in there. If so, that might save you from having to write your own midi handlers.
Steve |
|
Back to top
|
|
|
elmegil
Joined: Mar 20, 2012 Posts: 2177 Location: Chicago
Audio files: 16
|
Posted: Tue Jul 03, 2012 9:47 pm Post subject:
|
|
|
I've used the MIDI library, it does a good job of handling the housekeeping; I'm not sure whether it has filtering or not, but I suspect not. What type of filtering do you have in mind?
AFAIK no, you can't run multiple loops, but I think there are ways to do unrelated asynchronous things, which would get you something like the same ability. |
|
Back to top
|
|
|
Cynosure
Site Admin
Joined: Dec 11, 2010 Posts: 966 Location: Toronto, Ontario - Canada
Audio files: 82
|
Posted: Wed Jul 04, 2012 5:10 am Post subject:
|
|
|
I am using the midi library, but I am still learning it.
It is reading the note on messages, but I don't think it is getting the note number correct.
Here is what I am trying to do:
Have an oscillator on the arduino sending out sound all the time with a default frequency of 0.
When a note is pressed it reads the note number, changes the oscillator frequency to the note's frequency and sends a pulse trigger out on one of the logic outputs.
Is there a midi note number to frequency or delay time (used to change the frequency when bit-bashing) function in the midi library? _________________ JacobWatters.com |
|
Back to top
|
|
|
elmegil
Joined: Mar 20, 2012 Posts: 2177 Location: Chicago
Audio files: 16
|
Posted: Wed Jul 04, 2012 9:39 am Post subject:
|
|
|
I don't know of one in the library. That seems to frequently be handled by lookup table. In my case, I was mapping the note number to a control voltage, so all that the circuit had to do was map 0 - 127 onto 0V - 10V, which was easy to do by setting the reference voltage on a DAC. (And to be clear "I" didn't design that part of the circuit, it was from the MTS-100 by Thomas Henry).
I just went back to look at the info about it and saw the following things in the "features" list:
Software Thru, with message filtering. <<< so it looks like filtering is there
Use callbacks to handle input messages more easily. <<< this is what I was talking about for "background" processing; using callbacks lets you do something else in the main loop and then process MIDI data when it arrives
I don't see anything about a frequency table in the docs or on their main page.
I found this thread on the Arduino forums that has some sample code that includes a frequency table of some sort (the code by cyberheater). That looked like it could be useful.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1222425476/all
I'm curious what you mean about the wrong note number...
(Edit: helps if you include the link...) |
|
Back to top
|
|
|
comrade_zero
Joined: Mar 05, 2009 Posts: 66 Location: arizona
Audio files: 4
|
Posted: Fri Jul 06, 2012 12:31 am Post subject:
simple MIDI squarewave synth |
|
|
O.K., so here is some simple code for what you want. Please note it does not use the Arduino MIDI library. You need a properly constructed MIDI in circuit (5-pin DIN connector, optocoupler, etc. the MIDIvox site has a good circuit diagram if I recall correctly) sending Serial In data to your Arduino. This code only provides 60 notes, you can expand it as necessary. There are MIDI note number to frequency charts available on the web, you'll also want to check out the "tone" command in the Arduino resources...
Code: |
int state = 0;
int note = 0;
int velocity = 0;
int counter = 0;
byte inByte;
//array of note values will go here
int pitch [60] = {65,69,73,77,82,87,92,97,103,110,116,123,130,138,146,155,164,174,184,195,207,220,233,246,261,277,293,311,329,349,369,391,415,440,466,493,523,554,587,622,659,698,739,783,830,880,932,987,1046,1108,1174,1244,1318,1396,1479,1567,1661,1760,1864,1975};
void setup()
{
Serial.begin(31250);
pinMode(7,OUTPUT);
}
void loop()
{
while (1)
{
if (Serial.available())
{
inByte = Serial.read();
switch(state)
{
case 0:
if (inByte == 144)
{
state += 1;
}
break;
case 1:
if (inByte < 128)
{
note = inByte;
state += 1;
}
break;
case 2:
if (inByte < 128)
{
velocity = inByte;
if (velocity == 0)
{
state = 4;
break;
}
else
{
state = 3;
break;
}
break;
}
case 3:
inByte = Serial.read();
while (inByte > 0)
{
tone(7,pitch[note-36]);
inByte = Serial.read();
if (inByte == 0)
{
state = 4;
break;
}
}
case 4:
//digitalWrite(7,LOW);///turns off any notes
noTone(7);
state = 1;
}
}
}
}
|
This was compiled in Arduino alpha 022 (earlier versions may not have "tone" available.)
Anyway, if you have questions or if this doesn't work for you, please let me know.
Good luck, and I hope this helps.
c_z |
|
Back to top
|
|
|
Cynosure
Site Admin
Joined: Dec 11, 2010 Posts: 966 Location: Toronto, Ontario - Canada
Audio files: 82
|
Posted: Fri Jul 06, 2012 5:36 am Post subject:
|
|
|
Thanks for the response and the code sample. I did end up finding a way to do it using a library called Tone. I just got it working late lastnight. All the complicated stuff is in the library, so all I have to do is tell it the frequency and pin number.
It can output up to 3 tones at the same time on any three logic pins, so I now have a note and its major and minor thirds being outputted.
I plan on bumping the note up an octave and sending it to a divider. Then I can have the note an octave above, the base note, as many sub octaves as I want, and the major and minor thirds
I am really starting to like this little arduino thing. It will be a must have for all my future Lunetta synth projects. _________________ JacobWatters.com |
|
Back to top
|
|
|
|