annotate libtest/wavreader.c @ 458:e26b974f7ba3

doc/AMR-study-utils: update for the current situation
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 10 May 2024 18:45:36 +0000
parents 634df6435e16
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /* ------------------------------------------------------------------
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Copyright (C) 2009 Martin Storsjo
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 *
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * Licensed under the Apache License, Version 2.0 (the "License");
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 * you may not use this file except in compliance with the License.
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * You may obtain a copy of the License at
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 * http://www.apache.org/licenses/LICENSE-2.0
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 *
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * Unless required by applicable law or agreed to in writing, software
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * distributed under the License is distributed on an "AS IS" BASIS,
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 * express or implied.
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 * See the License for the specific language governing permissions
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 * and limitations under the License.
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 * -------------------------------------------------------------------
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 */
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include "wavreader.h"
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 #include <stdio.h>
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 #include <stdlib.h>
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 #include <string.h>
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 #include <stdint.h>
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 #define TAG(a, b, c, d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 struct wav_reader {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 FILE *wav;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 uint32_t data_length;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 int format;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 int sample_rate;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 int bits_per_sample;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 int channels;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 int byte_rate;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 int block_align;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 };
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 static uint32_t read_tag(struct wav_reader* wr) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 uint32_t tag = 0;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 tag = (tag << 8) | fgetc(wr->wav);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 tag = (tag << 8) | fgetc(wr->wav);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 tag = (tag << 8) | fgetc(wr->wav);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 tag = (tag << 8) | fgetc(wr->wav);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 return tag;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 static uint32_t read_int32(struct wav_reader* wr) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 uint32_t value = 0;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 value |= fgetc(wr->wav) << 0;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 value |= fgetc(wr->wav) << 8;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 value |= fgetc(wr->wav) << 16;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 value |= fgetc(wr->wav) << 24;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return value;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 static uint16_t read_int16(struct wav_reader* wr) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 uint16_t value = 0;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 value |= fgetc(wr->wav) << 0;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 value |= fgetc(wr->wav) << 8;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 return value;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 void* wav_read_open(const char *filename) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 struct wav_reader* wr = (struct wav_reader*) malloc(sizeof(*wr));
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 long data_pos = 0;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 memset(wr, 0, sizeof(*wr));
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 wr->wav = fopen(filename, "rb");
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (wr->wav == NULL) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 free(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 return NULL;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 while (1) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 uint32_t tag, tag2, length;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 tag = read_tag(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 if (feof(wr->wav))
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 break;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 length = read_int32(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (tag != TAG('R', 'I', 'F', 'F') || length < 4) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 fseek(wr->wav, length, SEEK_CUR);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 continue;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 tag2 = read_tag(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 length -= 4;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (tag2 != TAG('W', 'A', 'V', 'E')) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 fseek(wr->wav, length, SEEK_CUR);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 continue;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 // RIFF chunk found, iterate through it
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 while (length >= 8) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 uint32_t subtag, sublength;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 subtag = read_tag(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 if (feof(wr->wav))
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 break;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 sublength = read_int32(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 length -= 8;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 if (length < sublength)
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 break;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 if (subtag == TAG('f', 'm', 't', ' ')) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 if (sublength < 16) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 // Insufficient data for 'fmt '
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 break;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 wr->format = read_int16(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 wr->channels = read_int16(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 wr->sample_rate = read_int32(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 wr->byte_rate = read_int32(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 wr->block_align = read_int16(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 wr->bits_per_sample = read_int16(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 fseek(wr->wav, sublength - 16, SEEK_CUR);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 } else if (subtag == TAG('d', 'a', 't', 'a')) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 data_pos = ftell(wr->wav);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 wr->data_length = sublength;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 fseek(wr->wav, sublength, SEEK_CUR);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 } else {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 fseek(wr->wav, sublength, SEEK_CUR);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 length -= sublength;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 if (length > 0) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 // Bad chunk?
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 fseek(wr->wav, length, SEEK_CUR);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 fseek(wr->wav, data_pos, SEEK_SET);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 return wr;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 void wav_read_close(void* obj) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 struct wav_reader* wr = (struct wav_reader*) obj;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 fclose(wr->wav);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 free(wr);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 int wav_get_header(void* obj, int* format, int* channels, int* sample_rate, int* bits_per_sample, unsigned int* data_length) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 struct wav_reader* wr = (struct wav_reader*) obj;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 if (format)
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 *format = wr->format;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 if (channels)
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 *channels = wr->channels;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 if (sample_rate)
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 *sample_rate = wr->sample_rate;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 if (bits_per_sample)
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 *bits_per_sample = wr->bits_per_sample;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 if (data_length)
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 *data_length = wr->data_length;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 return wr->format && wr->sample_rate;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 int wav_read_data(void* obj, unsigned char* data, unsigned int length) {
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 struct wav_reader* wr = (struct wav_reader*) obj;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 int n;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 if (wr->wav == NULL)
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 return -1;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 if (length > wr->data_length)
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 length = wr->data_length;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 n = fread(data, 1, length, wr->wav);
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 wr->data_length -= length;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 return n;
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 }
634df6435e16 libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163