Blame view

main.cpp 2.64 KB
8a202cc2   eishchuk   Initial commit
1
2
3
4
  #include <iostream>
  #include "wavdata.h"
  #include "fft.h"
  #include <math.h>
c08d3ce3   eishchuk   Add key pressing
5
6
7
  #include <stdio.h>
  #include <stdlib.h>
  
8a202cc2   eishchuk   Initial commit
8
9
  
  #define DELAY 5000
c08d3ce3   eishchuk   Add key pressing
10
  #define TOUCH 30000
8a202cc2   eishchuk   Initial commit
11
12
  #define AMPLITUDE 0.5
  
c08d3ce3   eishchuk   Add key pressing
13
14
15
16
17
18
19
  #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
8a202cc2   eishchuk   Initial commit
20
  
c08d3ce3   eishchuk   Add key pressing
21
  #define MAX_NOTES 7
8a202cc2   eishchuk   Initial commit
22
  
8a202cc2   eishchuk   Initial commit
23
  
c08d3ce3   eishchuk   Add key pressing
24
25
26
  char * sounds[MAX_NOTES];
  char buttons[MAX_NOTES];
  int freqs[MAX_NOTES];
8a202cc2   eishchuk   Initial commit
27
      
c08d3ce3   eishchuk   Add key pressing
28
29
30
  void save(int freq, char * filename) {
      WavData data;
      data.load("COW.WAV");
8a202cc2   eishchuk   Initial commit
31
      
c08d3ce3   eishchuk   Add key pressing
32
      data.setFrequency(freq);
8a202cc2   eishchuk   Initial commit
33
      
c08d3ce3   eishchuk   Add key pressing
34
35
36
37
38
39
40
41
42
      char * data_do = data.data();
      char *data2 = new char[data.datasize()/2];
  
  	int i;
      int j = 0;
  	for(i=0;i<data.datasize();i+=2) {
  		data2[j]=data_do[i];
          j++;
      }
8a202cc2   eishchuk   Initial commit
43
      
c08d3ce3   eishchuk   Add key pressing
44
      int size = data.datasize()/2;
8a202cc2   eishchuk   Initial commit
45
46
      if (size > TOUCH) size = TOUCH;
      
c08d3ce3   eishchuk   Add key pressing
47
48
49
50
51
52
53
54
55
56
57
58
59
60
      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) {
8a202cc2   eishchuk   Initial commit
61
      
c08d3ce3   eishchuk   Add key pressing
62
63
      char command[256];
      int status;
8a202cc2   eishchuk   Initial commit
64
      
c08d3ce3   eishchuk   Add key pressing
65
66
67
68
69
70
71
72
      sprintf( command, "aplay -c 1 -q -t wav %s", filename );
  
      status = system( command );
       
      return status;
  }
  
  int run_sound(char button) {
8a202cc2   eishchuk   Initial commit
73
      
c08d3ce3   eishchuk   Add key pressing
74
75
76
77
78
      for(int i = 0; i < MAX_NOTES; i++ ) {
          if (buttons[i] == button) {
              return play_music(sounds[i]);
          }
      }
8a202cc2   eishchuk   Initial commit
79
      
c08d3ce3   eishchuk   Add key pressing
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
      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)
  {
8a202cc2   eishchuk   Initial commit
111
      
c08d3ce3   eishchuk   Add key pressing
112
      char button;
88c1f94c   eishchuk   Add notes
113
      
c08d3ce3   eishchuk   Add key pressing
114
115
      /* load sounds, buttons and frequencies*/
      load_data();
88c1f94c   eishchuk   Add notes
116
      
c08d3ce3   eishchuk   Add key pressing
117
118
      /* generate sounds from do to si (you'll find them in the same repository)*/
      generate_sounds();
88c1f94c   eishchuk   Add notes
119
      
c08d3ce3   eishchuk   Add key pressing
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");
88c1f94c   eishchuk   Add notes
121
      
c08d3ce3   eishchuk   Add key pressing
122
123
124
125
126
127
128
129
      /* 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);
      }
88c1f94c   eishchuk   Add notes
130
      
c08d3ce3   eishchuk   Add key pressing
131
132
      /* return to normal consol mode*/
      system ("/bin/stty cooked");
88c1f94c   eishchuk   Add notes
133
      
c08d3ce3   eishchuk   Add key pressing
134
      return 0;
8a202cc2   eishchuk   Initial commit
135
136
      
  }