aboutsummaryrefslogtreecommitdiff
path: root/lib/xbadpcm/mywav.h
blob: f29498dba4ba479f8a33c886a17f9360fccce22f (plain)
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
245
246
247
248
249
/*
  MyWAV 0.1.1
  by Luigi Auriemma
  e-mail: aluigi@autistici.org
  web:    aluigi.org

    Copyright 2005,2006 Luigi Auriemma

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

    http://www.gnu.org/licenses/gpl.txt
*/

#include <string.h>

//#include <stdint.h>
typedef unsigned char uint8_t;
typedef unsigned int uint32_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;

#include <stdio.h>

/*
the functions return ever 0 if success, other values (-1) if error
note that these functions have been written with compatibility in mind
so don't worry if you see useless instructions
*/



typedef struct {
    uint8_t     id[4];
    uint32_t    size;
} mywav_chunk;

typedef struct {
    int16_t     wFormatTag;
    uint16_t    wChannels;
    uint32_t    dwSamplesPerSec;
    uint32_t    dwAvgBytesPerSec;
    uint16_t    wBlockAlign;
    uint16_t    wBitsPerSample;
} mywav_fmtchunk;



    /* FILE WRITING */

    // 8 bit
int mywav_fwi08(FILE *fd, int num) {
    if(fputc((num      ) & 0xff, fd) < 0) return(-1);
    return(0);
}



    // 16 bit
int mywav_fwi16(FILE *fd, int num) {
    if(fputc((num      ) & 0xff, fd) < 0) return(-1);
    if(fputc((num >>  8) & 0xff, fd) < 0) return(-1);
    return(0);
}



    // 32 bit
int mywav_fwi32(FILE *fd, int num) {
    if(fputc((num      ) & 0xff, fd) < 0) return(-1);
    if(fputc((num >>  8) & 0xff, fd) < 0) return(-1);
    if(fputc((num >> 16) & 0xff, fd) < 0) return(-1);
    if(fputc((num >> 24) & 0xff, fd) < 0) return(-1);
    return(0);
}



    // data
int mywav_fwmem(FILE *fd, uint8_t *mem, int size) {
    if(size) {
        if(fwrite(mem, size, 1, fd) != 1) return(-1);
    }
    return(0);
}



    // chunk
int mywav_fwchunk(FILE *fd, mywav_chunk *chunk) {
    if(mywav_fwmem(fd, chunk->id, 4)) return(-1);
    if(mywav_fwi32(fd, chunk->size))  return(-1);
    return(0);
}



  // fmtchunk
int mywav_fwfmtchunk(FILE *fd, mywav_fmtchunk *fmtchunk) {
    if(mywav_fwi16(fd, fmtchunk->wFormatTag))       return(-1);
    if(mywav_fwi16(fd, fmtchunk->wChannels))        return(-1);
    if(mywav_fwi32(fd, fmtchunk->dwSamplesPerSec))  return(-1);
    if(mywav_fwi32(fd, fmtchunk->dwAvgBytesPerSec)) return(-1);
    if(mywav_fwi16(fd, fmtchunk->wBlockAlign))      return(-1);
    if(mywav_fwi16(fd, fmtchunk->wBitsPerSample))   return(-1);
    return(0);
}



    /* FILE READING */

    // 8 bit
int mywav_fri08(FILE *fd, uint8_t *num) {
    if(fread(num, 1, 1, fd) != 1)  return(-1);
    return(0);
}



    // 16 bit
int mywav_fri16(FILE *fd, uint16_t *num) {
    uint16_t    ret;
    uint8_t     tmp;

    if(fread(&tmp, 1, 1, fd) != 1) return(-1);  ret = tmp;
    if(fread(&tmp, 1, 1, fd) != 1) return(-1);  ret |= (tmp << 8);
    *num = ret;
    return(0);
}



    // 32 bit
