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 "PluckedGuitar.h"
elleo 0:ce6724069f0a 23 #include "mbed.h"
elleo 0:ce6724069f0a 24
elleo 0:ce6724069f0a 25 PluckedGuitar::PluckedGuitar() :
elleo 0:ce6724069f0a 26 Instrument::Instrument() {
elleo 0:ce6724069f0a 27 _decay = 1.1;
elleo 0:ce6724069f0a 28 }
elleo 0:ce6724069f0a 29
elleo 0:ce6724069f0a 30 void PluckedGuitar::note(Note note) {
elleo 0:ce6724069f0a 31 _num_samples = _samplerate / note.frequency;
elleo 0:ce6724069f0a 32 _duration = ((_samplerate * 60) / _bpm) * note.duration;
elleo 0:ce6724069f0a 33 _samples = (float *) malloc(_num_samples * sizeof(float));
elleo 0:ce6724069f0a 34 int i;
elleo 0:ce6724069f0a 35 _samples_played = 0;
elleo 0:ce6724069f0a 36 _position = 0;
elleo 0:ce6724069f0a 37
elleo 0:ce6724069f0a 38 for (i = 0; i < _num_samples; i++) {
elleo 0:ce6724069f0a 39 _samples[i] = abs(sin((float) rand()));
elleo 0:ce6724069f0a 40 }
elleo 0:ce6724069f0a 41 }
elleo 0:ce6724069f0a 42
elleo 0:ce6724069f0a 43 float PluckedGuitar::proc_sample() {
elleo 0:ce6724069f0a 44 DigitalOut l(LED2);
elleo 0:ce6724069f0a 45 if (_position == _num_samples) {
elleo 0:ce6724069f0a 46 _position = 0;
elleo 0:ce6724069f0a 47 }
elleo 0:ce6724069f0a 48 if (_samples_played > _duration) {
elleo 0:ce6724069f0a 49 l = 1;
elleo 0:ce6724069f0a 50 return -1;
elleo 0:ce6724069f0a 51 }
elleo 0:ce6724069f0a 52 if(_position == 0) {
elleo 0:ce6724069f0a 53 _samples[0] = _samples[_num_samples - 1] + (_samples[0] - _samples[_num_samples - 1]) / _decay;
elleo 0:ce6724069f0a 54 } else {
elleo 0:ce6724069f0a 55 _samples[_position] = _samples[_position-1] + (_samples[_position] - _samples[_position - 1]) / _decay;
elleo 0:ce6724069f0a 56 }
elleo 0:ce6724069f0a 57 float sample = _samples[_position];
elleo 0:ce6724069f0a 58
elleo 0:ce6724069f0a 59 _samples_played++;
elleo 0:ce6724069f0a 60 _position++;
elleo 0:ce6724069f0a 61 return sample;
elleo 0:ce6724069f0a 62 }