annotate miscutil/raw2wav.c @ 539:0daaae66b3f6

libtest: add TW-TS-005 write helper function
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 22 Sep 2024 00:21:16 +0000
parents c1dc094f0821
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
141
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads a 16-bit linear PCM speech recording in raw format
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * (either BE or LE) and converts it into WAV container format.
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdio.h>
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdint.h>
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdlib.h>
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <string.h>
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <strings.h>
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "../libtest/wavwriter.h"
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 static void
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 swap_bytes(bytes, cc)
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 uint8_t *bytes;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 unsigned cc;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 {
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 uint8_t *dp, *endp;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 int t;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 dp = bytes;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 endp = bytes + cc;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 while (dp < endp) {
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 t = dp[0];
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 dp[0] = dp[1];
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 dp[1] = t;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 dp += 2;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 main(argc, argv)
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 char **argv;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 {
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 int big_endian;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 FILE *binf;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 void *wav;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 uint8_t bytes[320];
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 int cc;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 if (argc != 4) {
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 usage: fprintf(stderr, "usage: %s be|le input.raw output.wav\n",
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 argv[0]);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 exit(1);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (!strcmp(argv[1], "be"))
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 big_endian = 1;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 else if (!strcmp(argv[1], "le"))
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 big_endian = 0;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 else
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 goto usage;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 binf = fopen(argv[2], "r");
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (!binf) {
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 perror(argv[2]);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 exit(1);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 wav = wav_write_open(argv[3], 8000, 16, 1);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (!wav) {
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 perror(argv[3]);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 exit(1);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 for (;;) {
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 cc = fread(bytes, 1, sizeof bytes, binf);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (cc <= 0)
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 break;
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 if (cc & 1) {
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 fprintf(stderr, "error: %s has odd number of bytes\n",
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 argv[2]);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 exit(1);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 }
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 if (big_endian)
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 swap_bytes(bytes, cc);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 wav_write_data(wav, bytes, cc);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 }
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 wav_write_close(wav);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 exit(0);
c1dc094f0821 pcm16-raw2wav utility written
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }