Skip to content

Audio Example AudioShield

MichaelMeissner edited this page Apr 1, 2020 · 1 revision

Audio Example AudioShield

This is the tonesweep program set up to use the PJRC audio shield. Revisions A-C of the audio shield are used with the Teensy 3.1, 3.2, 3.5, or 3.6. Revision D of the audio shield is used with the Teensy 4.0.

/*
  Demo of the audio sweep function.
  The user specifies the amplitude,
  start and end frequencies (which can sweep up or down)
  and the length of time of the sweep.

  Pins:		Teensy 4.0	Teensy 3.x

  LRCLK:	Pin 20/A6	Pin 23/A9
  BCLK:		Pin 21/A7	Pin 9
  DIN:		Pin 7		Pin 22/A8
  SCL (I2C):	Pin 19/A5	Pin 19/A5
  SDA (I2C):	Pin 18/A4	Pin 18/A4
  Ground:	Ground		Ground
  3.3v:		3.3v		3.3v
  VIN:		5v		5v  */

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

// GUItool: begin automatically generated code (edited by meissner afterwards).
AudioSynthToneSweep	tonesweep;		//xy=99,198
AudioControlSGTL5000	sgtl5000;
AudioMixer4		mixer2;			//xy=280,253
AudioMixer4		mixer1;			//xy=280,175
AudioOutputI2S		shield;			//xy=452,189

AudioConnection		patchCord1 (tonesweep, 0, mixer1, 0);
AudioConnection		patchCord2 (tonesweep, 0, mixer2, 0);
AudioConnection		patchCord3 (mixer2,    0, shield, 1);
AudioConnection		patchCord4 (mixer1,    0, shield, 0);
// GUItool: end automatically generated code

const float	t_ampx	= 0.8;
const int	t_lox	= 10;
const int	t_hix	= 22000;
const float	t_timex	= 10;		// Length of time for the sweep in seconds

// Do a sweep in both directions, enabling or disabling the left/right speakers
void do_sweep (int i)
{
  int do_left  = (i & 1) != 0;
  int do_right = (i & 2) != 0;
  float gain   = (do_left && do_right) ? 0.5f : 1.0f;

  Serial.printf ("Sweep up,   left = %c, right = %c\n",
		 (do_left)  ? 'Y' : 'N',
		 (do_right) ? 'Y' : 'N');

  mixer1.gain (0, do_left  ? gain : 0.0f);
  mixer2.gain (0, do_right ? gain : 0.0f);

  if (!tonesweep.play (t_ampx, t_lox, t_hix, t_timex)) {
    Serial.println ("ToneSweep - play failed");
    while (1)
      ;
  }

  // wait for the sweep to end
  while (tonesweep.isPlaying ())
    ;

  // and now reverse the sweep
  Serial.printf ("Sweep down, left = %c, right = %c\n",
		 (do_left)  ? 'Y' : 'N',
		 (do_right) ? 'Y' : 'N');

  if (!tonesweep.play (t_ampx, t_hix, t_lox, t_timex)) {
    Serial.println ("ToneSweep - play failed");
    while (1)
      ;
  }

  // wait for the sweep to end
  while (tonesweep.isPlaying ())
    ;

  Serial.println ("Sweep done");
}

void setup(void)
{
  // Wait for at least 3 seconds for the USB serial connection
  Serial.begin (9600);
  while (!Serial && millis () < 3000)
    ;

  AudioMemory (8);

  sgtl5000.enable ();
  sgtl5000.volume (0.5f);

  Serial.println ("setup done");

  for (int i = 1; i <= 3; i++)
    do_sweep (i);

  Serial.println ("Done");
}

void loop (void)
{
}
Clone this wiki locally