Skip to content

Commit

Permalink
DM: interrupts fix and update synthesis examples for SAMD51
Browse files Browse the repository at this point in the history
  • Loading branch information
deanm1278 committed Sep 24, 2018
1 parent 8f43258 commit 4a8bc71
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 25 deletions.
1 change: 1 addition & 0 deletions Audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
// at the same time, because AudioNoInterrupts() prevents any updates
// while you make changes.
//

#define AudioNoInterrupts() (NVIC_DISABLE_IRQ(IRQ_SOFTWARE))
#define AudioInterrupts() (NVIC_ENABLE_IRQ(IRQ_SOFTWARE))

Expand Down
9 changes: 0 additions & 9 deletions AudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,24 +232,15 @@ bool AudioStream::update_scheduled = false;
bool AudioStream::update_setup(void)
{
if (update_scheduled) return false;
#if defined(ARDUINO_ARCH_SAMD)
NVIC_SetPriority(SOFTWARE_IRQn, 208);
NVIC_EnableIRQ(SOFTWARE_IRQn);
#else
NVIC_SET_PRIORITY(IRQ_SOFTWARE, 208); // 255 = lowest priority
NVIC_ENABLE_IRQ(IRQ_SOFTWARE);
#endif
update_scheduled = true;
return true;
}

void AudioStream::update_stop(void)
{
#if defined(ARDUINO_ARCH_SAMD)
NVIC_DisableIRQ(SOFTWARE_IRQn);
#else
NVIC_DISABLE_IRQ(IRQ_SOFTWARE);
#endif
update_scheduled = false;
}

Expand Down
5 changes: 5 additions & 0 deletions AudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
#if defined(__SAMD51__)
#include "sam.h"

#define NVIC_ENABLE_IRQ NVIC_EnableIRQ
#define NVIC_DISABLE_IRQ NVIC_DisableIRQ
#define IRQ_SOFTWARE SOFTWARE_IRQn
#define NVIC_SET_PRIORITY NVIC_SetPriority

//we're gonna commandeer this kinda useless IQRn and use it as a software interrupt
#define SOFTWARE_IRQn EVSYS_4_IRQn
#define SOFTWARE_Handler EVSYS_4_Handler
Expand Down
29 changes: 25 additions & 4 deletions examples/Synthesis/Guitar/Guitar.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <Audio.h>
#include <Wire.h>
#ifndef __SAMD51__
#include <SD.h>
#include <SPI.h>

#include <SerialFlash.h>
#endif
#include "chords.h"

// Special thanks to Matthew Rahtz - http://amid.fish/karplus-strong/
Expand All @@ -14,16 +17,24 @@ AudioSynthKarplusStrong string5;
AudioSynthKarplusStrong string6;
AudioMixer4 mixer1;
AudioMixer4 mixer2;
AudioOutputAnalogStereo dacs;
#if defined(__SAMD51__)
AudioOutputAnalogStereo audioOut;
#else
AudioOutputI2S audioOut;
#endif
AudioConnection patchCord1(string1, 0, mixer1, 0);
AudioConnection patchCord2(string2, 0, mixer1, 1);
AudioConnection patchCord3(string3, 0, mixer1, 2);
AudioConnection patchCord4(string4, 0, mixer1, 3);
AudioConnection patchCord5(mixer1, 0, mixer2, 0);
AudioConnection patchCord6(string5, 0, mixer2, 1);
AudioConnection patchCord7(string6, 0, mixer2, 2);
AudioConnection patchCord8(mixer2, 0, dacs, 0);
AudioConnection patchCord9(mixer2, 0, dacs, 1);
AudioConnection patchCord8(mixer2, 0, audioOut, 0);
AudioConnection patchCord9(mixer2, 0, audioOut, 1);

#ifndef __SAMD51__
AudioControlSGTL5000 sgtl5000_1;
#endif

const int finger_delay = 5;
const int hand_delay = 220;
Expand All @@ -32,6 +43,10 @@ int chordnum=0;

void setup() {
AudioMemory(15);
#ifndef __SAMD51__
sgtl5000_1.enable();
sgtl5000_1.volume(0.6);
#endif
mixer1.gain(0, 0.15);
mixer1.gain(1, 0.15);
mixer1.gain(2, 0.15);
Expand Down Expand Up @@ -95,6 +110,11 @@ void loop() {
delay(hand_delay);
strum_dn(chord, 0.7);
delay(hand_delay);
#ifndef __SAMD51__
Serial.print("Max CPU Usage = ");
Serial.print(AudioProcessorUsageMax(), 1);
Serial.println("%");
#endif
}

void strum_up(const float *chord, float velocity)
Expand Down Expand Up @@ -130,3 +150,4 @@ void strum_dn(const float *chord, float velocity)
}



10 changes: 10 additions & 0 deletions examples/Synthesis/PlaySynthMusic/PlaySynthMusic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