int mywav_fri32(FILE *fd, uint32_t *num) {
    uint32_t    ret;
    uint8_t     tmp;

    if(fread(&tmp, 1, 1, fd) != 1) return(-1);  ret = tmp;
    if(fread(&tmp, 1, 1, fd) != 1) return(-1);  ret |= (tmp << 8);
    if(fread(&tmp, 1, 1, fd) != 1) return(-1);  ret |= (tmp << 16);
    if(fread(&tmp, 1, 1, fd) != 1) return(-1);  ret |= (tmp << 24);
    *num = ret;
    return(0);
}



    // data
int mywav_frmem(FILE *fd, uint8_t *mem, int size) {
    if(size) {
        if(fread(mem, size, 1, fd) != 1) return(-1);
    }
    return(0);
}



    // chunk
int mywav_frchunk(FILE *fd, mywav_chunk *chunk) {
    if(mywav_frmem(fd, (uint8_t *)&chunk->id, 4)) return(-1);
    if(mywav_fri32(fd, (uint32_t *)&chunk->size))  return(-1);
    return(0);
}



  // fmtchunk
int mywav_frfmtchunk(FILE *fd, mywav_fmtchunk *fmtchunk) {
    if(mywav_fri16(fd, (uint16_t *)&fmtchunk->wFormatTag))       return(-1);
    if(mywav_fri16(fd, (uint16_t *)&fmtchunk->wChannels))        return(-1);
    if(mywav_fri32(fd, (uint32_t *)&fmtchunk->dwSamplesPerSec))  return(-1);
    if(mywav_fri32(fd, (uint32_t *)&fmtchunk->dwAvgBytesPerSec)) return(-1);
    if(mywav_fri16(fd, (uint16_t *)&fmtchunk->wBlockAlign))      return(-1);
    if(mywav_fri16(fd, (uint16_t *)&fmtchunk->wBitsPerSample))   return(-1);
    return(0);
}


    /* MYWAV MAIN FUNCTIONS */

int mywav_seekchunk(FILE *fd, uint8_t *find) {
    mywav_chunk chunk;

    if(fseek(fd, sizeof(mywav_chunk) + 4, SEEK_SET) < 0) return(-1);

    while(!mywav_frchunk(fd, &chunk)) {
        if(!memcmp(chunk.id, find, 4)) return(chunk.size);
        if(fseek(fd, chunk.size, SEEK_CUR) < 0) break;
    }
    return(-1);
}



int mywav_data(FILE *fd, mywav_fmtchunk *fmt) {
    mywav_chunk chunk;
    uint8_t     type[4];

    if(mywav_frchunk(fd, &chunk)   < 0) return(-1);
    if(mywav_frmem(fd, type, 4)    < 0) return(-1);
    if(memcmp(type, "WAVE", 4))         return(-1);

    if(mywav_seekchunk(fd, (uint8_t*)"fmt ") < 0) return(-1);
    if(mywav_frfmtchunk(fd, fmt)   < 0) return(-1);

    return(mywav_seekchunk(fd, (uint8_t*)"data"));
}



int mywav_writehead(FILE *fd, mywav_fmtchunk *fmt, uint32_t data_size, uint8_t *more, int morelen) {
    mywav_chunk chunk;

    memcpy(chunk.id, "RIFF", 4);
    chunk.size =
        4                      +
        sizeof(mywav_chunk)    +
        sizeof(mywav_fmtchunk) +
        morelen                +
        sizeof(mywav_chunk)    +
        data_size;

    if(mywav_fwchunk(fd, &chunk)      < 0) return(-1);
    if(mywav_fwmem(fd, (uint8_t*)"WAVE", 4)     < 0) return(-1);

    memcpy(chunk.id, "fmt ", 4);
    chunk.size = sizeof(mywav_fmtchunk) + morelen;
    if(mywav_fwchunk(fd, &chunk)      < 0) return(-1);
    if(mywav_fwfmtchunk(fd, fmt)      < 0) return(-1);
    if(mywav_fwmem(fd, more, morelen) < 0) return(-1);

    memcpy(chunk.id, "data", 4);
    chunk.size = data_size;
    if(mywav_fwchunk(fd, &chunk)      < 0) return(-1);
    return(0);
}