Blame view

main.cpp 2.79 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
      char * data_do = data.data();
      char *data2 = new char[data.datasize()/2];
  
20416af8   eishchuk   Add logs
37
      // accelerate in 2 times
c08d3ce3   eishchuk   Add key pressing
38
39
40
41
42
43
  	int i;
      int j = 0;
  	for(i=0;i<data.datasize();i+=2) {
  		data2[j]=data_do[i];
          j++;
      }
8a202cc2   eishchuk   Initial commit
44
      
20416af8   eishchuk   Add logs
45
      // resize to one size (TOUCH)
c08d3ce3   eishchuk   Add key pressing
46
      int size = data.datasize()/2;
8a202cc2   eishchuk   Initial commit
47
48
      if (size > TOUCH) size = TOUCH;
      
c08d3ce3   eishchuk   Add key pressing
49
50
51
52
      data.clearData();
  	data.setDatasize(size);
  	data.setData(data2);
  
20416af8   eishchuk   Add logs
53
      // save with its filename
c08d3ce3   eishchuk   Add key pressing
54
55
56
57
58
59
60
61
62
63
      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
64
      
c08d3ce3   eishchuk   Add key pressing
65
66
      char command[256];
      int status;
8a202cc2   eishchuk   Initial commit
67
      
20416af8   eishchuk   Add logs
68
      /* make a command*/
c08d3ce3   eishchuk   Add key pressing
69
70
      sprintf( command, "aplay -c 1 -q -t wav %s", filename );
  
20416af8   eishchuk   Add logs
71
      /* execute command of play*/
c08d3ce3   eishchuk   Add key pressing
72
73
74
75
76
77
      status = system( command );
       
      return status;
  }
  
  int run_sound(char button) {
8a202cc2   eishchuk   Initial commit
78
      
c08d3ce3   eishchuk   Add key pressing
79
80
81
82
83
      for(int i = 0; i < MAX_NOTES; i++ ) {
          if (buttons[i] == button) {
              return play_music(sounds[i]);
          }
      }
8a202cc2   eishchuk   Initial commit
84
      
c08d3ce3   eishchuk   Add key pressing
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
      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
116
      
c08d3ce3   eishchuk   Add key pressing
117
      char button;
88c1f94c   eishchuk   Add notes
118
      
c08d3ce3   eishchuk   Add key pressing
119
120
      /* load sounds, buttons and frequencies*/
      load_data();
88c1f94c   eishchuk   Add notes
121
      
c08d3ce3   eishchuk   Add key pressing
122
123
      /* generate sounds from do to si (you'll find them in the same repository)*/
      generate_sounds();
88c1f94c   eishchuk   Add notes
124
      
c08d3ce3   eishchuk   Add key pressing
125
      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
126
      
c08d3ce3   eishchuk   Add key pressing
127
128
129
130
131
132
133
134
      /* 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
135
      
c08d3ce3   eishchuk   Add key pressing
136
137
      /* return to normal consol mode*/
      system ("/bin/stty cooked");
88c1f94c   eishchuk   Add notes
138
      
c08d3ce3   eishchuk   Add key pressing
139
      return 0;
8a202cc2   eishchuk   Initial commit
140
141
      
  }