Browse Source

working

main
Hendrik Langer 5 years ago
parent
commit
ecb6755d74
  1. BIN
      A-Whole-New-World-(Theme-From-'Aladdin').mid
  2. BIN
      Bohemian-Rhapsody-1.mid
  3. BIN
      Star-Wars-Theme-(From-'Star-Wars').mid
  4. BIN
      darude-sandstorm.mid
  5. BIN
      mario64-medley.mid
  6. 2
      platformio.ini
  7. 42
      send.py
  8. BIN
      smario.mid
  9. 157
      src/main.cpp
  10. 12
      src/midi/pitches.h
  11. BIN
      toto-africa.mid

BIN
A-Whole-New-World-(Theme-From-'Aladdin').mid

Binary file not shown.

BIN
Bohemian-Rhapsody-1.mid

Binary file not shown.

BIN
Star-Wars-Theme-(From-'Star-Wars').mid

Binary file not shown.

BIN
darude-sandstorm.mid

Binary file not shown.

BIN
mario64-medley.mid

Binary file not shown.

2
platformio.ini

@ -13,7 +13,7 @@ default_envs = megaatmega2560
[common] [common]
lib_deps = lib_deps =
Tone ToneLibrary
# AccelStepper # AccelStepper
# SpeedyStepper # SpeedyStepper
MIDI Library MIDI Library

42
send.py

