Commit c08d3ce39b3f7c3b367a2fabb716176d5fa028ae

Authored by eishchuk
1 parent 88c1f94c

Add key pressing

COW_d.WAV deleted
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
1 1
2 -CC = g++  
3 -FLAGS = -c  
4 -OBJECTS = $(SOURDES:.cpp=.o)  
5 -  
6 -TARGET = clavier  
7 -  
8 -$(TARGET) : $(OBJECTS)  
9 -  
10 -all: $(TARGET) 2 +all:
  3 + g++ -c main.cpp fft.cpp wavdata.cpp
  4 + g++ -o clavier main.o fft.o wavdata.o
11 5
12 clean: 6 clean:
13 - rm -f core *.o $(TARGET) 7 + rm -f core *.o clavier
14 8
15 9
16 10
No preview for this file type
No preview for this file type
No preview for this file type
clavier 0 → 100755
No preview for this file type
@@ -2,93 +2,135 @@ @@ -2,93 +2,135 @@
2 #include "wavdata.h" 2 #include "wavdata.h"
3 #include "fft.h" 3 #include "fft.h"
4 #include <math.h> 4 #include <math.h>
  5 +#include <stdio.h>
  6 +#include <stdlib.h>
  7 +
5 8
6 #define DELAY 5000 9 #define DELAY 5000
7 -#define TOUCH 35000 10 +#define TOUCH 30000
8 #define AMPLITUDE 0.5 11 #define AMPLITUDE 0.5
9 12
10 -int main(int argc, char **argv)  
11 -{  
12 - /*WavData w;  
13 - char * name = "COW.WAV";  
14 - w.load(name);  
15 -  
16 - char *data = w.data();  
17 - char *data2 = new char[w.datasize()*2];  
18 -  
19 - int i;  
20 - for(i=0;i<w.datasize();i++)  
21 - data2[i]=data[i];  
22 - for(;i<w.datasize()*2;i++)  
23 - data2[i]=128;  
24 -  
25 - for(int i=DELAY;i<2*w.datasize();i++)  
26 - {  
27 - float value = (float)(unsigned char)data2[i-DELAY]-128.0;  
28 - value = value * AMPLITUDE;  
29 - int val = (unsigned int)value + (unsigned char)data2[i];  
30 - if(val>255)val=255;  
31 - if(val<0)val=0; 13 +#define DO_FREQ 16744
  14 +#define RE_FREQ 18794
  15 +#define MI_FREQ 21096
  16 +#define FA_FREQ 22350
  17 +#define SOL_FREQ 25087
  18 +#define LA_FREQ 28160
  19 +#define SI_FREQ 31608
32 20
33 - data2[i]= (unsigned char)(unsigned int)val;  
34 - } 21 +#define MAX_NOTES 7
35 22
36 - w.clearData();  
37 - w.setDatasize(w.datasize()*2);  
38 - w.setData(data2);  
39 23
40 - char * name_save = "COW_d.WAV";  
41 - w.save(name_save);*/ 24 +char * sounds[MAX_NOTES];
  25 +char buttons[MAX_NOTES];
  26 +int freqs[MAX_NOTES];
42 27
  28 +void save(int freq, char * filename) {
  29 + WavData data;
  30 + data.load("COW.WAV");
43 31
44 - // recréer une note ) partir du son de la vache 32 + data.setFrequency(freq);
45 33
46 - WavData note_do;  
47 - note_do.load("COW.WAV");  
48 -  
49 - printf("Frequency : %d\n", note_do.frequency());  
50 -  
51 -  
52 -  
53 - char * data_do = note_do.data(); 34 + char * data_do = data.data();
  35 + char *data2 = new char[data.datasize()/2];
  36 +
  37 + int i;
  38 + int j = 0;
  39 + for(i=0;i<data.datasize();i+=2) {
  40 + data2[j]=data_do[i];
  41 + j++;
  42 + }
54 43
55 - // TOUCH = durée d'une note  
56 - int size = note_do.datasize(); 44 + int size = data.datasize()/2;
57 if (size > TOUCH) size = TOUCH; 45 if (size > TOUCH) size = TOUCH;
58 46
59 - // changer le son pour avoir une note approprié 47 + data.clearData();
  48 + data.setDatasize(size);
  49 + data.setData(data2);
  50 +
  51 + data.save(filename);
  52 +}
  53 +
  54 +void generate_sounds() {
  55 + for (int i = 0; i < MAX_NOTES; i++) {
  56 + save(freqs[i], sounds[i]);
  57 + }
  58 +}
  59 +
  60 +int play_music(char* filename) {
60 61
61 - /*int i;  
62 - for (i = 0; i < size ; i++) {  
63 - data_do[i] = i;  
64 - }*/ 62 + char command[256];
  63 + int status;
65 64
66 - // assign new data & save 65 + sprintf( command, "aplay -c 1 -q -t wav %s", filename );
  66 +
  67 + status = system( command );
  68 +
  69 + return status;
  70 +}
  71 +
  72 +int run_sound(char button) {
67 73
68 - //note_do.clearData();  
69 - note_do.setDatasize(TOUCH);  
70 - note_do.setFrequency(16744);  
71 - //note_do.setData(data_do); 74 + for(int i = 0; i < MAX_NOTES; i++ ) {
  75 + if (buttons[i] == button) {
  76 + return play_music(sounds[i]);
  77 + }
  78 + }
72 79
73 - note_do.save("DO.WAV"); 80 + return -1;
  81 +}
  82 +
  83 +void load_data() {
  84 + sounds[0] = "DO.WAV",
  85 + sounds[1] = "RE.WAV",
  86 + sounds[2] = "MI.WAV",
  87 + sounds[3] = "FA.WAV",
  88 + sounds[4] = "SOL.WAV",
  89 + sounds[5] = "LA.WAV",
  90 + sounds[6] = "SI.WAV";
  91 +
  92 + buttons[0] = 'q';
  93 + buttons[1] = 's';
  94 + buttons[2] = 'd';
  95 + buttons[3] = 'f';
  96 + buttons[4] = 'g';
  97 + buttons[5] = 'h';
  98 + buttons[6] = 'j';
  99 +
  100 + freqs[0] = DO_FREQ;
  101 + freqs[1] = RE_FREQ;
  102 + freqs[2] = MI_FREQ;
  103 + freqs[3] = FA_FREQ;
  104 + freqs[4] = SOL_FREQ;
  105 + freqs[5] = LA_FREQ;
  106 + freqs[6] = SI_FREQ;
  107 +}
  108 +
  109 +int main(int argc, char **argv)
  110 +{
74 111
75 - note_do.setFrequency(18794);  
76 - note_do.save("RE.WAV"); 112 + char button;
77 113
78 - note_do.setFrequency(21096);  
79 - note_do.save("MI.WAV"); 114 + /* load sounds, buttons and frequencies*/
  115 + load_data();
80 116
81 - note_do.setFrequency(22350);  
82 - note_do.save("FA.WAV"); 117 + /* generate sounds from do to si (you'll find them in the same repository)*/
  118 + generate_sounds();
83 119
84 - note_do.setFrequency(25087);  
85 - note_do.save("SOL.WAV"); 120 + printf("You can play on the keyboards q-j which corresponds to the notes do - si\n. To exit press e.\n Enjoy!\n");
86 121
87 - note_do.setFrequency(28160);  
88 - note_do.save("LA.WAV"); 122 + /* reconfigure consol to detect pressed key without enter pressing*/
  123 + system ("/bin/stty raw");
  124 +
  125 + /* char can be read without enter; to stop it - . should be pressed*/
  126 + while((button=getchar())!= '.') {
  127 + putchar(button);
  128 + run_sound(button);
  129 + }
89 130
90 - note_do.setFrequency(31608);  
91 - note_do.save("SI.WAV"); 131 + /* return to normal consol mode*/
  132 + system ("/bin/stty cooked");
92 133
  134 + return 0;
93 135
94 } 136 }
No preview for this file type
readme.md 0 → 100644
@@ -0,0 +1,9 @@ @@ -0,0 +1,9 @@
  1 +# to compile project
  2 +make clean
  3 +make all
  4 +
  5 +# to execute
  6 +./clavier
  7 +
  8 +# to play on "piano": press buttons from 'q' to 'j' which correspond to do - si
  9 +# to stop : press '.'
task.txt deleted
@@ -1 +0,0 @@ @@ -1 +0,0 @@
1 -Realiser un clavier de piano sur les touches pour avoir le son de vach pour chaque note musicale  
test deleted
No preview for this file type
testWAV.pro deleted
@@ -1,12 +0,0 @@ @@ -1,12 +0,0 @@
1 -######################################################################  
2 -# Automatically generated by qmake (2.01a) lun. mai 9 11:16:11 2011  
3 -######################################################################  
4 -  
5 -TEMPLATE = app  
6 -TARGET =  
7 -DEPENDPATH += .  
8 -INCLUDEPATH += .  
9 -  
10 -# Input  
11 -HEADERS += fft.h wavdata.h  
12 -SOURCES += fft.cpp main.cpp wavdata.cpp