From 2d41b9b4f63d5fb6656b9a3817c2347adc2010f2 Mon Sep 17 00:00:00 2001 From: doohoyi Date: Tue, 10 Dec 2019 22:12:54 +0900 Subject: [PATCH] working on clients (keyboard & samplers) --- client/src/main.cpp | 29 ---- {client => osc}/platformio.ini | 0 osc/src/main.cpp | 81 +++++++++++ sampler/platformio.ini | 28 ++++ sampler/src/main.cpp | 257 +++++++++++++++++++++++++++++++++ 5 files changed, 366 insertions(+), 29 deletions(-) delete mode 100644 client/src/main.cpp rename {client => osc}/platformio.ini (100%) create mode 100644 osc/src/main.cpp create mode 100644 sampler/platformio.ini create mode 100644 sampler/src/main.cpp diff --git a/client/src/main.cpp b/client/src/main.cpp deleted file mode 100644 index 0adb486..0000000 --- a/client/src/main.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include -const int16_t I2C_ADDR = 3; - -void receiveEvent(int howMany) { - (void) howMany; - while (1 < Wire.available()) { - char c = Wire.read(); - Serial.print(c); - } - char x = Wire.read(); - Serial.println(x); -} - -void requestEvent() { - if (random(1000) == 0) { - Wire.write("[bcdefghabcdefghabcdefghabcdefg]"); //32 bytes - } else { - Wire.write(" "); // no letter to send - } -} - -void setup() { - Wire.begin(I2C_ADDR); - Wire.onRequest(requestEvent); - Wire.onReceive(receiveEvent); -} - -void loop() { -} diff --git a/client/platformio.ini b/osc/platformio.ini similarity index 100% rename from client/platformio.ini rename to osc/platformio.ini diff --git a/osc/src/main.cpp b/osc/src/main.cpp new file mode 100644 index 0000000..042c222 --- /dev/null +++ b/osc/src/main.cpp @@ -0,0 +1,81 @@ +//arduino +#include + +//osc +#include +#ifdef SLIP_USBSERIAL +#include +SLIPEncodedUSBSerial SLIPSerial(Serial); + +//i2c +#include +const int16_t I2C_ADDR = 3; +#define POST_LENGTH 32 +#define POST_BUFF_LEN (POST_LENGTH + 1) +//==================== +// postman's protocol +// letter frame ( '[' + 30 bytes + ']' ) +// : [123456789012345678901234567890] +// 'MIDI' letter frame +// : [123456789012345678901234567890] +// : [KKKVVVG.......................] +// : KKK - Key +// .substring(1, 4); +// : VVV - Velocity (volume/amp.) +// .substring(4, 7); +// : G - Gate (note on/off) +// .substring(7, 8); +//=================== +void receiveEvent(int howMany) { + // nothing to expect.. but.. let's just print out.. + while (Wire.available()) + Serial.print(Wire.read()); + Serial.println(); +} +// well. i don't know how fast should i be able to send msg.. to the net.. +// first test, and then.. if needed. will come back. +bool new_letter; +char letter_outro[POST_BUFF_LEN] = "................................"; +void requestEvent() { + if (random(1000) == 0) { + Wire.write("[bcdefghabcdefghabcdefghabcdefg]"); //32 bytes + } else { + Wire.write(" "); // no letter to send + } +} + +// +void midinote(OSCMessage &msg, int offset) { + // msg.match("/oncnt"); + if (msg.match("/onoff")) { + msg.match("/velocity"); + msg.match("/pitch"); + } +} + +// +void setup() { + //i2c + Wire.begin(I2C_ADDR); + Wire.onRequest(requestEvent); + Wire.onReceive(receiveEvent); + + //osc + SLIPSerial.begin(57600); +} + +void loop() { + //osc + OSCBundle bundleIN; + int size; + while(!SLIPSerial.endofPacket()) { + if( (size = SLIPSerial.available()) > 0) { + while(size--) { + bundleIN.fill(SLIPSerial.read()); + } + } + } + if(!bundleIN.hasError()) { + bundleIN.route("/note", midinote); + } +} diff --git a/sampler/platformio.ini b/sampler/platformio.ini new file mode 100644 index 0000000..1b94da1 --- /dev/null +++ b/sampler/platformio.ini @@ -0,0 +1,28 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[platformio] +env_default = teensy35 + +[common] +lib_deps = + 721@3.0.2 ; TaskScheduler + +[env:teensy35] +platform = teensy@3.6.0 +board = teensy35 +framework = arduino +lib_deps = ${common.lib_deps} + +[env:teensy36] +platform = teensy@3.6.0 +board = teensy36 +framework = arduino +lib_deps = ${common.lib_deps} diff --git a/sampler/src/main.cpp b/sampler/src/main.cpp new file mode 100644 index 0000000..1825cf0 --- /dev/null +++ b/sampler/src/main.cpp @@ -0,0 +1,257 @@ +//HACK: let auto-poweroff speakers stay turned ON! - (creative muvo mini) +#define IDLE_FREQ 22000 +#define IDLE_AMP 0 // --> creative muvo 2 doesn't need this. they just stay on! + +//i2c +#include +#define I2C_ADDR 3 +#define POST_LENGTH 32 +#define POST_BUFF_LEN (POST_LENGTH + 1) + +//teensy audio +#include +#include +#include +#include + +//teensy 3.5 with SD card +#define SDCARD_CS_PIN BUILTIN_SDCARD +#define SDCARD_MOSI_PIN 11 // not actually used +#define SDCARD_SCK_PIN 13 // not actually used + +// GUItool: begin automatically generated code +AudioPlaySdWav playSdWav1; //xy=224,265 +AudioSynthWaveformSine sine1; //xy=236,361 +AudioMixer4 mixer2; //xy=497,328 +AudioMixer4 mixer1; //xy=499,245 +AudioAmplifier amp1; //xy=633,245 +AudioAmplifier amp2; //xy=634,328 +AudioOutputAnalogStereo dacs1; //xy=788,284 +AudioConnection patchCord1(playSdWav1, 0, mixer1, 0); +AudioConnection patchCord2(playSdWav1, 1, mixer2, 0); +AudioConnection patchCord3(sine1, 0, mixer1, 1); +AudioConnection patchCord4(sine1, 0, mixer2, 1); +AudioConnection patchCord5(mixer2, amp2); +AudioConnection patchCord6(mixer1, amp1); +AudioConnection patchCord7(amp1, 0, dacs1, 0); +AudioConnection patchCord8(amp2, 0, dacs1, 1); +// GUItool: end automatically generated code + +//task +#include +Scheduler runner; +//song # +int sample_now = 0; //0~99 +// +void sound_player_start() +{ + //filename buffer - 8.3 naming convension! 8+1+3+1 = 13 + char filename[13] = "NN.WAV"; + //search for the sound file + int limit = (sample_now % 100); // 0~99 + filename[0] = '0' + (limit / 10); // [N]N.WAV + filename[1] = '0' + (limit % 10); // N[N].WAV + //TEST + Serial.println(filename); + //start the player! + //NOTE: block out 're-triggering' + if (playSdWav1.isPlaying() == false) { + playSdWav1.play(filename); + } + //mark the indicator : HIGH: ON + // digitalWrite(13, HIGH); + //to wait a bit for updating isPlaying() + delay(10); +} +void sound_player_stop() { + //stop the player. + if (playSdWav1.isPlaying() == true) { + playSdWav1.stop(); + } +} +void sound_player_check() { + if (playSdWav1.isPlaying() == false) { + //mark the indicator : LOW: OFF + digitalWrite(13, LOW); + //let speaker leave turned ON! + sine1.amplitude(IDLE_AMP); + } + else { + //let speaker leave turned ON! + sine1.amplitude(0); + } +} +// +Task sound_player_start_task(0, TASK_ONCE, sound_player_start); +Task sound_player_stop_task(0, TASK_ONCE, sound_player_stop); +Task sound_player_check_task(0, TASK_FOREVER, sound_player_check, &runner, true); + +//i2c +#include +void requestEvent() { + Wire.write(" "); // no letter to send +} +void receiveEvent(int numBytes) { + //numBytes : how many bytes received(==available) + static char letter_intro[POST_BUFF_LEN] = "................................"; + + // Serial.println("[i2c] on receive!"); + + int nb = Wire.readBytes(letter_intro, POST_LENGTH); + Serial.println(letter_intro); + + if (POST_LENGTH == nb) { + + //convert to String + String msg = String(letter_intro); + + //parse letter string. + + // letter frame ( '[' + 30 bytes + ']' ) + // : [123456789012345678901234567890] + + // 'MIDI' letter frame + // : [123456789012345678901234567890] + // : [KKKVVVG.......................] + // : KKK - Key + // .substring(1, 4); + // : VVV - Velocity (volume/amp.) + // .substring(4, 7); + // : G - Gate (note on/off) + // .substring(7, 8); + + String str_key = msg.substring(1, 4); + String str_velocity = msg.substring(4, 7); + String str_gate = msg.substring(7, 8); + + // + int key = str_key.toInt(); + sample_now = key; + // + int velocity = str_velocity.toInt(); // 0 ~ 127 + float amp_gain = (float)velocity / 127.0; + amp1.gain(amp_gain); + amp2.gain(amp_gain); + // + int gate = str_gate.toInt(); + if (gate == 0) { + sound_player_stop_task.restart(); + } else { + sound_player_start_task.restart(); + } + } +} + +// SD TEST +void printDirectory(File dir, int numTabs) { + while(true) { + + File entry = dir.openNextFile(); + if (!entry) { + // no more files + //Serial.println("**nomorefiles**"); + break; + } + for (uint8_t i=0; i +// const int16_t I2C_ADDR = 3; +// +// void receiveEvent(int howMany) { +// (void) howMany; +// while (1 < Wire.available()) { +// char c = Wire.read(); +// Serial.print(c); +// } +// char x = Wire.read(); +// Serial.println(x); +// } +// +// void requestEvent() { +// if (random(1000) == 0) { +// Wire.write("[bcdefghabcdefghabcdefghabcdefg]"); //32 bytes +// } else { +// Wire.write(" "); // no letter to send +// } +// } +// +// void setup() { +// Wire.begin(I2C_ADDR); +// Wire.onRequest(requestEvent); +// Wire.onReceive(receiveEvent); +// } +// +// void loop() { +// }