@ -1,15 +1,17 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import sys, getopt import sys, argparse
import time import time
import threading import threading
from mido import MidiFile from mido import MidiFile
import serial import serial
drum_channels = [9]
def serialreceive(ser): def serialreceive(ser):
print("Serial running") print("Serial running")
while True: while ser:
try: try:
line = ser.readline().decode() line = ser.readline().decode()
print(line) print(line)
@ -20,32 +22,36 @@ def serialreceive(ser):
def main(argv): def main(argv):
inputfile = '' inputfile = ''
outputport = '' outputport = ''
try: parser = argparse.ArgumentParser()
opts, args = getopt.getopt(argv, "hi:o:",["ifile=","oport="]) parser.add_argument('midifilename', help="the midi file you want to send")
except getopt.GetoptError: parser.add_argument('-P', '--serialport', help="serial port to open", default="/dev/ttyUSB0")
print('send.py -i <inputfile> -o <outputport>') parser.add_argument('--with-drums', action="store_true", help="send percussion track 10")
sys.exit(2) args = parser.parse_args()
for opt, arg in opts: with serial.Serial(args.serialport, 115200, timeout=12) as ser:
if opt == '-h':
print('send.py -i <inputfile> -o <outputport>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--oport"):
outputport = arg
with serial.Serial(outputport, 115200, timeout=12) as ser:
receivethread = threading.Thread(target=serialreceive, args=(ser,)) receivethread = threading.Thread(target=serialreceive, args=(ser,))
receivethread.daemon = True
receivethread.start() receivethread.start()
with MidiFile(inputfile) as mid: time.sleep(1)
with MidiFile(args.midifilename) as mid:
for msg in mid: for msg in mid:
time.sleep(msg.time) time.sleep(msg.time)
if not msg.is_meta: if not msg.is_meta:
if msg.type == 'note_on' or msg.type == 'note_off': if msg.type == 'note_on' or msg.type == 'note_off':
if (not (msg.channel in drum_channels)) or args.with_drums:
print(msg) print(msg)
ser.write(msg.bin()) ser.write(msg.bin())
else:
print("skipped percussion: " + str(msg))
else: else:
print("not sent: " + str(msg)) print("not sent: " + str(msg))
#time.sleep(1000) if msg.type == 'program_change' and msg.program in [50,]:
drum_channels.append(msg.channel)
else:
print("meta message: " + str(msg))
print("EOF")
print("done")
print("serial closed")
if __name__ == "__main__": if __name__ == "__main__":
main(sys.argv[1:]) main(sys.argv[1:])

BIN
smario.mid

Binary file not shown.

157
src/main.cpp

@ -20,14 +20,33 @@
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings); MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial, MIDI, MySettings);
Tone freq1; const int NUM_STEPPER = 5;
Tone freq2;
Tone freq3;
int ledState = LOW; // ledState used to set the LED Tone freq[NUM_STEPPER];
unsigned long previousMillis[NUM_STEPPER] = {0}; // will store last time LED was updated
const long interval = 1000; // interval at which to blink (milliseconds) const long interval = 1000; // interval at which to blink (milliseconds)
unsigned long previousMillis = 0; // will store last time LED was updated volatile byte playing[NUM_STEPPER] = {0};
unsigned long lastMillis[3] = {0,0,0}; // will store last time LED was updated int enable_pin[NUM_STEPPER] = {X_ENABLE_PIN, Y_ENABLE_PIN, Z_ENABLE_PIN, E0_ENABLE_PIN, E1_ENABLE_PIN};
int findSameNotes(byte noteNumber) {
int candidate = -1;
bool alreadyPlayingHarmonic = false;
for (int i=0; i<NUM_STEPPER; i++) {
if (playing[i] == noteNumber) return -2; // already playing this note
if (noteNumber+12 == playing[i] || noteNumber == playing[i]+12) alreadyPlayingHarmonic = true;
for (int k=i+1; k<NUM_STEPPER; k++) {
if (playing[k] == playing[i]) {
return k;
} else if (playing[k]+12 == playing[i] || playing[k] == playing[i]+12) {
candidate = k;
}
}
}
if (candidate >= 0 && !alreadyPlayingHarmonic) return candidate;
return -1;
}
void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity) void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity)
@ -38,42 +57,68 @@ void handleNoteOn(byte inChannel, byte inNumber, byte inVelocity)
// Serial.println(inVelocity); // Serial.println(inVelocity);
// Serial.flush(); // Serial.flush();
//noTone(); //noTone();
long freq = sNotePitches[inNumber];
freq *=4; //if (inChannel == 0 || inChannel >= 32) return;
while (freq > 12000) freq/=2; //if (inChannel == 8) return;
if (inChannel == 9) digitalWrite(LED_PIN, HIGH);
if (inChannel == 9) { long f = sNotePitches[inNumber];
freq1.play(freq); f *= 4;
lastMillis[0] = millis(); while (f > 12000) f/=2;
} else if (inChannel == 7) { //digitalWrite(LED_PIN, HIGH);
freq2.play(freq); bool sent = false;
lastMillis[1] = millis(); for (int i=0; i<NUM_STEPPER; i++) {
} else if (inChannel == 2) { if (playing[i] == 0) {
freq3.play(freq); digitalWrite(enable_pin[i], LOW);
lastMillis[2] = millis(); freq[i].play(f);
playing[i] = inNumber;
previousMillis[i] = millis();
sent = true;
break;
} }
} }
if (!sent) {
digitalWrite(LED_PIN, HIGH);
int i = findSameNotes(inNumber);
if (i >= 0) {
digitalWrite(enable_pin[i], LOW);
freq[i].play(f);
playing[i] = inNumber;
previousMillis[i] = millis();
}
}
}
void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity) void handleNoteOff(byte inChannel, byte inNumber, byte inVelocity)
{ {
// Serial.print("NoteOff "); // Serial.print("NoteOff ");
// Serial.print(inNumber); // Serial.print(inNumber);
// Serial.print("\tvelocity: "); // Serial.print("\tvelocity: ");
// Serial.println(inVelocity); // Serial.println(inVelocity);
if (inChannel == 9) digitalWrite(LED_PIN, LOW);
if (inChannel == 9) freq1.stop(); //if (inChannel == 0 || inChannel >= 32) return;
if (inChannel == 7) freq2.stop(); //if (inChannel == 8) return;
if (inChannel == 2) freq3.stop();
digitalWrite(LED_PIN, LOW);
for (int i=0; i<NUM_STEPPER; i++) {
if (playing[i] == inNumber) {
freq[i].stop();
playing[i] = 0;
break;
}
}
} }
void setup() void setup()
{ {
Serial.begin(115200); Serial.begin(115200);
delay(500); /* delay(500);
Serial.println("##########"); Serial.println("##########");
Serial.println("MIDI"); Serial.println("MIDI");
Serial.println("##########"); Serial.println("##########");
Serial.flush(); Serial.flush();
*/
// initialize LED digital pin as an output. // initialize LED digital pin as an output.
pinMode(LED_PIN, OUTPUT); pinMode(LED_PIN, OUTPUT);
@ -89,61 +134,40 @@ void setup()
pinMode(Z_STEP_PIN, OUTPUT); pinMode(Z_STEP_PIN, OUTPUT);
pinMode(Z_ENABLE_PIN, OUTPUT); pinMode(Z_ENABLE_PIN, OUTPUT);
digitalWrite(X_DIR_PIN, HIGH); pinMode(E0_DIR_PIN, OUTPUT);
digitalWrite(X_ENABLE_PIN, HIGH); pinMode(E0_STEP_PIN, OUTPUT);
pinMode(E0_ENABLE_PIN, OUTPUT);
pinMode(E1_DIR_PIN, OUTPUT);
pinMode(E1_STEP_PIN, OUTPUT);
pinMode(E1_ENABLE_PIN, OUTPUT);
digitalWrite(Y_DIR_PIN, HIGH); for (int i=0; i<NUM_STEPPER; i++) {
digitalWrite(Y_ENABLE_PIN, HIGH); digitalWrite(enable_pin[i], HIGH);
}
digitalWrite(Z_DIR_PIN, HIGH);
digitalWrite(Z_ENABLE_PIN, HIGH);
MIDI.setHandleNoteOn(handleNoteOn); MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff); MIDI.setHandleNoteOff(handleNoteOff);
MIDI.begin(MIDI_CHANNEL_OMNI); MIDI.begin(MIDI_CHANNEL_OMNI);
freq1.begin(X_STEP_PIN); freq[0].begin(X_STEP_PIN);
freq2.begin(Y_STEP_PIN); freq[1].begin(Y_STEP_PIN);
freq3.begin(Z_STEP_PIN); freq[2].begin(Z_STEP_PIN);
freq[3].begin(E0_STEP_PIN);
// digitalWrite(X_ENABLE_PIN, LOW); freq[4].begin(E1_STEP_PIN);
// tone(X_STEP_PIN, 14000);
// tone(X_STEP_PIN, 4*440);
// delay(2000);
} }
void loop() void loop()
{ {
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
Serial.println("arduino");
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
//digitalWrite(LED_PIN, ledState);
}
if (currentMillis - lastMillis[0] >= 2000 || lastMillis[0] == 0) { for (int i=0; i<NUM_STEPPER; i++) {
digitalWrite(X_ENABLE_PIN, HIGH); if (currentMillis - previousMillis[i] >= interval || previousMillis[i] == 0) {
} else { digitalWrite(enable_pin[i], HIGH);
digitalWrite(X_ENABLE_PIN, LOW); playing[i] = 0;
}
if (currentMillis - lastMillis[1] >= 2000 || lastMillis[1] == 0) {
digitalWrite(Y_ENABLE_PIN, HIGH);
} else {
digitalWrite(Y_ENABLE_PIN, LOW);
} }
if (currentMillis - lastMillis[2] >= 2000 || lastMillis[2] == 0) {
digitalWrite(Z_ENABLE_PIN, HIGH);
} else {
digitalWrite(Z_ENABLE_PIN, LOW);
} }
//Serial.println("test"); //Serial.println("test");
@ -159,4 +183,7 @@ void loop()
//tone(X_STEP_PIN, 120U); //tone(X_STEP_PIN, 120U);
// wait for a second // wait for a second
//delay(100); //delay(100);
} }

