FreeCalypso > hg > gsm-codec-lib
annotate libtest/wavwriter.c @ 550:de333989a12b
document gsm[e]fr-decode-tw5[-r] utilities
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 05 Oct 2024 02:16:48 +0000 |
parents | 634df6435e16 |
children |
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 "wavwriter.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 <string.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 <stdlib.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 struct wav_writer { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 FILE *wav; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 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
|
28 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 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
|
30 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
|
31 int channels; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 }; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 static void write_string(struct wav_writer* ww, const char *str) { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 fputc(str[0], ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 fputc(str[1], ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 fputc(str[2], ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 fputc(str[3], ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 } |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 static void write_int32(struct wav_writer* ww, int value) { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 fputc((value >> 0) & 0xff, ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 fputc((value >> 8) & 0xff, ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 fputc((value >> 16) & 0xff, ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 fputc((value >> 24) & 0xff, ww->wav); |
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 void write_int16(struct wav_writer* ww, int value) { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 fputc((value >> 0) & 0xff, ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 fputc((value >> 8) & 0xff, ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 } |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 static void write_header(struct wav_writer* ww, int length) { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 int bytes_per_frame, bytes_per_sec; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 write_string(ww, "RIFF"); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 write_int32(ww, 4 + 8 + 16 + 8 + length); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 write_string(ww, "WAVE"); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 write_string(ww, "fmt "); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 write_int32(ww, 16); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 bytes_per_frame = ww->bits_per_sample/8*ww->channels; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 bytes_per_sec = bytes_per_frame*ww->sample_rate; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 write_int16(ww, 1); // Format |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 write_int16(ww, ww->channels); // Channels |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 write_int32(ww, ww->sample_rate); // Samplerate |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 write_int32(ww, bytes_per_sec); // Bytes per sec |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 write_int16(ww, bytes_per_frame); // Bytes per frame |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 write_int16(ww, ww->bits_per_sample); // 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
|
70 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 write_string(ww, "data"); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 write_int32(ww, length); |
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 void* wav_write_open(const char *filename, int sample_rate, int bits_per_sample, int channels) { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 struct wav_writer* ww = (struct wav_writer*) malloc(sizeof(*ww)); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 memset(ww, 0, sizeof(*ww)); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 ww->wav = fopen(filename, "wb"); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 if (ww->wav == NULL) { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 free(ww); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 return NULL; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 } |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 ww->data_length = 0; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 ww->sample_rate = sample_rate; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 ww->bits_per_sample = 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
|
86 ww->channels = channels; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 write_header(ww, ww->data_length); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 return ww; |
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 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 void wav_write_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
|
93 struct wav_writer* ww = (struct wav_writer*) obj; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 if (ww->wav == NULL) { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 free(ww); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 return; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 } |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 fseek(ww->wav, 0, SEEK_SET); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 write_header(ww, ww->data_length); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 fclose(ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 free(ww); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 } |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 void wav_write_data(void* obj, const unsigned char* data, int length) { |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 struct wav_writer* ww = (struct wav_writer*) obj; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 if (ww->wav == NULL) |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 return; |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
108 fwrite(data, length, 1, ww->wav); |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
109 ww->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
|
110 } |
634df6435e16
libtest: WAV reader and writer code from opencore-amr-0.1.6/test
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
111 |