Skip to content

Instantly share code, notes, and snippets.

@donghee
Forked from TomWhitwell/clouds.cc
Created September 11, 2016 15:38
Show Gist options
  • Save donghee/785238a8974094fb31b43f8ac6615159 to your computer and use it in GitHub Desktop.
Save donghee/785238a8974094fb31b43f8ac6615159 to your computer and use it in GitHub Desktop.
Clouds firmware - simple 1 second audio delay
// Copyright 2014 Olivier Gillet.
//
// Author: Olivier Gillet (ol.gillet@gmail.com)
//
// 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) {
// 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
delayLine[(long)recordHead % delayLength] = input->l;
recordHead++;
// Read the Left output from the delay line
output->l = delayLine[(long)playHead % delayLength];
playHead++;
output++;
input++;
}
}
void Init() {
// start the timer that calls SysTick_Handler (see system.h)
System sys;
sys.Init(true);
sys.StartTimers();
// 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);
codec.Start(32, &FillBuffer);
// configure and initialize the internal ADC (for CV and pots) and
// LED driver.
leds.Init();
adc.Init();
}
// 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