Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Technicus/22781f298d7f2297c665555ebfa94ffa to your computer and use it in GitHub Desktop.
Save Technicus/22781f298d7f2297c665555ebfa94ffa to your computer and use it in GitHub Desktop.
// FluidSynth_Test_03_GPIO_INPUT_24_NOTES_NO_INTERRUPT.c
// http://www.malinov.com/Home/sergey-s-blog/inteledison-simplei2saudiosetup
// http://www.instructables.com/id/MIDI-SoundFont-synthetizer-with-Intel-Edison-and-F/?ALLSTEPS
// http://fluidsynth.sourceforge.net/api/
// http://fluidsynth.sourcearchive.com/documentation/1.0.5/example_8c-source.html
// https://musescore.org/en/handbook/soundfont#list
// http://www.i-programmer.info/programming/hardware/8744-exploring-edison-mraa-gpio.html?start=3
// Fluidsynth Library Documentation
// http://fluidsynth.sourceforge.net/api/index.html#CreatingSettings
// Soundfont Editor
// http://www.polyphone.fr/index.php?lang=en&page=download
// Compile with:
// gcc FluidSynth_Test_03_GPIO_INPUT_24_NOTES_NO_INTERRUPT.c -o FluidSynth_Test_03_GPIO_INPUT_24_NOTES_NO_INTERRUPT -lmraa -L/usr/local/lib -lfluidsynth
// Run with:
// ./FluidSynth_Test_03_GPIO_INPUT_24_NOTES_NO_INTERRUPT
//
#define APPLICATION "./FluidSynth_Test_03_GPIO_INPUT_24_NOTES_NO_INTERRUPT"
#define VERSION ".03"
#include "mraa.h"
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <fluidsynth.h>
#include "terminal_colors.h"
#define INPUT_PIN_00 4
#define INPUT_PIN_01 6
#define INPUT_PIN_02 8
#define INPUT_PIN_03 9
#define INPUT_PIN_04 10
#define INPUT_PIN_05 11
#define INPUT_PIN_06 13
#define INPUT_PIN_07 14
#define INPUT_PIN_08 15
#define INPUT_PIN_09 20
#define INPUT_PIN_10 21
#define INPUT_PIN_11 23
#define INPUT_PIN_12 24
#define INPUT_PIN_13 25
#define INPUT_PIN_14 26
#define INPUT_PIN_15 31
#define INPUT_PIN_16 32
#define INPUT_PIN_17 33
#define INPUT_PIN_18 35
#define INPUT_PIN_19 36
#define INPUT_PIN_20 48
#define INPUT_PIN_21 40
#define INPUT_PIN_22 41
#define INPUT_PIN_23 55
#define INPUT_PIN_24 53
#define INPUT_PIN_25 52
#define STATUS_PIN 54
#define NUMBEROFPINS 24
/*#define NOTE_00 71 */ #define NOTE_00 48
/*#define NOTE_01 70 */ #define NOTE_01 49
/*#define NOTE_02 69 */ #define NOTE_02 50
/*#define NOTE_03 68 */ #define NOTE_03 51
/*#define NOTE_04 67 */ #define NOTE_04 52
/*#define NOTE_05 66 */ #define NOTE_05 53
/*#define NOTE_06 65 */ #define NOTE_06 54
/*#define NOTE_07 64 */ #define NOTE_07 55
/*#define NOTE_08 63 */ #define NOTE_08 56
/*#define NOTE_09 62 */ #define NOTE_09 57
/*#define NOTE_10 61 */ #define NOTE_10 58
/*#define NOTE_11 60 */ #define NOTE_11 59
/*#define NOTE_12 59 */ #define NOTE_12 60
/*#define NOTE_13 58 */ #define NOTE_13 61
/*#define NOTE_14 57 */ #define NOTE_14 62
/*#define NOTE_15 56 */ #define NOTE_15 63
/*#define NOTE_16 55 */ #define NOTE_16 64
/*#define NOTE_17 54 */ #define NOTE_17 65
/*#define NOTE_18 53 */ #define NOTE_18 66
/*#define NOTE_19 52 */ #define NOTE_19 67
/*#define NOTE_20 51 */ #define NOTE_20 68
/*#define NOTE_21 50 */ #define NOTE_21 69
/*#define NOTE_22 49 */ #define NOTE_22 70
/*#define NOTE_23 48 */ #define NOTE_23 71
#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 initalizeAudioDevice();
void createsynth();
void deletesynth();
void loadSoundFont_Harp();
void loadSoundFont_ThrowUp();
void loadSoundFont_WillSMith();
void playNote(int note, int state);
void switchPressed_Utility_0(void* pin);
void switchPressed_Utility_1(void* pin);
int fontSet[2];
int main(int argc, char** argv){
int currentState[24];
int previousState[24];
int inputPin_Numbers[24] = {
INPUT_PIN_00,
INPUT_PIN_01,
INPUT_PIN_02,
INPUT_PIN_03,
INPUT_PIN_04,
INPUT_PIN_05,
INPUT_PIN_06,
INPUT_PIN_07,
INPUT_PIN_08,
INPUT_PIN_09,
INPUT_PIN_10,
INPUT_PIN_11,
INPUT_PIN_12,
INPUT_PIN_13,
INPUT_PIN_14,
INPUT_PIN_15,
INPUT_PIN_16,
INPUT_PIN_17,
INPUT_PIN_18,
INPUT_PIN_19,
INPUT_PIN_20,
INPUT_PIN_21,
INPUT_PIN_22,
INPUT_PIN_23
};
int statusCount = 0;
int statusState = 0;
int stateTimer = 1000;
mraa_init();
// setup the pins for polling
int pin;
mraa_gpio_context inputPin[NUMBEROFPINS];
for (pin = 0; pin <= 24; pin++) {
inputPin[pin] = mraa_gpio_init(inputPin_Numbers[pin]);
mraa_gpio_dir(inputPin[pin], MRAA_GPIO_IN);
currentState[pin] = mraa_gpio_read(inputPin[pin]);
previousState[pin] = currentState[pin];
}
mraa_gpio_context inputPin_Utility[2];
inputPin_Utility[0] = mraa_gpio_init(INPUT_PIN_24);
mraa_gpio_dir(inputPin_Utility[0], MRAA_GPIO_IN);
mraa_gpio_isr(inputPin_Utility[0], MRAA_GPIO_EDGE_BOTH, &switchPressed_Utility_0, inputPin_Utility[0]);
inputPin_Utility[1] = mraa_gpio_init(INPUT_PIN_25);
mraa_gpio_dir(inputPin_Utility[1], MRAA_GPIO_IN);
mraa_gpio_isr(inputPin_Utility[1], MRAA_GPIO_EDGE_BOTH, &switchPressed_Utility_1, inputPin_Utility[1]);
mraa_gpio_context statusPin;
statusPin = mraa_gpio_init(STATUS_PIN);
mraa_gpio_dir(statusPin, MRAA_GPIO_OUT);
mraa_gpio_write(statusPin, 1);
initalizeAudioDevice();
createsynth();
loadSoundFont_Harp();
//loadSoundFont_ThrowUp();
printf(TEXT_GREEN"\tSTARTING: " TEXT_BLUE APPLICATION"\n\n"NO_COLOR);
printf(TEXT_RED "\t\tPlay the LASER-HARP:\n\n"NO_COLOR);
for (;;) {
for (pin = 0; pin <= 24; pin++) {
if (mraa_gpio_read(inputPin[pin]) != currentState[pin]){
currentState[pin] = mraa_gpio_read(inputPin[pin]);
if ((currentState[pin] == 1) && (previousState[pin] == 0)){
// mraa_gpio_write(statusPin, 1);
playNote(pin, 1);
}
if ((currentState[pin] == 0) && (previousState[pin] == 1)){
// mraa_gpio_write(statusPin, 0);
playNote(pin, 0);
}
previousState[pin] = currentState[pin];
}
}
// if (statusState == 0 & statusCount < stateTimer){
// statusCount ++;
// mraa_gpio_write(statusPin, 1);
// // printf(TEXT_YELLOW"\t\t\tstatusCount\t" TEXT_CYAN"\t%d" TEXT_YELLOW"\t\t\tstatusState\t" TEXT_CYAN"\t%d\n" NO_COLOR, statusCount, statusState);
// }
// if (statusState == 0 & statusCount == stateTimer){
// statusState = 1;
// statusCount = 0;
// // printf(TEXT_YELLOW"\t\t\tstatusCount\t" TEXT_CYAN"\t%d" TEXT_YELLOW"\t\t\tstatusState\t" TEXT_CYAN"\t%d\n" NO_COLOR, statusCount, statusState);
// }
// if (statusState == 1 & statusCount < stateTimer){
// statusCount ++;
// // printf(TEXT_YELLOW"\t\t\tstatusCount\t" TEXT_CYAN"\t%d" TEXT_YELLOW"\t\t\tstatusState\t" TEXT_CYAN"\t%d\n" NO_COLOR, statusCount, statusState);
// mraa_gpio_write(statusPin, 0);
// }
// if (statusState == 1 & statusCount == stateTimer){
// statusState = 0;
// statusCount = 0;
// // printf(TEXT_YELLOW"\t\t\tstatusCount\t" TEXT_CYAN"\t%d" TEXT_YELLOW"\t\t\tstatusState\t" TEXT_CYAN"\t%d\n" NO_COLOR, statusCount, statusState);
// }
}
deletesynth();
return MRAA_SUCCESS;
}
void initalizeAudioDevice(){
// # reset codec
printf(TEXT_YELLOW"\n\ti2cset -y 1 0x1a 0x1e 0x00" TEXT_GREEN"\treset codec\n");
system("i2cset -y 1 0x1a 0x1e 0x00");
// # disable DAC and output powerdown
printf(TEXT_YELLOW"\ti2cset -y 1 0x1a 0x0c 0x07" TEXT_GREEN"\tdisable DAC and output powerdown\n");
system("i2cset -y 1 0x1a 0x0c 0x07");
// # set volume for headphone output (both channels)
printf(TEXT_YELLOW"\ti2cset -y 1 0x1a 0x05 0xff" TEXT_GREEN"\tset volume for headphone output (both channels)\n");
// printf(TEXT_YELLOW"\ti2cset -y 1 0x1a 0x05 0xff" TEXT_GREEN"\tset volume for headphone output (seperate channels)\n");
// system("i2cset -y 1 0x1a 0x05 0x65");
system("i2cset -y 1 0x1a 0x05 0x7f");
// system("i2cset -y 1 0x1a 0x04 0x20");
// system("i2cset -y 1 0x1a 0x02 0x7f");
// # analog audio path control (DAC enabled)
printf(TEXT_YELLOW"\ti2cset -y 1 0x1a 0x08 0x12" TEXT_GREEN"\tanalog audio path control (DAC enabled)\n");
system("i2cset -y 1 0x1a 0x08 0x12");
// # digital audio path control
printf(TEXT_YELLOW"\ti2cset -y 1 0x1a 0x0a 0x00" TEXT_GREEN"\tdigital audio path control\n");
system("i2cset -y 1 0x1a 0x0a 0x00");
// # set sample rate (48000Hz, assuming 12.288MHz codec clock)
printf(TEXT_YELLOW"\ti2cset -y 1 0x1a 0x10 0x00" TEXT_GREEN"\tset sample rate (48000Hz, assuming 12.288MHz codec clock)\n");
system("i2cset -y 1 0x1a 0x10 0x00");
// # digital audio interface format set (DSP mode, 24 bit)
printf(TEXT_YELLOW"\ti2cset -y 1 0x1a 0x0e 0x8b" TEXT_GREEN"\tdigital audio interface format set (DSP mode, 24 bit)\n");
system("i2cset -y 1 0x1a 0x0e 0x8b");
// # activate interface
printf(TEXT_YELLOW"\ti2cset -y 1 0x1a 0x12 0x01" TEXT_GREEN"\tactivate interface\n\n");
system("i2cset -y 1 0x1a 0x12 0x01");
}
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_setnum(settings, "synth.sample-rate", 48000);
fluid_settings_setint(settings, "audio.periods", 2);
fluid_settings_setint(settings, "audio.period-size", 384);
fluid_settings_setint(settings, "synth.cpu-cores", 2);
fluid_settings_setint(settings, "synth.polyphony", 128);
fluid_settings_setstr(settings, "synth.parallel-render", "yes");
// fluid_settings_setstr(settings, "audio.driver", "alsa");
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_Harp() {
int sfont_id;
// fluid_synth_sfload(synth, "/home/root/audio/audio_files/FluidR3_GM2-2.SF2", 1);
// fluid_synth_program_select_by_sfont_name(synth, );
// fluid_synth_program_select(synth, 1, "/home/root/audio/audio_files/FluidR3_GM2-2.SF2");
/* Load a SoundFont*/
//sfont_id = fluid_synth_sfload(synth, "/home/root/audio/audio_files/FluidR3_GM2-2.SF2", 0);
sfont_id = fluid_synth_sfload(synth, "/home/root/audio/audio_files/Harp_00.sf2", 0);
/* Select bank 0 and preset 0 in the SoundFont we just loaded on
* channel 0 */
// fluid_synth_program_select(synth, 0, sfont_id, 0, 47);
fluid_synth_program_change(synth, 0, 46);
}
void loadSoundFont_ThrowUp(){
int sfont_id;
// fluid_synth_sfload(synth, "/home/root/audio/audio_files/FluidR3_GM2-2.SF2", 1);
// fluid_synth_program_select_by_sfont_name(synth, );
// fluid_synth_program_select(synth, 1, "/home/root/audio/audio_files/FluidR3_GM2-2.SF2");
/* Load a SoundFont*/
sfont_id = fluid_synth_sfload(synth, "/home/root/audio/audio_files/Throw_Up-1a.sf2", 0); //WillSmith.sf2
/* Select bank 0 and preset 0 in the SoundFont we just loaded on
* channel 0 */
fluid_synth_program_select(synth, 0, sfont_id, 0, 0);
// fluid_synth_program_change(synth, 0, 46);
}
void loadSoundFont_WillSmith(){
int sfont_id;
// fluid_synth_sfload(synth, "/home/root/audio/audio_files/FluidR3_GM2-2.SF2", 1);
// fluid_synth_program_select_by_sfont_name(synth, );
// fluid_synth_program_select(synth, 1, "/home/root/audio/audio_files/FluidR3_GM2-2.SF2");
/* Load a SoundFont*/
sfont_id = fluid_synth_sfload(synth, "/home/root/audio/audio_files/WillSmith.sf2", 0); //WillSmith.sf2
/* Select bank 0 and preset 0 in the SoundFont we just loaded on
* channel 0 */
fluid_synth_program_select(synth, 0, sfont_id, 0, 0);
// fluid_synth_program_change(synth, 0, 46);
}
void playNote(int note, int state){
int inputPin_Numbers[24] = {
INPUT_PIN_00,
INPUT_PIN_01,
INPUT_PIN_02,
INPUT_PIN_03,
INPUT_PIN_04,
INPUT_PIN_05,
INPUT_PIN_06,
INPUT_PIN_07,
INPUT_PIN_08,
INPUT_PIN_09,
INPUT_PIN_10,
INPUT_PIN_11,
INPUT_PIN_12,
INPUT_PIN_13,
INPUT_PIN_14,
INPUT_PIN_15,
INPUT_PIN_16,
INPUT_PIN_17,
INPUT_PIN_18,
INPUT_PIN_19,
INPUT_PIN_20,
INPUT_PIN_21,
INPUT_PIN_22,
INPUT_PIN_23
};
int synthNotes[24] = {
NOTE_00,
NOTE_01,
NOTE_02,
NOTE_03,
NOTE_04,
NOTE_05,
NOTE_06,
NOTE_07,
NOTE_08,
NOTE_09,
NOTE_10,
NOTE_11,
NOTE_12,
NOTE_13,
NOTE_14,
NOTE_15,
NOTE_16,
NOTE_17,
NOTE_18,
NOTE_19,
NOTE_20,
NOTE_21,
NOTE_22,
NOTE_23,
};
if (state == 1){
printf(TEXT_YELLOW"\t\tsynthNotes[" TEXT_CYAN"%d" TEXT_YELLOW"]\tinputPin_Numbers[" TEXT_CYAN"%d" TEXT_YELLOW"]\t\tnote: " TEXT_GREEN"%d " TEXT_YELLOW"\t\tstate: " TEXT_GREEN"%d OFF\n", synthNotes[note], inputPin_Numbers[note], note, state);
fluid_synth_noteoff(synth, 0, synthNotes[note]);
} else {
printf(TEXT_YELLOW"\t\tsynthNotes[" TEXT_CYAN"%d" TEXT_YELLOW"]\tinputPin_Numbers[" TEXT_CYAN"%d" TEXT_YELLOW"]\t\tnote: " TEXT_RED"%d " TEXT_YELLOW"\t\tstate: " TEXT_RED"%d ON\n", synthNotes[note], inputPin_Numbers[note], note, state);
fluid_synth_noteon(synth, 0, synthNotes[note], 100);
}
}
void switchPressed_Utility_0(void* pin) {
mraa_gpio_context statusPin;
statusPin = mraa_gpio_init(STATUS_PIN);
mraa_gpio_dir(statusPin, MRAA_GPIO_OUT);
struct timespec ttime;
clock_gettime(CLOCK_REALTIME, &ttime);
usleep(500);
int s = mraa_gpio_read((mraa_gpio_context) pin);
if (s == 0) {
btime = ttime;
// printf(TEXT_RED"\t\ttime" NO_COLOR" = " TEXT_GREEN"%f (s)\n" NO_COLOR, btime);
printf(TEXT_YELLOW"\t\tUTILITY: " TEXT_CYAN"%d\t" TEXT_GREEN"RELEASED\n" NO_COLOR, pin);
mraa_gpio_write(statusPin, 1);
}
else {
double nseconds = (double)((ttime.tv_sec - btime.tv_sec) * BILLION) + (double) (ttime.tv_nsec - btime.tv_nsec) ;
// printf(TEXT_RED"\t\ttime" NO_COLOR" = " TEXT_GREEN"%f (s)\n" NO_COLOR, nseconds / BILLION);
printf(TEXT_YELLOW"\t\tswitchPressed_Utility_0\n" NO_COLOR, pin);
printf(TEXT_YELLOW"\t\tUTILITY: " TEXT_CYAN"%d\t" TEXT_RED"PRESSED\n" NO_COLOR, pin);
mraa_gpio_write(statusPin, 0);
loadSoundFont_Harp();
// loadSoundFont_ThrowUp();
}
}
void switchPressed_Utility_1(void* pin) {
mraa_gpio_context statusPin;
statusPin = mraa_gpio_init(STATUS_PIN);
mraa_gpio_dir(statusPin, MRAA_GPIO_OUT);
struct timespec ttime;
clock_gettime(CLOCK_REALTIME, &ttime);
usleep(500);
int s = mraa_gpio_read((mraa_gpio_context) pin);
if (s == 0) {
btime = ttime;
// printf(TEXT_RED"\t\ttime" NO_COLOR" = " TEXT_GREEN"%f (s)\n" NO_COLOR, btime);
printf(TEXT_YELLOW"\t\tUTILITY: " TEXT_CYAN"%d\t" TEXT_GREEN"RELEASED\n" NO_COLOR, pin);
mraa_gpio_write(statusPin, 1);
}
else {
double nseconds = (double)((ttime.tv_sec - btime.tv_sec) * BILLION) + (double) (ttime.tv_nsec - btime.tv_nsec) ;
// printf(TEXT_RED"\t\ttime" NO_COLOR" = " TEXT_GREEN"%f (s)\n" NO_COLOR, nseconds / BILLION);
printf(TEXT_YELLOW"\t\tswitchPressed_Utility_0\n" NO_COLOR, pin);
printf(TEXT_YELLOW"\t\tUTILITY: " TEXT_CYAN"%d\t" TEXT_RED"PRESSED\n" NO_COLOR, pin);
mraa_gpio_write(statusPin, 0);
// loadSoundFont_Violin();
// loadSoundFont_ThrowUp();
loadSoundFont_WillSmith();
// WillSmith.sf2
}
//Add a long press
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment