In my last blogs, I described how I managed to extend TinyUSB to provide an simple USB Audio device in Arduino.

So far I have been using some randon numbers to generate noise. This is not very helpful to check if we lost some samples. A SawTooth wave is much more convinient.

We can use the AudioTools to generate this. Here is the example sketch, that also shows how we can define an Arduino Stream as input (as an alternative to the callbacks) that we have used so far.

#include "Adafruit_TinyUSB.h"
#include "AudioTools.h"  // https://github.com/pschatzmann/arduino-audio-tools

Adafruit_USBD_Audio usb;
AudioInfo info(44100, 2, 16);
SawToothGenerator<int16_t> sawtooth;               
GeneratedSoundStream<int16_t> sawthooth_stream(sawtooth);

void setup() {
  Serial.begin(115200);
  //while(!Serial);  // wait for serial

  // generate 493 hz (note B4)
  sawtooth.begin(info, 493.0f);

  // Start USB device as Audio Source
  usb.setInput(sawthooth_stream);
  usb.begin(info.sample_rate, info.channels, info.bits_per_sample);
}

void loop() {
  // optional: use LED do display status
  usb.updateLED();
}

We just define a SawTooth, convert it to a GeneratedSoundStream and start the sawtooth by calling begin.

And here is the result, visualized with the help of Audacity:

Dependencies

Please note that this example is the conventions used at the time of posting. This might change, so you better double check with the examples that are provided with the TinyUSB project!


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *