USB_MIDI_AUDIO_SERIAL and SLIPEncodedOSC

elgiano

Member
Hi!
I've seen in other posts that it's possible to use the USB_MIDI_AUDIO_SERIAL flag to use teensy as a USB soundcard and write to the Serial console. Is it also possible to communicate using OSC on the same USB port? I've tried but I couldn't make it work.
Here is some example code. It compiles and I see an audio interface streaming audio, but if I send OSC I get no reply. I noticed BOARD_HAS_USB_SERIAL is not defined by OSCData.h, so SLIP is initialized to Serial1... could this be the problem?

C:
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <Audio.h>
#include <Wire.h>

AudioSynthWaveformSine   sine1;
AudioInputUSB            usbIn;
AudioOutputUSB           usbOut;
AudioInputI2S            lineIn;
AudioOutputI2S           lineOut;
AudioConnection          patchCord1(usbIn, 0, lineOut, 0);
AudioConnection          patchCord2(usbIn, 1, lineOut, 1);
AudioConnection          patchCord3(sine1, 0, usbOut, 0);
AudioConnection          patchCord4(sine1, 0, usbOut, 1);
AudioControlSGTL5000     sgtl5000;

void initAudio() {
  AudioMemory(10);
  sgtl5000.enable();
  sgtl5000.inputSelect(AUDIO_INPUT_LINEIN);
  sine1.frequency(120);
  sine1.amplitude(0.6);
}

// OSC SETUP
#ifdef BOARD_HAS_USB_SERIAL
#include <SLIPEncodedUSBSerial.h>
SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
#else
#include <SLIPEncodedSerial.h>
 SLIPEncodedSerial SLIPSerial(Serial1);
#endif

// for debug
void osc_echo(OSCMessage& msg) {
  SLIPSerial.beginPacket();
    msg.send(SLIPSerial);
  SLIPSerial.endPacket();    
}

void osc_listen() {
  OSCBundle bundleIN;
  int size;
  while(!SLIPSerial.endofPacket()) {
    if ((size = SLIPSerial.available()) > 0) {
      while(size--) bundleIN.fill(SLIPSerial.read());
    }
  }
  if (!bundleIN.hasError()) {
    bundleIN.dispatch("/echo", osc_echo);
  }
}

void setup() {
  initAudio();
  SLIPSerial.begin(9600);
}

void loop() {
  osc_listen();
}

Then I've tried to patch the OSC library. In OSCData.h, SLIPEncodedUSBSerial.h and .cpp I add an extra #ifdef check:

C:
#if (defined(CORE_TEENSY) && defined(USB_SERIAL)) || (!defined(CORE_TEENSY) && defined(__AVR_ATmega32U4__)) || defined(__SAM3X8E__) || (defined(_USB) && defined(_USE_USB_FOR_SERIAL_)) || defined(BOARD_maple_mini) || defined(USB_MIDI_AUDIO_SERIAL)
I added only the last clause to check for USB_MIDI_AUDIO_SERIAL. This snippet was from SLIPEncodedUSBSerial.cpp.

And it seems to work fine for now: I get both audio and OSC communication. Has anyone tried or have any comments?
 
Back
Top