Last active
August 1, 2016 05:52
-
-
Save Technicus/ce5df487d5eb4af90bb92cdf8805c3c0 to your computer and use it in GitHub Desktop.
Laser-Harp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FluidSynth_Test_02_GPIO_INPUT_SINGLE_NOTE.c | |
// http://fluidsynth.sourceforge.net/api/ | |
// http://www.i-programmer.info/programming/hardware/8744-exploring-edison-mraa-gpio.html?start=3 | |
// http://www.malinov.com/Home/sergey-s-blog/inteledison-simplei2saudiosetup | |
// Compile with this | |
// gcc FluidSynth_Test_02_GPIO_INPUT_SINGLE_NOTE.c -o FluidSynth_Test_02_GPIO_INPUT_SINGLE_NOTE -lmraa -L/usr/local/lib -lfluidsynth | |
#include "mraa.h" | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <time.h> | |
#include <fluidsynth.h> | |
#define INPUT_PIN 4 | |
#define BILLION 1000000000L | |
struct timespec btime, etime;__time_t i; | |
// MRAA_GPIO_EDGE_NONE = 0, | |
// MRAA_GPIO_EDGE_BOTH = 1, | |
// MRAA_GPIO_EDGE_RISING = 2, | |
// MRAA_GPIO_EDGE_FALLING = 3 | |
fluid_synth_t* synth; | |
fluid_audio_driver_t* adriver; | |
// function prototypes | |
void switchPressed(void* pin); | |
void createsynth(); | |
void deletesynth(); | |
void loadsoundfont(); | |
int main(int argc, char** argv) { | |
mraa_init(); | |
mraa_gpio_context inputPin = mraa_gpio_init(INPUT_PIN); | |
mraa_gpio_dir(inputPin, MRAA_GPIO_IN); | |
mraa_gpio_isr(inputPin, MRAA_GPIO_EDGE_BOTH, &switchPressed, inputPin); | |
createsynth(); | |
loadsoundfont(); | |
for (;;) {} | |
deletesynth(); | |
return MRAA_SUCCESS; | |
} | |
void switchPressed(void* pin) { | |
struct timespec ttime; | |
clock_gettime(CLOCK_REALTIME, &ttime); | |
usleep(500); | |
int s = mraa_gpio_read((mraa_gpio_context) pin); | |
if (s == 0) { | |
btime = ttime; | |
/* Release the note on key 60 */ | |
fluid_synth_noteoff(synth, 0, 60); | |
printf("time = %f (s)\n", btime); | |
printf("fluid_synth_noteoff(synth, 0, 60);\t/* Release the note on key 60 */\n"); | |
} | |
else { | |
double nseconds = (double)((ttime.tv_sec - btime.tv_sec) * BILLION) + (double) (ttime.tv_nsec - btime.tv_nsec) ; | |
printf("time = %f (s)\n", nseconds / BILLION); | |
/* Play a note on key 60 with velocity 100 on MIDI channel 0 */ | |
printf("fluid_synth_noteon(synth, 0, 60, 100);\t/* Play a note on key 60 with velocity 100 on MIDI channel 0 */\n"); | |
fluid_synth_noteon(synth, 0, 60, 100); | |
} | |
} | |
void createsynth() { | |
fluid_settings_t* settings; | |
settings = new_fluid_settings(); | |
fluid_settings_setstr(settings, "synth.reverb.active", "yes"); | |
fluid_settings_setstr(settings, "synth.chorus.active", "no"); | |
fluid_settings_setint(settings, "audio.period-size", 384); | |
fluid_settings_setnum(settings, "synth.sample-rate", 48000); | |
synth = new_fluid_synth(settings); | |
adriver = new_fluid_audio_driver(settings, synth); | |
} | |
void deletesynth() { | |
delete_fluid_audio_driver(adriver); | |
delete_fluid_synth(synth); | |
} | |
void loadsoundfont() { | |
fluid_synth_sfload(synth, "/home/root/audio/audio_files/FluidR3_GM2-2.SF2", 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment