forked from sacchy777/mid2wav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiobuf.c
248 lines (213 loc) · 7.64 KB
/
audiobuf.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Audio Buffer library for audio processing
*
* audiobuf.c
*
*
* Copyright (c) 2013 sada.gussy (sada dot gussy at gmail dot com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "audiobuf.h"
#include "misc.h"
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
audiobuf_t *audiobuf_create(int size, int n_channels, int sampling_rate, int qbit){
int i;
audiobuf_t *a;
a = (audiobuf_t *)malloc(sizeof(audiobuf_t));
if(a == NULL) goto error;
memset(a, 0, sizeof(audiobuf_t));
a->data = (float**)malloc(sizeof(float*) * n_channels);
if(a->data == NULL) goto error;
memset(a->data, 0, sizeof(float *) * n_channels);
a->index = (int*)malloc(sizeof(int) * n_channels);
if(a->index == NULL) goto error;
memset(a->index, 0, sizeof(int) * n_channels);
for(i = 0; i < n_channels; i ++){
a->data[i] = (float *)malloc(sizeof(float) * size);
if(a->data[i] == NULL) goto error;
}
a->n_channels = n_channels;
a->size = size;
a->qbit = qbit;
a->sampling_rate = sampling_rate;
audiobuf_clear(a);
return a;
error:
audiobuf_destroy(a);
return NULL;
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audiobuf_destroy(audiobuf_t *a){
int i;
if(a == NULL) return;
if(a->data == NULL) goto free1;
if(a->index == NULL) goto free2;
for(i = 0; i < a->n_channels; i ++){
if(a->data[i] == NULL){
goto free3;
}else{
free(a->data[i]);
}
}
free3:
free(a->index);
free2:
free(a->data);
free1:
free(a);
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audiobuf_copy(audiobuf_t *src, audiobuf_t *dst){
int i,j;
int maxdata = src->size < dst->size ? src->size : dst->size;
int maxchannel = src->n_channels < dst->n_channels ? src->n_channels : dst->n_channels;
for(i = 0; i < maxchannel; i ++){
for(j = 0; j < maxdata; j ++){
dst->data[i][j] = src->data[i][j];
}
}
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audoibuf_merge(audiobuf_t *src, audiobuf_t *dst){
int i,j;
int maxdata = src->size < dst->size ? src->size : dst->size;
int maxchannel = src->n_channels < dst->n_channels ? src->n_channels : dst->n_channels;
for(i = 0; i < maxchannel; i ++){
for(j = 0; j < maxdata; j ++){
dst->data[i][j] += src->data[i][j];
}
}
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
audiobuf_t *audiobuf_duplicate(audiobuf_t *a){
audiobuf_t *b;
b = audiobuf_create(a->size, a->n_channels, a->sampling_rate, a->qbit);
audiobuf_copy(a, b);
return b;
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audiobuf_clear(audiobuf_t *a){
int i;
for(i = 0; i < a->n_channels; i++){
memset(a->data[i], 0, sizeof(float) * a->size);
a->index[i] = 0;
}
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audiobuf_rewind(audiobuf_t *a){
int i;
for(i = 0; i < a->n_channels; i++){
a->index[i] = 0;
}
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audiobuf_seek(audiobuf_t *a, int pos){
int i;
for(i = 0; i < a->n_channels; i++){
a->index[i] = pos;
}
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audiobuf_add(audiobuf_t *a, int channel, float data){
a->data[channel][a->index[channel]] += data;
a->index[channel] ++;
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audiobuf_write(audiobuf_t *a, int channel, float data){
a->data[channel][a->index[channel]] = data;
a->index[channel] ++;
}
float audiobuf_get(audiobuf_t *a, int channel){
return a->data[channel][a->index[channel]];
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
void audiobuf_wrap(audiobuf_t *a){
int i;
for(i = 0 ; i < a->n_channels; i ++){
if(a->index[i] >= a->size) a->index[i] = 0;
}
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
int audiobuf_save_append(audiobuf_t *a, const char *filename){
int i, j;
int d;
size_t filesize;
FILE *fp;
struct stat statbuf;
fp = fopen(filename, "ab");
if(fp == NULL) return -1;
/* write raw pcm data */
for(i = 0 ; i < a->size; i ++){
for(j = 0; j < a->n_channels; j ++){
d = 0x8000 * (a->data[j][i]+1.0) - 0x8000;
fwrite2Bles(fp, d);
}
}
fclose(fp); /* To get next fseek work correctly, I have to close the file here */
fp = fopen(filename, "r+b");
if(fp == NULL) return -1;
fstat(fileno(fp), &statbuf);
filesize = statbuf.st_size;
fseek(fp, 4L, SEEK_SET);
fwrite4Bleu(fp, filesize - 8);
fseek(fp, 40L, SEEK_SET);
fwrite4Bleu(fp, filesize - 44);
fclose(fp);
return 0;
}
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------*/
int audiobuf_save_header(audiobuf_t *a, const char *filename){
FILE *fp;
fp = fopen(filename, "wb");
if(fp == NULL) return -1;
fwrite("RIFF", 4, 1, fp); /* RIFF Header*/
fwrite4Bleu(fp, 0); /* payload size */
fwrite("WAVE", 4, 1, fp); /* WAVE Header*/
fwrite("fmt ", 4, 1, fp); /* fmt chunk Header*/
fwrite4Bleu(fp, 16); /* fmt chunk size in bytes */
fwrite2Bleu(fp, 1); /* format ID of raw PCM */
fwrite2Bleu(fp, a->n_channels); /* number of channels */
fwrite4Bleu(fp, a->sampling_rate); /* sampling rate in Hz */
fwrite4Bleu(fp, a->sampling_rate*a->n_channels*a->qbit/8); /* data rate */
fwrite2Bleu(fp, a->n_channels*a->qbit/8); /* block size */
fwrite2Bleu(fp, a->qbit); /* bit per sample */
fwrite("data", 4, 1, fp); /* data chunk */
fwrite4Bleu(fp, 0); /* data size in bytes */
fclose(fp);
return 0;
}