working on clients (keyboard & samplers)
This commit is contained in:
parent
ffb8190b6b
commit
2d41b9b4f6
5 changed files with 366 additions and 29 deletions
|
|
@ -1,29 +0,0 @@
|
|||
#include <Wire.h>
|
||||
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() {
|
||||
}
|
||||
81
osc/src/main.cpp
Normal file
81
osc/src/main.cpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
//arduino
|
||||
#include <Arduino.h>
|
||||
|
||||
//osc
|
||||
#include <OSCBundle.h>
|
||||
#ifdef SLIP_USBSERIAL
|
||||
#include <SLIPEncodedUSBSerial.h>
|
||||
SLIPEncodedUSBSerial SLIPSerial(Serial);
|
||||
|
||||
//i2c
|
||||
#include <Wire.h>
|
||||
const int16_t I2C_ADDR = 3;
|
||||
#define POST_LENGTH 32
|
||||
#define POST_BUFF_LEN (POST_LENGTH + 1)
|
||||
//==========<protocol>==========
|
||||
// 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);
|
||||
//==========</protocol>=========
|
||||
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);
|
||||
}
|
||||
}
|
||||
28
sampler/platformio.ini
Normal file
28
sampler/platformio.ini
Normal file
|
|
@ -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}
|
||||
257
sampler/src/main.cpp
Normal file
257
sampler/src/main.cpp
Normal file
|
|
@ -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 <Wire.h>
|
||||
#define I2C_ADDR 3
|
||||
#define POST_LENGTH 32
|
||||
#define POST_BUFF_LEN (POST_LENGTH + 1)
|
||||
|
||||
//teensy audio
|
||||
#include <Audio.h>
|
||||
#include <SPI.h>
|
||||
#include <SD.h>
|
||||
#include <SerialFlash.h>
|
||||
|
||||
//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 <TaskScheduler.h>
|
||||
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 <Wire.h>
|
||||
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<numTabs; i++) {
|
||||
Serial.print('\t');
|
||||
}
|
||||
Serial.print(entry.name());
|
||||
if (entry.isDirectory()) {
|
||||
Serial.println("/");
|
||||
printDirectory(entry, numTabs+1);
|
||||
} else {
|
||||
// files have sizes, directories do not
|
||||
Serial.print("\t\t");
|
||||
Serial.println(entry.size(), DEC);
|
||||
}
|
||||
entry.close();
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
File root;
|
||||
void setup() {
|
||||
|
||||
//serial monitor
|
||||
Serial.begin(9600);
|
||||
delay(50);
|
||||
|
||||
//i2c
|
||||
Wire.begin(I2C_ADDR);
|
||||
Wire.onReceive(receiveEvent);
|
||||
Wire.onRequest(requestEvent);
|
||||
|
||||
//SD - AudioPlaySdWav @ teensy audio library needs SD.begin() first. don't forget/ignore!
|
||||
//+ let's additionally check contents of SD.
|
||||
if (!SD.begin(BUILTIN_SDCARD)) {
|
||||
Serial.println("[sd] initialization failed!");
|
||||
return;
|
||||
}
|
||||
Serial.println("[sd] initialization done.");
|
||||
root = SD.open("/");
|
||||
printDirectory(root, 0);
|
||||
|
||||
//audio
|
||||
AudioMemory(20);
|
||||
mixer1.gain(0,1.0);
|
||||
mixer1.gain(1,1.0);
|
||||
mixer1.gain(2,0);
|
||||
mixer1.gain(3,0);
|
||||
mixer2.gain(0,1.0);
|
||||
mixer2.gain(1,1.0);
|
||||
mixer2.gain(2,0);
|
||||
mixer2.gain(3,0);
|
||||
amp1.gain(1.0);
|
||||
amp2.gain(1.0);
|
||||
|
||||
//let auto-poweroff speakers stay turned ON!
|
||||
sine1.frequency(IDLE_FREQ);
|
||||
|
||||
//led
|
||||
pinMode(13, OUTPUT);
|
||||
digitalWrite(13, LOW); // LOW: OFF
|
||||
|
||||
//player task
|
||||
runner.addTask(sound_player_start_task);
|
||||
runner.addTask(sound_player_stop_task);
|
||||
|
||||
//
|
||||
Serial.println("[setup] done.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
runner.execute();
|
||||
}
|
||||
|
||||
////
|
||||
// postman - bi-directional protocol test code backup
|
||||
|
||||
// #include <Wire.h>
|
||||
// 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() {
|
||||
// }
|
||||
Loading…
Reference in a new issue