-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample_decode.c
More file actions
72 lines (60 loc) · 2.21 KB
/
example_decode.c
File metadata and controls
72 lines (60 loc) · 2.21 KB
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
#include <helix_mp3.h>
#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#define SAMPLES_PER_FRAME 2
#define PCM_BUFFER_SIZE_SAMPLES (1024 * 32)
#define PCM_BUFFER_SIZE_FRAMES (PCM_BUFFER_SIZE_SAMPLES / SAMPLES_PER_FRAME)
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("Usage: %s infile.mp3 outfile.raw\n", argv[0]);
return -EINVAL;
}
const char *input_path = argv[1];
const char *output_path = argv[2];
helix_mp3_t mp3;
int16_t pcm_buffer[PCM_BUFFER_SIZE_SAMPLES];
int err;
FILE *out_fd;
/* Initialize decoder */
err = helix_mp3_init_file(&mp3, input_path);
if (err) {
printf("Failed to init decoder for file '%s', error: %d\n", input_path, err);
return err;
}
do {
/* Open output file */
out_fd = fopen(output_path, "wb");
if (out_fd == NULL) {
printf("Failed to open output file '%s'\n", output_path);
err = -EIO;
break;
}
printf("Decoding '%s' to '%s'...\n", input_path, output_path);
/* Decode the whole file */
while (1) {
const size_t frames_read = helix_mp3_read_pcm_frames_s16(&mp3, pcm_buffer, PCM_BUFFER_SIZE_FRAMES);
if (frames_read == 0) {
printf("Reached EOF!\n");
break;
}
const size_t frames_written = fwrite(pcm_buffer, sizeof(*pcm_buffer) * SAMPLES_PER_FRAME, frames_read, out_fd);
if (frames_written != frames_read) {
printf("Failed to write decoded frames to '%s'', expected %zu frames, written %zu frames!\n", output_path, frames_read, frames_written);
err = -EIO;
break;
}
}
const size_t frame_count = helix_mp3_get_pcm_frames_decoded(&mp3);
const uint32_t sample_rate = helix_mp3_get_sample_rate(&mp3);
const uint32_t bitrate = helix_mp3_get_bitrate(&mp3);
printf("Done! Decoded %zu frames, last frame sample rate: %" PRIu32 "Hz, bitrate: %" PRIu32 "kbps\n", frame_count, sample_rate, bitrate / 1000);
} while (0);
/* Cleanup */
if (out_fd != NULL) {
fclose(out_fd);
}
helix_mp3_deinit(&mp3);
return err;
}