Last active
June 1, 2016 10:32
-
-
Save TomWhitwell/9bbc6b844e1caf9e105081e35f84b0fb to your computer and use it in GitHub Desktop.
1 second delay with feedback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2014 Olivier Gillet. | |
// | |
// Author: Olivier Gillet ([email protected]) | |
// | |
// 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. | |
// | |
// See http://creativecommons.org/licenses/MIT/ for more information. | |
// This is the 'Hello World version of Clouds Firmware | |
// Modified by Matthias Puech | |
#include "clouds/drivers/codec.h" | |
#include "clouds/drivers/system.h" | |
#include "clouds/drivers/version.h" | |
#include "clouds/drivers/leds.h" | |
#include "clouds/drivers/switches.h" | |
#include "clouds/drivers/adc.h" | |
using namespace clouds; | |
Leds leds; | |
Switches switches; | |
Adc adc; | |
int counter; | |
// create an array with space for one second of audio | |
uint32_t delayLength = 48000; | |
int16_t delayLine[48000]; | |
// create variables to store record and playback positions | |
// Use floats for these because we might want to have them | |
// move faster or slower. | |
double recordHead = 0; | |
double playHead = 1; // Start one sample ahead of the record head | |
// Default interrupt handlers. | |
extern "C" { | |
void NMI_Handler() { } | |
void HardFault_Handler() { while (1); } | |
void MemManage_Handler() { while (1); } | |
void BusFault_Handler() { while (1); } | |
void UsageFault_Handler() { while (1); } | |
void SVC_Handler() { } | |
void DebugMon_Handler() { } | |
void PendSV_Handler() { } | |
// called every 1ms | |
void SysTick_Handler() { | |
counter++; | |
// a little LED animation for fun | |
for(int i=0; i<4; i++) { | |
leds.set_status(i, (counter+i*64) & 255, (counter+i*64+128) & 255); | |
} | |
leds.Write(); | |
} | |
} | |
// called every time the codec needs one buffer of data | |
void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t n) { | |
// Get the value of the Position pot/CV and tell the system to | |
// read the pots next time around | |
float pos = adc.float_value(ADC_POSITION_POTENTIOMETER_CV); | |
adc.Convert(); | |
// for each sample | |
while (n--) { | |
// Write the Left input to the delay line | |
// | |
// Using the modulo "% delayLength" means it writes in a loop | |
// starting again at the beginning when it gets to the end | |
// | |
// Use (long) to convert the float into an integer, you can't | |
// use a float to set the index on an array | |
//Add feedback by adding the output multiplied by the Position knob. Divide that by 2, to avoid overloading the output. | |
delayLine[(long)recordHead % delayLength] = (input->l + (delayLine[(long)playHead % delayLength] * pos)) /2; | |
recordHead++; | |
// Read the Left output from the delay line | |
output->l = delayLine[(long)playHead % delayLength]; | |
playHead++; | |
output++; | |
input++; | |
} | |
} | |
void Init() { | |
// configure and initialize the internal ADC (for CV and pots) and | |
// LED driver. | |
leds.Init(); | |
adc.Init(); | |
// start the timer that calls SysTick_Handler (see system.h) | |
System sys; | |
sys.Init(true); | |
// later versions of Clouds have slight hardware difference; this is | |
// to differentiate them | |
Version version; | |
version.Init(); | |
bool master = !version.revised(); | |
// initialize the codec at 48kHz, with a buffer size of 32 samples | |
Codec codec; | |
codec.Init(master, 48000); | |
// start the system | |
sys.StartTimers(); | |
codec.Start(32, &FillBuffer); | |
} | |
// this is the function that is called on startup | |
int main(void) { | |
// initialize and configure all the devices | |
Init(); | |
// and then do nothing (the timers will trigger the appropriate functions) | |
while (1) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment