§ Топ-уровень
Это простой класс для создания wav-файла с разной частотой (11025, 22050, 44100) и каналами (1-моно или 2-стерео). Здесь используется 8-битный диапазон звука (от 0 до 255).
1#include <stdio.h>
2#include <stdlib.h>
3#include "wave.cc"
4
5int main(int argc, char* argv[]) {
6
7 Twave wave(44100, 1);
8 for (int i = 0; i < 44100; i++) wave.add(i & 255);
9 wave.save("hello.wav");
10 return 0;
11}
§ Код класса
1
2struct __attribute__((__packed__)) WAVEFMTHEADER {
3
4 unsigned int chunkId;
5 unsigned int chunkSize;
6 unsigned int format;
7 unsigned int subchunk1Id;
8 unsigned int subchunk1Size;
9 unsigned short audioFormat;
10 unsigned short numChannels;
11 unsigned int sampleRate;
12 unsigned int byteRate;
13 unsigned short blockAlign;
14 unsigned short bitsPerSample;
15 unsigned int subchunk2Id;
16 unsigned int subchunk2Size;
17};
18
19class Twave {
20
21protected:
22
23 unsigned int frequency = 44100;
24 unsigned short channels = 2;
25 unsigned int wav_size = 0;
26 unsigned int top = 0;
27 unsigned char* data = NULL;
28
29public:
30
31
32 Twave(int _frequency = 44100, int _channels = 2) {
33
34 frequency = _frequency;
35 channels = _channels;
36 top = 0;
37 wav_size = 0;
38 data = NULL;
39 }
40
41
42 ~Twave() {
43 if (data) free(data);
44 }
45
46
47 void add(unsigned char lf, unsigned char rt = 0) {
48
49
50 if (wav_size == 0) {
51
52 top = frequency * channels;
53 data = (unsigned char*) malloc(top);
54
55 } else if (wav_size >= top) {
56
57 top *= 2;
58 data = (unsigned char*) realloc(data, top);
59 }
60
61 if (channels >= 1) data[wav_size++] = lf;
62 if (channels == 2) data[wav_size++] = rt;
63 }
64
65
66 void save(const char* filename) {
67
68 unsigned int _wavesize = wav_size + 0x24;
69 unsigned int _byteinsec = frequency * channels;
70
71 struct WAVEFMTHEADER head = {
72 0x46464952,
73 _wavesize,
74 0x45564157,
75 0x20746d66,
76 16,
77 1,
78 channels,
79 frequency,
80 _byteinsec,
81 channels,
82 8,
83 0x61746164,
84 wav_size
85 };
86
87 FILE* fp = fopen(filename, "wb");
88 if (fp) {
89
90 fseek(fp, 0, SEEK_SET);
91 fwrite(&head, 1, sizeof(WAVEFMTHEADER), fp);
92 fwrite(data, 1, wav_size, fp);
93 fclose(fp);
94 }
95 }
96};