main.cpp
2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <iostream>
#include "wavdata.h"
#include "fft.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define DELAY 5000
#define TOUCH 30000
#define AMPLITUDE 0.5
#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
#define MAX_NOTES 7
char * sounds[MAX_NOTES];
char buttons[MAX_NOTES];
int freqs[MAX_NOTES];
void save(int freq, char * filename) {
WavData data;
data.load("COW.WAV");
data.setFrequency(freq);
char * data_do = data.data();
char *data2 = new char[data.datasize()/2];
// accelerate in 2 times
int i;
int j = 0;
for(i=0;i<data.datasize();i+=2) {
data2[j]=data_do[i];
j++;
}
// resize to one size (TOUCH)
int size = data.datasize()/2;
if (size > TOUCH) size = TOUCH;
data.clearData();
data.setDatasize(size);
data.setData(data2);
// save with its filename
data.save(filename);
}
void generate_sounds() {
for (int i = 0; i < MAX_NOTES; i++) {
save(freqs[i], sounds[i]);
}
}
int play_music(char* filename) {
char command[256];
int status;
/* make a command*/
sprintf( command, "aplay -c 1 -q -t wav %s", filename );
/* execute command of play*/
status = system( command );
return status;
}
int run_sound(char button) {
for(int i = 0; i < MAX_NOTES; i++ ) {
if (buttons[i] == button) {
return play_music(sounds[i]);
}
}
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)
{
char button;
/* load sounds, buttons and frequencies*/
load_data();
/* generate sounds from do to si (you'll find them in the same repository)*/
generate_sounds();
printf("You can play on the keyboards q-j which corresponds to the notes do - si\n. To exit press e.\n Enjoy!\n");
/* 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);
}
/* return to normal consol mode*/
system ("/bin/stty cooked");
return 0;
}