File: audio_wave.c

package info (click to toggle)
twolame 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 3,440 kB
  • sloc: ansic: 9,939; sh: 4,451; perl: 343; makefile: 183
file content (242 lines) | stat: -rw-r--r-- 7,600 bytes parent folder | download
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
/*
 *  TwoLAME: an optimized MPEG Audio Layer Two encoder
 *
 *  Copyright (C) 2001-2004 Michael Cheng
 *  Copyright (C) 2004-2018 The TwoLAME Project
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "audio_wave.h"


/*****************************************************************************
*
*  Routines to determine byte order (of this machine) and swap bytes
*
*****************************************************************************/

enum byte_order { order_unknown, order_bigEndian, order_littleEndian };

static enum byte_order DetermineByteOrder(void)
{
    char s[sizeof(long) + 1];
    union {
        long longval;
        char charval[sizeof(long)];
    } probe;
    probe.longval = 0x41424344L;    /* ABCD in ASCII */
    strncpy(s, probe.charval, sizeof(long));
    s[sizeof(long)] = '\0';
    /* printf("byte order is %s\n", s ); */

    if (strcmp(s, "ABCD") == 0)
        return order_bigEndian;
    else if (strcmp(s, "DCBA") == 0)
        return order_littleEndian;
    else
        return order_unknown;
}

static void SwapBytesInWords(short *loc, int words)
{
    int i;
    short thisval;
    char *dst, *src;
    src = (char *) &thisval;
    for (i = 0; i < words; i++) {
        thisval = *loc;
        dst = (char *) loc++;
        dst[0] = src[1];
        dst[1] = src[0];
    }
}




/*****************************************************************************
*
*  Read in specified number of samples
*
*****************************************************************************/

int wave_get_samples(wave_info_t * wave_info, short int pcm[], int numSamples)
{
    int samples_read;
    FILE *file = wave_info->soundfile;

    samples_read = fread(pcm, sizeof(short int), numSamples, file);

    if (wave_info->byteswap) {
        SwapBytesInWords(pcm, samples_read);
    }

    return (samples_read / wave_info->channels);
}




/*****************************************************************************
*
*  parse the wave header.
*  returns NULL if not open failed
*  returns a wave_info_t * if wave header successfully parsed.
*  needs to fill in : samplerate + channels
*
*  POST: wave_info->soundfile is set to be at the start of the
*        PCM audio data
*
*****************************************************************************/

wave_info_t *wave_init(char *inPath)
{
    unsigned char wave_header_buffer[40];   // HH fixed
    int wave_header_stereo = -1;
    unsigned long samplerate;
    enum byte_order NativeByteOrder = order_unknown;


    wave_info_t *wave_info = NULL;
    FILE *file;

    if ((file = fopen(inPath, "rb")) == NULL) {
        printf("WAV: cannot open input file: %s\n", inPath);
        return (NULL);
    }


    /************** WAVE ************************/
    /* Nick Burch <The_Leveller@newmail.net> */
    /********************************************/
    /* Wave File Headers: (Dec) */
    /* 8-11 = "WAVE" */
    /* 22 = Stereo / Mono */
    /* 01 = mono, 02 = stereo */
    /* 24 = Sampling Frequency */
    /* 32 = Data Rate */
    /* 01 = x1 (8bit Mono) */
    /* 02 = x2 (8bit Stereo or */
    /* 16bit Mono) */
    /* 04 = x4 (16bit Stereo) */
    /********************************************/

    fseek(file, 0, SEEK_SET);
    if (fread(wave_header_buffer, 1, 40, file) != 40)
        return (NULL);

    if (wave_header_buffer[8] == 'W' && wave_header_buffer[9] == 'A'
            && wave_header_buffer[10] == 'V' && wave_header_buffer[11] == 'E') {
        printf("Parsing Wave File Header\n");
        if (NativeByteOrder == order_unknown) {
            NativeByteOrder = DetermineByteOrder();
            if (NativeByteOrder == order_unknown) {
                printf("byte order not determined\n");
                fclose(file);
                return (NULL);
            }
        }

        if (NativeByteOrder == order_littleEndian) {
            samplerate = wave_header_buffer[24] +
                         (wave_header_buffer[25] << 8) +
                         (wave_header_buffer[26] << 16) + (wave_header_buffer[27] << 24);
        } else {
            samplerate = wave_header_buffer[27] +
                         (wave_header_buffer[26] << 8) +
                         (wave_header_buffer[25] << 16) + (wave_header_buffer[24] << 24);
        }

        /* Wave File */
        switch (samplerate) {
        case 44100:
        case 48000:
        case 32000:
        case 24000:
        case 22050:
        case 16000:
            printf(">>> %ld Hz sampling freq selected\n", samplerate);
            break;
        default:
            /* Unknown Unsupported Frequency */
            printf(">>> Unknown samp freq %ld Hz in Wave Header\n", samplerate);
            printf(">>> Default 44.1 kHz samp freq selected\n");
            samplerate = 44100;
            break;
        }

        if ((long) wave_header_buffer[22] == 1) {
            printf(">>> Input Wave File is Mono\n");
            wave_header_stereo = 0;
        }
        if ((long) wave_header_buffer[22] == 2) {
            printf(">>> Input Wave File is Stereo\n");
            wave_header_stereo = 1;
        }
        if ((long) wave_header_buffer[32] == 1) {
            printf(">>> Input Wave File is 8 Bit\n");
            printf("Input File must be 16 Bit! Please Re-sample");
            fclose(file);
            return (NULL);
        }
        if ((long) wave_header_buffer[32] == 2) {
            if (wave_header_stereo == 1) {
                printf(">>> Input Wave File is 8 Bit\n");
                printf("Input File must be 16 Bit! Please Re-sample");
                fclose(file);
                return (NULL);
            }
        }

        /* should probably use the wave header to determine size here FIXME MFC Feb 2003 */
        if (fseek(file, 44, SEEK_SET) != 0) {
            /* there's a way of calculating the size of the wave header. i'll just jump 44 to start
               with */
            printf("Could not seek to PCM sound data in \"%s\".\n", inPath);
            fclose(file);
            return (NULL);
        }
        // Successfully processed the wave header
        wave_info = (wave_info_t *) calloc(1, sizeof(wave_info_t));
        wave_info->soundfile = file;
        if (wave_header_stereo == 1)
            wave_info->channels = 2;
        else
            wave_info->channels = 1;
        wave_info->samplerate = samplerate;

        // UNKNOWN. But we really should be able to work
        // it out. FIX THIS. MFC May03.
        wave_info->num_samples = -1;

        // Enable byte swap for big endian machines
        if (NativeByteOrder == order_bigEndian)
            wave_info->byteswap = 1;
        else
            wave_info->byteswap = 0;

        return (wave_info);
    }
    // not a wave file
    fclose(file);
    return (NULL);
}

/* vim:ts=4:sw=4:nowrap: */