12
src/midi/pitches.h

@ -92,6 +92,14 @@
#define NOTE_CS8 4435 #define NOTE_CS8 4435
#define NOTE_D8 4699 #define NOTE_D8 4699
#define NOTE_DS8 4978 #define NOTE_DS8 4978
#define NOTE_E8 5274
#define NOTE_F8 5588
#define NOTE_FS8 5920
#define NOTE_G8 6272
#define NOTE_GS8 6645
#define NOTE_A8 7040
#define NOTE_AS8 7459
#define NOTE_B8 7902
static const uint16_t sNotePitches[] = { static const uint16_t sNotePitches[] = {
NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1, NOTE_E1, NOTE_F1, NOTE_FS1, NOTE_B0, NOTE_C1, NOTE_CS1, NOTE_D1, NOTE_DS1, NOTE_E1, NOTE_F1, NOTE_FS1,
@ -104,5 +112,7 @@ static const uint16_t sNotePitches[] = {
NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, NOTE_C6, NOTE_CS6, NOTE_D6,
NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6,
NOTE_B6, NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_B6, NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7,
NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7, NOTE_C8, NOTE_CS8, NOTE_D8, NOTE_DS8, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7, NOTE_C8, NOTE_CS8, NOTE_D8,
NOTE_DS8, NOTE_E8, NOTE_F8, NOTE_FS8, NOTE_G8, NOTE_GS8, NOTE_A8, NOTE_AS8,
NOTE_B8
}; };

BIN
toto-africa.mid

Binary file not shown.
Loading…
Cancel
Save