comparison miscutil/raw2wav.c @ 141:c1dc094f0821

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