#include <Audio.h>
#include <Wire.h>
#ifndef __SAMD51__
#include <SD.h>
#include <SPI.h>
#include <SerialFlash.h>
#endif

#include "PlaySynthMusic.h"

Expand Down Expand Up @@ -107,7 +109,11 @@ AudioConnection patchCord32(env15, 0, mixer4, 3);
// Now create 2 mixers for the main output
AudioMixer4 mixerLeft;
AudioMixer4 mixerRight;
#ifndef __SAMD51__
AudioOutputI2S audioOut;
#else
AudioOutputAnalogStereo audioOut;
#endif

// Mix all channels to both the outputs
AudioConnection patchCord33(mixer1, 0, mixerLeft, 0);
Expand All @@ -121,7 +127,9 @@ AudioConnection patchCord40(mixer4, 0, mixerRight, 3);
AudioConnection patchCord41(mixerLeft, 0, audioOut, 0);
AudioConnection patchCord42(mixerRight, 0, audioOut, 1);

#ifndef __SAMD51__
AudioControlSGTL5000 codec;
#endif

// Initial value of the volume control
int volume = 50;
Expand All @@ -142,8 +150,10 @@ void setup()
// so give it 12 just to be sure.
AudioMemory(18);

#ifndef __SAMD51__
codec.enable();
codec.volume(0.45);
#endif

// reduce the gain on some channels, so half of the channels
// are "positioned" to the left, half to the right, but all
Expand Down
24 changes: 15 additions & 9 deletions examples/Synthesis/SimpleDrum/SimpleDrum.ino
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

#include <synth_simple_drum.h>

#include <Audio.h>
#include <Wire.h>
#ifndef __SAMD51__
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#endif

// GUItool: begin automatically generated code
AudioSynthSimpleDrum drum2; //xy=399,244
AudioSynthSimpleDrum drum3; //xy=424,310
AudioSynthSimpleDrum drum1; //xy=431,197
AudioSynthSimpleDrum drum4; //xy=464,374
AudioMixer4 mixer1; //xy=737,265
AudioOutputI2S i2s1; //xy=979,214
#ifndef __SAMD51__
AudioOutputI2S audioOut;
#else
AudioOutputAnalogStereo audioOut;
#endif
AudioConnection patchCord1(drum2, 0, mixer1, 1);
AudioConnection patchCord2(drum3, 0, mixer1, 2);
AudioConnection patchCord3(drum1, 0, mixer1, 0);
AudioConnection patchCord4(drum4, 0, mixer1, 3);
AudioConnection patchCord5(mixer1, 0, i2s1, 0);
AudioConnection patchCord6(mixer1, 0, i2s1, 1);
AudioConnection patchCord5(mixer1, 0, audioOut, 0);
AudioConnection patchCord6(mixer1, 0, audioOut, 1);
#ifndef __SAMD51__
AudioControlSGTL5000 sgtl5000_1; //xy=930,518
#endif
// GUItool: end automatically generated code

static uint32_t next;
Expand Down Expand Up @@ -62,8 +64,10 @@ void setup() {
drum4.secondMix(0.0);
drum4.pitchMod(0.0);

#ifndef __SAMD51__
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
#endif

AudioInterrupts();

Expand Down Expand Up @@ -96,8 +100,10 @@ void loop() {
num++;

Serial.print("Diagnostics: ");
#ifndef __SAMD51__
Serial.print(AudioProcessorUsageMax());
Serial.print(" ");
#endif
Serial.println(AudioMemoryUsageMax());
AudioProcessorUsageMaxReset();
}
Expand Down
17 changes: 14 additions & 3 deletions examples/Synthesis/pulseWidth/pulseWidth.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@

#include <Audio.h>
#include <Wire.h>
#ifndef __SAMD51__
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#endif

// GUItool: begin automatically generated code
AudioSynthWaveform waveform1; //xy=188,240
AudioEffectEnvelope envelope1; //xy=371,237
AudioOutputI2S i2s1; //xy=565,241

#ifndef __SAMD51__
AudioOutputI2S audioOut; //xy=565,241
#else
AudioOutputAnalogStereo audioOut;
#endif
AudioConnection patchCord1(waveform1, envelope1);
AudioConnection patchCord2(envelope1, 0, i2s1, 0);
AudioConnection patchCord3(envelope1, 0, i2s1, 1);
AudioConnection patchCord2(envelope1, 0, audioOut, 0);
AudioConnection patchCord3(envelope1, 0, audioOut, 1);
#ifndef __SAMD51__
AudioControlSGTL5000 audioShield; //xy=586,175
#endif
// GUItool: end automatically generated code


Expand All @@ -23,8 +32,10 @@ void setup(void)

// Set up
AudioMemory(8);
#ifndef __SAMD51__
audioShield.enable();
audioShield.volume(0.45);
#endif

waveform1.pulseWidth(0.5);
waveform1.begin(0.4, 220, WAVEFORM_PULSE);
Expand Down

0 comments on commit 4a8bc71

Please sign in to comment.