From c08d3ce39b3f7c3b367a2fabb716176d5fa028ae Mon Sep 17 00:00:00 2001 From: eishchuk Date: Wed, 19 Jun 2019 09:54:58 +0200 Subject: [PATCH] Add key pressing --- COW_d.WAV | Bin 91180 -> 0 bytes DO.WAV | Bin 35044 -> 0 bytes FA.WAV | Bin 35044 -> 0 bytes LA.WAV | Bin 35044 -> 0 bytes MI.WAV | Bin 35044 -> 0 bytes Makefile | 14 ++++---------- RE.WAV | Bin 35044 -> 0 bytes SI.WAV | Bin 35044 -> 0 bytes SOL.WAV | Bin 35044 -> 0 bytes clavier | Bin 0 -> 25728 bytes main.cpp | 170 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------- main.o | Bin 5400 -> 0 bytes readme.md | 9 +++++++++ task.txt | 1 - test | Bin 25184 -> 0 bytes testWAV.pro | 12 ------------ 16 files changed, 119 insertions(+), 87 deletions(-) delete mode 100644 COW_d.WAV create mode 100755 clavier create mode 100644 readme.md delete mode 100644 task.txt delete mode 100755 test delete mode 100644 testWAV.pro diff --git a/COW_d.WAV b/COW_d.WAV deleted file mode 100644 index bae44ad..0000000 Binary files a/COW_d.WAV and /dev/null differ diff --git a/DO.WAV b/DO.WAV index 4779914..dc35936 100644 Binary files a/DO.WAV and b/DO.WAV differ diff --git a/FA.WAV b/FA.WAV index 3537b76..89cae2c 100644 Binary files a/FA.WAV and b/FA.WAV differ diff --git a/LA.WAV b/LA.WAV index 71840da..a99657f 100644 Binary files a/LA.WAV and b/LA.WAV differ diff --git a/MI.WAV b/MI.WAV index 4f09f08..8d704aa 100644 Binary files a/MI.WAV and b/MI.WAV differ diff --git a/Makefile b/Makefile index b7eed0b..a6e7d1e 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,10 @@ -CC = g++ -FLAGS = -c -OBJECTS = $(SOURDES:.cpp=.o) - -TARGET = clavier - -$(TARGET) : $(OBJECTS) - -all: $(TARGET) +all: + g++ -c main.cpp fft.cpp wavdata.cpp + g++ -o clavier main.o fft.o wavdata.o clean: - rm -f core *.o $(TARGET) + rm -f core *.o clavier diff --git a/RE.WAV b/RE.WAV index 83b4b5c..6d0abf5 100644 Binary files a/RE.WAV and b/RE.WAV differ diff --git a/SI.WAV b/SI.WAV index de8d481..f430542 100644 Binary files a/SI.WAV and b/SI.WAV differ diff --git a/SOL.WAV b/SOL.WAV index 893df85..483617d 100644 Binary files a/SOL.WAV and b/SOL.WAV differ diff --git a/clavier b/clavier new file mode 100755 index 0000000..b35d47d Binary files /dev/null and b/clavier differ diff --git a/main.cpp b/main.cpp index 9b79b23..2bce6e7 100644 --- a/main.cpp +++ b/main.cpp @@ -2,93 +2,135 @@ #include "wavdata.h" #include "fft.h" #include +#include +#include + #define DELAY 5000 -#define TOUCH 35000 +#define TOUCH 30000 #define AMPLITUDE 0.5 -int main(int argc, char **argv) -{ - /*WavData w; - char * name = "COW.WAV"; - w.load(name); - - char *data = w.data(); - char *data2 = new char[w.datasize()*2]; - - int i; - for(i=0;i255)val=255; - if(val<0)val=0; +#define DO_FREQ 16744 +#define RE_FREQ 18794 +#define MI_FREQ 21096 +#define FA_FREQ 22350 +#define SOL_FREQ 25087 +#define LA_FREQ 28160 +#define SI_FREQ 31608 - data2[i]= (unsigned char)(unsigned int)val; - } +#define MAX_NOTES 7 - w.clearData(); - w.setDatasize(w.datasize()*2); - w.setData(data2); - char * name_save = "COW_d.WAV"; - w.save(name_save);*/ +char * sounds[MAX_NOTES]; +char buttons[MAX_NOTES]; +int freqs[MAX_NOTES]; +void save(int freq, char * filename) { + WavData data; + data.load("COW.WAV"); - // recréer une note ) partir du son de la vache + data.setFrequency(freq); - WavData note_do; - note_do.load("COW.WAV"); - - printf("Frequency : %d\n", note_do.frequency()); - - - - char * data_do = note_do.data(); + char * data_do = data.data(); + char *data2 = new char[data.datasize()/2]; + + int i; + int j = 0; + for(i=0;i TOUCH) size = TOUCH; - // changer le son pour avoir une note approprié + data.clearData(); + data.setDatasize(size); + data.setData(data2); + + data.save(filename); +} + +void generate_sounds() { + for (int i = 0; i < MAX_NOTES; i++) { + save(freqs[i], sounds[i]); + } +} + +int play_music(char* filename) { - /*int i; - for (i = 0; i < size ; i++) { - data_do[i] = i; - }*/ + char command[256]; + int status; - // assign new data & save + sprintf( command, "aplay -c 1 -q -t wav %s", filename ); + + status = system( command ); + + return status; +} + +int run_sound(char button) { - //note_do.clearData(); - note_do.setDatasize(TOUCH); - note_do.setFrequency(16744); - //note_do.setData(data_do); + for(int i = 0; i < MAX_NOTES; i++ ) { + if (buttons[i] == button) { + return play_music(sounds[i]); + } + } - note_do.save("DO.WAV"); + return -1; +} + +void load_data() { + sounds[0] = "DO.WAV", + sounds[1] = "RE.WAV", + sounds[2] = "MI.WAV", + sounds[3] = "FA.WAV", + sounds[4] = "SOL.WAV", + sounds[5] = "LA.WAV", + sounds[6] = "SI.WAV"; + + buttons[0] = 'q'; + buttons[1] = 's'; + buttons[2] = 'd'; + buttons[3] = 'f'; + buttons[4] = 'g'; + buttons[5] = 'h'; + buttons[6] = 'j'; + + freqs[0] = DO_FREQ; + freqs[1] = RE_FREQ; + freqs[2] = MI_FREQ; + freqs[3] = FA_FREQ; + freqs[4] = SOL_FREQ; + freqs[5] = LA_FREQ; + freqs[6] = SI_FREQ; +} + +int main(int argc, char **argv) +{ - note_do.setFrequency(18794); - note_do.save("RE.WAV"); + char button; - note_do.setFrequency(21096); - note_do.save("MI.WAV"); + /* load sounds, buttons and frequencies*/ + load_data(); - note_do.setFrequency(22350); - note_do.save("FA.WAV"); + /* generate sounds from do to si (you'll find them in the same repository)*/ + generate_sounds(); - note_do.setFrequency(25087); - note_do.save("SOL.WAV"); + printf("You can play on the keyboards q-j which corresponds to the notes do - si\n. To exit press e.\n Enjoy!\n"); - note_do.setFrequency(28160); - note_do.save("LA.WAV"); + /* reconfigure consol to detect pressed key without enter pressing*/ + system ("/bin/stty raw"); + + /* char can be read without enter; to stop it - . should be pressed*/ + while((button=getchar())!= '.') { + putchar(button); + run_sound(button); + } - note_do.setFrequency(31608); - note_do.save("SI.WAV"); + /* return to normal consol mode*/ + system ("/bin/stty cooked"); + return 0; } diff --git a/main.o b/main.o index 96985ef..c2ade45 100644 Binary files a/main.o and b/main.o differ diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..d46da7d --- /dev/null +++ b/readme.md @@ -0,0 +1,9 @@ +# to compile project +make clean +make all + +# to execute +./clavier + +# to play on "piano": press buttons from 'q' to 'j' which correspond to do - si +# to stop : press '.' diff --git a/task.txt b/task.txt deleted file mode 100644 index 20b2fbf..0000000 --- a/task.txt +++ /dev/null @@ -1 +0,0 @@ -Realiser un clavier de piano sur les touches pour avoir le son de vach pour chaque note musicale diff --git a/test b/test deleted file mode 100755 index cf8c001..0000000 Binary files a/test and /dev/null differ diff --git a/testWAV.pro b/testWAV.pro deleted file mode 100644 index d2d7879..0000000 --- a/testWAV.pro +++ /dev/null @@ -1,12 +0,0 @@ -###################################################################### -# Automatically generated by qmake (2.01a) lun. mai 9 11:16:11 2011 -###################################################################### - -TEMPLATE = app -TARGET = -DEPENDPATH += . -INCLUDEPATH += . - -# Input -HEADERS += fft.h wavdata.h -SOURCES += fft.cpp main.cpp wavdata.cpp -- libgit2 0.21.2