A simple string synthesizer implementing the Karplus-Strong algorithm. Licensed under the GNU LGPL.

Dependencies:   mbed

Committer:
elleo
Date:
Wed Jan 09 22:46:12 2013 +0000
Revision:
0:ce6724069f0a
Migration to new repository

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elleo 0:ce6724069f0a 1 /*
elleo 0:ce6724069f0a 2 * libmbed-synth An audio synthesis library capable of running
elleo 0:ce6724069f0a 3 * alongside other activities.
elleo 0:ce6724069f0a 4 * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com>
elleo 0:ce6724069f0a 5 *
elleo 0:ce6724069f0a 6 * This library is free software; you can redistribute it and/or
elleo 0:ce6724069f0a 7 * modify it under the terms of the GNU Library General Public
elleo 0:ce6724069f0a 8 * License as published by the Free Software Foundation; either
elleo 0:ce6724069f0a 9 * version 2 of the License, or (at your option) any later version.
elleo 0:ce6724069f0a 10 *
elleo 0:ce6724069f0a 11 * This library is distributed in the hope that it will be useful,
elleo 0:ce6724069f0a 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
elleo 0:ce6724069f0a 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
elleo 0:ce6724069f0a 14 * Library General Public License for more details.
elleo 0:ce6724069f0a 15 *
elleo 0:ce6724069f0a 16 * You should have received a copy of the GNU Library General Public
elleo 0:ce6724069f0a 17 * License along with this library; if not, write to the
elleo 0:ce6724069f0a 18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
elleo 0:ce6724069f0a 19 * Boston, MA 02111-1307, USA.
elleo 0:ce6724069f0a 20 */
elleo 0:ce6724069f0a 21
elleo 0:ce6724069f0a 22 #include "Synth.h"
elleo 0:ce6724069f0a 23
elleo 0:ce6724069f0a 24 Synth::Synth(PinName aout) :
elleo 0:ce6724069f0a 25 _aout(aout) {
elleo 0:ce6724069f0a 26 _repeat = false;
elleo 0:ce6724069f0a 27 _samplerate = 22000;
elleo 0:ce6724069f0a 28 _samples = NULL;
elleo 0:ce6724069f0a 29 _sample_size = 0;
elleo 0:ce6724069f0a 30 _position = 0;
elleo 0:ce6724069f0a 31 _bpm = 120;
elleo 0:ce6724069f0a 32 _notes = NULL;
elleo 0:ce6724069f0a 33 _num_notes = 0;
elleo 0:ce6724069f0a 34 }
elleo 0:ce6724069f0a 35
elleo 0:ce6724069f0a 36 void Synth::play() {
elleo 0:ce6724069f0a 37 _ticker.attach(this, &Synth::_proc_sample, 1.0 / _samplerate);
elleo 0:ce6724069f0a 38
elleo 0:ce6724069f0a 39 DigitalOut l(LED1);
elleo 0:ce6724069f0a 40 l = 1;
elleo 0:ce6724069f0a 41 }
elleo 0:ce6724069f0a 42
elleo 0:ce6724069f0a 43 void Synth::pause() {
elleo 0:ce6724069f0a 44 _ticker.detach();
elleo 0:ce6724069f0a 45 }
elleo 0:ce6724069f0a 46
elleo 0:ce6724069f0a 47 void Synth::stop() {
elleo 0:ce6724069f0a 48 _position = 0;
elleo 0:ce6724069f0a 49 _ticker.detach();
elleo 0:ce6724069f0a 50 }
elleo 0:ce6724069f0a 51
elleo 0:ce6724069f0a 52 void Synth::set_repeat(bool repeat) {
elleo 0:ce6724069f0a 53 _repeat = repeat;
elleo 0:ce6724069f0a 54 }
elleo 0:ce6724069f0a 55
elleo 0:ce6724069f0a 56 void Synth::set_instrument(PluckedGuitar *instrument){
elleo 0:ce6724069f0a 57 _instrument = instrument;
elleo 0:ce6724069f0a 58 _instrument->set_bpm(_bpm);
elleo 0:ce6724069f0a 59 _instrument->set_samplerate(_samplerate);
elleo 0:ce6724069f0a 60 }
elleo 0:ce6724069f0a 61
elleo 0:ce6724069f0a 62 void Synth::set_bpm(int bpm) {
elleo 0:ce6724069f0a 63 _bpm = bpm;
elleo 0:ce6724069f0a 64 if(_instrument != NULL) {
elleo 0:ce6724069f0a 65 _instrument->set_bpm(_bpm);
elleo 0:ce6724069f0a 66 }
elleo 0:ce6724069f0a 67 }
elleo 0:ce6724069f0a 68
elleo 0:ce6724069f0a 69 void Synth::_proc_sample() {
elleo 0:ce6724069f0a 70 if (_position == 0) {
elleo 0:ce6724069f0a 71 _instrument->note(_notes[_position]);
elleo 0:ce6724069f0a 72 _position++;
elleo 0:ce6724069f0a 73 }
elleo 0:ce6724069f0a 74
elleo 0:ce6724069f0a 75 float sample = _instrument->proc_sample();
elleo 0:ce6724069f0a 76
elleo 0:ce6724069f0a 77 if (sample == -1) {
elleo 0:ce6724069f0a 78 _instrument->note(_notes[_position]);
elleo 0:ce6724069f0a 79 _position++;
elleo 0:ce6724069f0a 80 } else {
elleo 0:ce6724069f0a 81 _aout = sample;
elleo 0:ce6724069f0a 82 }
elleo 0:ce6724069f0a 83
elleo 0:ce6724069f0a 84 if (sample == -1 && _position > _num_notes) {
elleo 0:ce6724069f0a 85 if(_repeat) {
elleo 0:ce6724069f0a 86 _position = 0;
elleo 0:ce6724069f0a 87 } else {
elleo 0:ce6724069f0a 88 stop();
elleo 0:ce6724069f0a 89 }
elleo 0:ce6724069f0a 90 }
elleo 0:ce6724069f0a 91 }
elleo 0:ce6724069f0a 92
elleo 0:ce6724069f0a 93 void Synth::add_note(float frequency, float duration) {
elleo 0:ce6724069f0a 94 _num_notes++;
elleo 0:ce6724069f0a 95 Note n;
elleo 0:ce6724069f0a 96 n.frequency = frequency;
elleo 0:ce6724069f0a 97 n.duration = duration;
elleo 0:ce6724069f0a 98 if (_notes == NULL) {
elleo 0:ce6724069f0a 99 _notes = (Note*) malloc(sizeof(Note));
elleo 0:ce6724069f0a 100 } else {
elleo 0:ce6724069f0a 101 _notes = (Note*) realloc(_notes, sizeof(Note) * _num_notes);
elleo 0:ce6724069f0a 102
elleo 0:ce6724069f0a 103 }
elleo 0:ce6724069f0a 104 _notes[_num_notes - 1] = n;
elleo 0:ce6724069f0a 105 }