![](/media/cache/profiles/ed3d2c21991e3bef5e069713af9fa6ca.50x50_q85.jpg)
A simple string synthesizer implementing the Karplus-Strong algorithm. Licensed under the GNU LGPL.
Revision 0:ce6724069f0a, committed 2013-01-09
- Comitter:
- elleo
- Date:
- Wed Jan 09 22:46:12 2013 +0000
- Commit message:
- Migration to new repository
Changed in this revision
diff -r 000000000000 -r ce6724069f0a Instrument.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Instrument.cpp Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,44 @@ +/* + * libmbed-synth An audio synthesis library capable of running + * alongside other activities. + * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. +*/ + +#include "Instrument.h" +#include "mbed.h" + +Instrument::Instrument() { + _position = 0; + _samples = NULL; + _num_samples = 0; + _samplerate = 11000; + _bpm = 120; + _samples_played = 0; +} + +void Instrument::set_samplerate(int samplerate) { + _samplerate = samplerate; +} + +void Instrument::set_bpm(int bpm) { + _bpm = bpm; +} + +float Instrument::proc_sample() { + return 0; +} \ No newline at end of file
diff -r 000000000000 -r ce6724069f0a Instrument.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Instrument.h Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,46 @@ +/* + * libmbed-synth An audio synthesis library capable of running + * alongside other activities. + * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. +*/ + +#ifndef MBED_INSTRUMENT_H +#define MBED_INSTRUMENT_H + +#include "Note.h" + +class Instrument { + + public: + Instrument(); + void note(Note note); + void set_samplerate(int samplerate); + void set_bpm(int bpm); + float proc_sample(); + + protected: + float *_samples; + int _num_samples; + int _position; + int _samplerate; + int _bpm; + int _duration; + int _samples_played; +}; + +#endif \ No newline at end of file
diff -r 000000000000 -r ce6724069f0a Note.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Note.h Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,30 @@ +/* + * libmbed-synth An audio synthesis library capable of running + * alongside other activities. + * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. +*/ + +#ifndef MBED_NOTE_H +#define MBED_NOTE_H + +typedef struct _Note { + float frequency; + float duration; +} Note; + +#endif \ No newline at end of file
diff -r 000000000000 -r ce6724069f0a Notes.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Notes.h Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,120 @@ +/* + * libmbed-synth An audio synthesis library capable of running + * alongside other activities. + * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. +*/ + +#ifndef MBED_NOTES_H +#define MBED_NOTES_H + +#define NOTE_G0 24.50 +#define NOTE_A_FLAT0 25.96 +#define NOTE_A0 27.50 +#define NOTE_B_FLAT0 29.14 +#define NOTE_B0 30.87 +#define NOTE_C1 32.70 +#define NOTE_C_SHARP1 34.65 +#define NOTE_D1 36.71 +#define NOTE_E_FLAT1 38.89 +#define NOTE_E1 41.20 +#define NOTE_F1 43.65 +#define NOTE_F_SHARP1 46.25 +#define NOTE_G1 49.00 +#define NOTE_A_FLAT1 51.91 +#define NOTE_A1 55.00 +#define NOTE_B_FLAT1 58.27 +#define NOTE_B1 61.74 +#define NOTE_C2 65.41 +#define NOTE_C_SHARP2 69.30 +#define NOTE_D2 73.42 +#define NOTE_E_FLAT2 77.78 +#define NOTE_E2 82.41 +#define NOTE_F2 87.31 +#define NOTE_F_SHARP2 92.50 +#define NOTE_G2 98.00 +#define NOTE_A_FLAT2 103.83 +#define NOTE_A2 110.00 +#define NOTE_B_FLAT2 116.54 +#define NOTE_B2 123.47 +#define NOTE_C3 130.81 +#define NOTE_C_SHARP3 138.59 +#define NOTE_D3 146.83 +#define NOTE_E_FLAT3 155.56 +#define NOTE_E3 164.81 +#define NOTE_F3 174.61 +#define NOTE_F_SHARP3 185.00 +#define NOTE_G3 196.00 +#define NOTE_A_FLAT3 207.65 +#define NOTE_A3 220.00 +#define NOTE_B_FLAT3 233.08 +#define NOTE_B3 246.94 +#define NOTE_C4 261.63 +#define NOTE_C_SHARP4 277.18 +#define NOTE_D4 293.66 +#define NOTE_E_FLAT4 311.13 +#define NOTE_E4 329.63 +#define NOTE_F4 349.23 +#define NOTE_F_SHARP4 369.99 +#define NOTE_G4 392.00 +#define NOTE_A_FLAT4 415.30 +#define NOTE_A4 440.00 +#define NOTE_B_FLAT4 466.16 +#define NOTE_B4 493.88 +#define NOTE_C5 523.25 +#define NOTE_C_SHARP5 554.37 +#define NOTE_D5 587.33 +#define NOTE_E_FLAT5 622.25 +#define NOTE_E5 659.26 +#define NOTE_F5 698.46 +#define NOTE_F_SHARP5 739.99 +#define NOTE_G5 783.99 +#define NOTE_A_FLAT5 830.61 +#define NOTE_A5 880.00 +#define NOTE_B_FLAT5 932.33 +#define NOTE_B5 987.77 +#define NOTE_C6 1046.50 +#define NOTE_C_SHARP6 1108.73 +#define NOTE_D6 1174.66 +#define NOTE_E_FLAT6 1244.51 +#define NOTE_E6 1318.51 +#define NOTE_F6 1396.91 +#define NOTE_F_SHARP6 1479.98 +#define NOTE_G6 1567.98 +#define NOTE_A_FLAT6 1661.22 +#define NOTE_A6 1760.00 +#define NOTE_B_FLAT6 1864.66 +#define NOTE_B6 1975.53 +#define NOTE_C7 2093.00 +#define NOTE_C_SHARP7 2217.46 +#define NOTE_D7 2349.32 +#define NOTE_E_FLAT7 2489.02 +#define NOTE_E7 2637.02 +#define NOTE_F7 2793.83 +#define NOTE_F_SHARP7 2959.96 +#define NOTE_G7 3135.96 +#define NOTE_A_FLAT7 3322.44 +#define NOTE_A7 3520.00 +#define NOTE_B_FLAT7 3729.31 +#define NOTE_B7 3951.07 +#define NOTE_C8 4186.01 +#define NOTE_C_SHARP8 4434.92 +#define NOTE_D8 4698.64 +#define NOTE_E_FLAT8 4978.03 + + +#endif \ No newline at end of file
diff -r 000000000000 -r ce6724069f0a PluckedGuitar.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PluckedGuitar.cpp Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,62 @@ +/* + * libmbed-synth An audio synthesis library capable of running + * alongside other activities. + * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. +*/ + +#include "PluckedGuitar.h" +#include "mbed.h" + +PluckedGuitar::PluckedGuitar() : + Instrument::Instrument() { + _decay = 1.1; +} + +void PluckedGuitar::note(Note note) { + _num_samples = _samplerate / note.frequency; + _duration = ((_samplerate * 60) / _bpm) * note.duration; + _samples = (float *) malloc(_num_samples * sizeof(float)); + int i; + _samples_played = 0; + _position = 0; + + for (i = 0; i < _num_samples; i++) { + _samples[i] = abs(sin((float) rand())); + } +} + +float PluckedGuitar::proc_sample() { + DigitalOut l(LED2); + if (_position == _num_samples) { + _position = 0; + } + if (_samples_played > _duration) { + l = 1; + return -1; + } + if(_position == 0) { + _samples[0] = _samples[_num_samples - 1] + (_samples[0] - _samples[_num_samples - 1]) / _decay; + } else { + _samples[_position] = _samples[_position-1] + (_samples[_position] - _samples[_position - 1]) / _decay; + } + float sample = _samples[_position]; + + _samples_played++; + _position++; + return sample; +} \ No newline at end of file
diff -r 000000000000 -r ce6724069f0a PluckedGuitar.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PluckedGuitar.h Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,38 @@ +/* + * libmbed-synth An audio synthesis library capable of running + * alongside other activities. + * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. +*/ + +#ifndef MBED_PLUCKEDGUITAR_H +#define MBED_PLUCKEDGUITAR_H + +#include "Instrument.h" + +class PluckedGuitar : public Instrument { + + public: + PluckedGuitar(); + void note(Note note); + float proc_sample(); + + protected: + float _decay; +}; + +#endif
diff -r 000000000000 -r ce6724069f0a Synth.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Synth.cpp Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,105 @@ +/* + * libmbed-synth An audio synthesis library capable of running + * alongside other activities. + * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. +*/ + +#include "Synth.h" + +Synth::Synth(PinName aout) : + _aout(aout) { + _repeat = false; + _samplerate = 22000; + _samples = NULL; + _sample_size = 0; + _position = 0; + _bpm = 120; + _notes = NULL; + _num_notes = 0; +} + +void Synth::play() { + _ticker.attach(this, &Synth::_proc_sample, 1.0 / _samplerate); + + DigitalOut l(LED1); + l = 1; +} + +void Synth::pause() { + _ticker.detach(); +} + +void Synth::stop() { + _position = 0; + _ticker.detach(); +} + +void Synth::set_repeat(bool repeat) { + _repeat = repeat; +} + +void Synth::set_instrument(PluckedGuitar *instrument){ + _instrument = instrument; + _instrument->set_bpm(_bpm); + _instrument->set_samplerate(_samplerate); +} + +void Synth::set_bpm(int bpm) { + _bpm = bpm; + if(_instrument != NULL) { + _instrument->set_bpm(_bpm); + } +} + +void Synth::_proc_sample() { + if (_position == 0) { + _instrument->note(_notes[_position]); + _position++; + } + + float sample = _instrument->proc_sample(); + + if (sample == -1) { + _instrument->note(_notes[_position]); + _position++; + } else { + _aout = sample; + } + + if (sample == -1 && _position > _num_notes) { + if(_repeat) { + _position = 0; + } else { + stop(); + } + } +} + +void Synth::add_note(float frequency, float duration) { + _num_notes++; + Note n; + n.frequency = frequency; + n.duration = duration; + if (_notes == NULL) { + _notes = (Note*) malloc(sizeof(Note)); + } else { + _notes = (Note*) realloc(_notes, sizeof(Note) * _num_notes); + + } + _notes[_num_notes - 1] = n; +} \ No newline at end of file
diff -r 000000000000 -r ce6724069f0a Synth.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Synth.h Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,60 @@ +/* + * libmbed-synth An audio synthesis library capable of running + * alongside other activities. + * Copyright (C) <2009> Michael Sheldon <mike@mikeasoft.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. +*/ + +#ifndef MBED_SYNTH_H +#define MBED_SYNTH_H + +#include "mbed.h" +#include "Instrument.h" +#include "PluckedGuitar.h" +#include "Note.h" +#include "Notes.h" + +class Synth { + + public: + Synth(PinName aout); + void add_note(float frequency, float duration); + void rest(double duration); + void play(); + void pause(); + void stop(); + void set_repeat(bool repeat); + void set_instrument(PluckedGuitar *instrument); + void set_bpm(int bpm); + + protected: + AnalogOut _aout; + Ticker _ticker; + int _samplerate; + double *_samples; + int _sample_size; + bool _repeat; + int _position; + int _bpm; + PluckedGuitar *_instrument; + Note *_notes; + int _num_notes; + void _proc_sample(); +}; + + +#endif \ No newline at end of file
diff -r 000000000000 -r ce6724069f0a main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,128 @@ +#include "mbed.h" +#include "Synth.h" + +Synth s(p18); + +void fly_me_to_the_moon(Synth *s) { + //Fly me to the moon + s->add_note(NOTE_C5, 1.5); + s->add_note(NOTE_B4, 0.5); + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_G4, 0.5); + + s->add_note(NOTE_F4, 1.5); + s->add_note(NOTE_G4, 0.5); + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_C5, 0.5); + + s->add_note(NOTE_B4, 1.5); + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_G4, 0.5); + s->add_note(NOTE_F4, 0.5); + + s->add_note(NOTE_E4, 3); + + s->add_note(NOTE_A4, 1.5); + s->add_note(NOTE_G4, 0.5); + s->add_note(NOTE_F4, 0.5); + s->add_note(NOTE_E4, 0.5); + + s->add_note(NOTE_D4, 1.5); + s->add_note(NOTE_E4, 0.5); + s->add_note(NOTE_F4, 0.5); + s->add_note(NOTE_A4, 0.5); + + s->add_note(NOTE_A_FLAT4, 1.5); + s->add_note(NOTE_F4, 0.5); + s->add_note(NOTE_E4, 0.5); + s->add_note(NOTE_D4, 0.5); + + s->add_note(NOTE_C4, 2); + s->add_note(NOTE_C_SHARP4, 1); + + s->add_note(NOTE_D4, 0.5); + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_A4, 3); + s->add_note(NOTE_C5, 1); + s->add_note(NOTE_B4, 1); + + s->add_note(NOTE_G4, 5); + s->add_note(NOTE_B3, 1); + + s->add_note(NOTE_C4, 0.5); + s->add_note(NOTE_F4, 0.5); + s->add_note(NOTE_F4, 3); + s->add_note(NOTE_A4, 1); + s->add_note(NOTE_G4, 1); + s->add_note(NOTE_F4, 1); + s->add_note(NOTE_E4, 5); + + s->add_note(NOTE_C5, 1.5); + s->add_note(NOTE_B4, 0.5); + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_G4, 0.5); + + s->add_note(NOTE_F4, 1.5); + s->add_note(NOTE_G4, 0.5); + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_C5, 0.5); + + s->add_note(NOTE_B4, 1.5); + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_G4, 0.5); + s->add_note(NOTE_F4, 0.5); + + s->add_note(NOTE_E4, 3); + + s->add_note(NOTE_A4, 1.5); + s->add_note(NOTE_G4, 0.5); + s->add_note(NOTE_F4, 0.5); + s->add_note(NOTE_E4, 0.5); + + s->add_note(NOTE_D4, 1.5); + s->add_note(NOTE_E4, 0.5); + s->add_note(NOTE_F4, 0.5); + s->add_note(NOTE_G4, 0.5); + + s->add_note(NOTE_A_FLAT4, 1.5); + s->add_note(NOTE_F4, 0.5); + s->add_note(NOTE_E4, 0.5); + s->add_note(NOTE_D4, 0.5); + + s->add_note(NOTE_C4, 2); + s->add_note(NOTE_C_SHARP4, 1); + + s->add_note(NOTE_D4, 0.5); + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_A4, 3); + s->add_note(NOTE_C5, 1); + s->add_note(NOTE_B4, 1); + + s->add_note(NOTE_G4, 5); + s->add_note(NOTE_A_FLAT4, 1); + + s->add_note(NOTE_A4, 0.5); + s->add_note(NOTE_C4, 0.5); + s->add_note(NOTE_C4, 3); + s->add_note(NOTE_C4, 1); + s->add_note(NOTE_D4, 1); + + s->add_note(NOTE_C4, 3); +} + +int main() { + PluckedGuitar *g = new PluckedGuitar(); + s.set_bpm(100); + s.set_instrument(g); + fly_me_to_the_moon(&s); + s.set_repeat(true); + s.play(); + DigitalOut led1(LED1); + DigitalOut led4(LED4); + led1 = 1; + while(1) { + led1 = !led1; + led4 = !led4; + wait(1); + } +} \ No newline at end of file
diff -r 000000000000 -r ce6724069f0a mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jan 09 22:46:12 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/32af5db564d4 \ No newline at end of file