Skip to content

Instantly share code, notes, and snippets.

@jrsa jrsa/rings.cc
Last active Nov 25, 2017

Embed
What would you like to do?
"Hello World' for Mutable Instruments Rings module
// Copyright 2015 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.
#include "rings/drivers/adc.h"
#include "rings/drivers/codec.h"
#include "rings/drivers/leds.h"
#include "rings/drivers/system.h"
#include "rings/drivers/version.h"
#include "stmlib/dsp/dsp.h"
#include "stmlib/system/system_clock.h"
using namespace rings;
using namespace stmlib;
Adc adc;
Codec codec;
Leds leds;
int counter;
// Default interrupt handlers.
extern "C" {
int __errno;
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() { }
// gets called every 0.125 ms or something
void SysTick_Handler() {
counter++;
// a little LED animation for fun
for(int i=0; i<2; i++) {
leds.set(i, ((counter + i) & 166) > 127, ((counter+i*64) & 327) > 127);
}
leds.Write();
}
}
// from rings/dsp/dsp.h:
const size_t kMaxBlockSize = 24;
static const float kSampleRate = 48000.0f;
float in[kMaxBlockSize];
float out[kMaxBlockSize];
float aux[kMaxBlockSize];
void FillBuffer(Codec::Frame* input, Codec::Frame* output, size_t size) {
float freq = adc.float_value(ADC_CHANNEL_POT_FREQUENCY);
adc.Convert();
for (size_t i = 0; i < size; ++i) {
float in_sample = static_cast<float>(input[i].r) / 32768.0f; // only right input goes to the panel on rings
float out = in_sample * freq; // freq knob attenuates audio signal
output[i].l = Clip16(static_cast<int32_t>(out * 32768.0f));
output[i].r = Clip16(static_cast<int32_t>(out * 32768.0f));
}
}
void Init() {
System sys;
Version version;
sys.Init(true);
version.Init();
leds.Init();
adc.Init();
if (!codec.Init(!version.revised(), kSampleRate)) {
while(1);
}
if (!codec.Start(kMaxBlockSize, &FillBuffer)) {
while(1);
}
codec.set_line_input_gain(22);
sys.StartTimers();
}
int main(void) {
Init();
while (1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.