forked from sacchy777/mid2wav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudiobuf.h
178 lines (155 loc) · 6.47 KB
/
audiobuf.h
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
/*
* Audio Buffer library for audio processing
*
* audiobuf.h
*
*
* 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.
*
*/
/* ----------------------------------------------------------------------
* This library needs:
* misc.c
*
* This library assmes:
* (unsigned) int as 32 bit integer,
* (unsigned) short as 16 bit integer
*
* ----------------------------------------------------------------------*/
#ifndef AUDIOBUF_H
#define AUDIOBUF_H
#ifdef __cplusplus
extern "C" {
#endif
#define AUDIOBUF_VERSION "0.1"
/* ----------------------------------------------------------------------
* audio buffer data structure
*
* n_channels:
* number of audio channels, default 2(stereo)
* sampling_rate:
* sampling rate. used for making wav file header, default 44100
* qbit:
* quantum bit. used for making wav file header, default 16
* data:
* wave data in float(-1.0 .. 1.0)
* index:
* current index of the data
* size:
* number of float data in each channel
* ----------------------------------------------------------------------*/
typedef struct {
int n_channels;
int sampling_rate;
int qbit;
float **data;
int *index;
int size;
} audiobuf_t;
/* ----------------------------------------------------------------------
* constructor
*
* size: buffer size in number of floats in each channel
* n_channels: number of channels, 2 for stereo
* sampling_rate: number of samples in a second. 44100 for default
*
* returns:
* pointer or NULL
* ----------------------------------------------------------------------*/
audiobuf_t *audiobuf_create(int size, int n_channels, int sampling_rate, int qbit);
#define AUDIOBUF_SAMPLING_RATE_DEFAULT 44100
#define AUDIOBUF_QBIT_DEFAULT 16
#define AUDIOBUF_N_CHANNELS_DEFAULT 2
#define audiobuf_create_default(s) \
audiobuf_create(s, \
AUDIOBUF_N_CHANNELS_DEFAULT, \
AUDIOBUF_SAMPLING_RATE_DEFAULT, \
AUDIOBUF_QBIT_DEFAULT);
/* ----------------------------------------------------------------------
* another constructor
*
* creates audiobuf from an existing audiobuf
* ----------------------------------------------------------------------*/
audiobuf_t *audiobuf_duplicate(audiobuf_t *a);
/* ----------------------------------------------------------------------
* destructor
* ----------------------------------------------------------------------*/
void audiobuf_destroy(audiobuf_t *a);
/* ----------------------------------------------------------------------
* clears data
* ----------------------------------------------------------------------*/
void audiobuf_clear(audiobuf_t *a);
/* ----------------------------------------------------------------------
* rewinds index
* ----------------------------------------------------------------------*/
void audiobuf_rewind(audiobuf_t *a);
/* ----------------------------------------------------------------------
* sets index to pos
* ----------------------------------------------------------------------*/
void audiobuf_seek(audiobuf_t *a, int pos);
/* ----------------------------------------------------------------------
* adds data to specified channel, and proceed index
* ----------------------------------------------------------------------*/
#define AUDIOBUF_CHANNEL_L 0
#define AUDIOBUF_CHANNEL_R 1
void audiobuf_add(audiobuf_t *a, int channel, float data);
#define audiobuf_add_L(a, d) audiobuf_add(a, AUDIOBUF_CHANNEL_L, d)
#define audiobuf_add_R(a, d) audiobuf_add(a, AUDIOBUF_CHANNEL_R, d)
/* ----------------------------------------------------------------------
* overwrites data to specified channel, and proceed index
* ----------------------------------------------------------------------*/
void audiobuf_write(audiobuf_t *a, int channel, float data);
#define audiobuf_write_L(a, d) audiobuf_write(a, AUDIOBUF_CHANNEL_L, d)
#define audiobuf_write_R(a, d) audiobuf_write(a, AUDIOBUF_CHANNEL_R, d)
/* ----------------------------------------------------------------------
* save data to .wav file.
* call audiobuf_save_header first.
*
* returns:
* 0 .. success
* -1 .. failed
* ----------------------------------------------------------------------*/
int audiobuf_save_header(audiobuf_t *a, const char *filename);
int audiobuf_save_append(audiobuf_t *a, const char *filename);
#define audiobuf_save(a, f) audiobuf_save_header(a, f);audiobuf_save_append(a, f)
/* ----------------------------------------------------------------------
* copies data from src to dst
* ----------------------------------------------------------------------*/
void audoibuf_copy(audiobuf_t *src, audiobuf_t *dst);
/* ----------------------------------------------------------------------
* adds data from src to dst
* ----------------------------------------------------------------------*/
void audoibuf_merge(audiobuf_t *src, audiobuf_t *dst);
/* ----------------------------------------------------------------------
* gets a wave sample at current position, index does not proceed by
* calling this.
* ----------------------------------------------------------------------*/
float audiobuf_get(audiobuf_t *a, int channel);
#define audiobuf_get_L(a) audiobuf_get(a, AUDIOBUF_CHANNEL_L)
#define audiobuf_get_R(a) audiobuf_get(a, AUDIOBUF_CHANNEL_R)
/* ----------------------------------------------------------------------
* rewinds index only if index is out of range
* ----------------------------------------------------------------------*/
void audiobuf_wrap(audiobuf_t *a);
#ifdef __cplusplus
}
#endif
#endif