Teensy Audio: AudioOutputUSB only working when combined with different AudioOutput

emiel.h

Member
Hi!

I've ran into a bug in the Teensy audio library when playing audio over USB.
Apparently "AudioOutputUSB" only works when there is also a different audio output (like AudioOutputAnalog) present in the sketch.
The following code should send a sound over USB but it only works when AudioOutputAnalog is uncommented.

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioSynthWaveformSine   sine1;
//AudioOutputAnalog        dac1;
AudioOutputUSB           usb1;
AudioConnection          patchCord1(sine1, 0, usb1, 0);
AudioConnection          patchCord2(sine1, 0, usb1, 1);

void setup() {
  AudioMemory(15);
  sine1.amplitude(0.2);
  sine1.frequency(500);
}

void loop() {
}

My best guess is that there is some optimization going on that checks whether there actually is an audio output available before it processes any sounds but doesn't take in account AudioOutputUSB.
 
Or you may include a PIT according to following example (include file for PIT0 to be include before setup())
Code:
/*
 * WMXZ Teensy core library
 * Copyright (c) 2017 WMXZ.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
// AudioTrigger.h
// ensures Audio updates for USB streaming
//

#ifndef AudioTrigger_h_
#define AudioTrigger_h_

#include "Arduino.h"
#include "AudioStream.h"
#include "utility/pdb.h"

// correct PDB_Period to PIT_PERIOD
#define PIT_PERIOD ((PDB_PERIOD+1)*128 -1)

class AudioTrigger : public AudioStream
{
public:
        AudioTrigger() : AudioStream(0, NULL) {init(); prio = 8;}
		void init(void);
        virtual void update(void) {;}
private:
        static bool update_responsibility;
		static void isr(void);
		int prio;
};

bool AudioTrigger::update_responsibility = false;

void AudioTrigger::init(void)
{	// check with AudioStream if we are responsable for updates
	update_responsibility = update_setup();

	// assign local function as ISR
	_VectorsRam[IRQ_PIT_CH0 + 16] = isr;
	NVIC_SET_PRIORITY(IRQ_PIT_CH0, prio*16);	
	NVIC_ENABLE_IRQ(IRQ_PIT_CH0);

	// turn on PIT clock
	SIM_SCGC6 |= SIM_SCGC6_PIT;
	// turn on PIT     
	PIT_MCR = 0x00;
	
	// Timer 0     
	PIT_LDVAL0 = PIT_PERIOD;     
	PIT_TCTRL0 = 2; // enable Timer 0 interrupts      
	PIT_TCTRL0 |= 1; // start Timer 0
}

void AudioTrigger::isr(void)
{
	PIT_TFLG0=1;
	if (update_responsibility) AudioStream::update_all();
}

AudioTrigger trigger;

#endif

I know this thread is old, but I wanted to give an alternative solution to adding audio adc of dac objects
 
Last edited:
Back
Top