]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2013 nu774 | |
3 | * For conditions of distribution and use, see copyright notice in COPYING | |
4 | */ | |
5 | #if HAVE_CONFIG_H | |
6 | # include "config.h" | |
7 | #endif | |
8 | #include <stdlib.h> | |
9 | #include <string.h> | |
10 | #if HAVE_STDINT_H | |
11 | # include <stdint.h> | |
12 | #endif | |
13 | #include "pcm_reader.h" | |
14 | ||
15 | typedef struct pcm_sint16_converter_t { | |
16 | pcm_reader_vtbl_t *vtbl; | |
17 | pcm_reader_t *src; | |
18 | pcm_sample_description_t format; | |
19 | void *pivot; | |
20 | unsigned capacity; | |
21 | } pcm_sint16_converter_t; | |
22 | ||
23 | static inline pcm_reader_t *get_source(pcm_reader_t *reader) | |
24 | { | |
25 | return ((pcm_sint16_converter_t *)reader)->src; | |
26 | } | |
27 | ||
28 | static const | |
29 | pcm_sample_description_t *get_format(pcm_reader_t *reader) | |
30 | { | |
31 | return &((pcm_sint16_converter_t *)reader)->format; | |
32 | } | |
33 | ||
34 | static int64_t get_length(pcm_reader_t *reader) | |
35 | { | |
36 | return pcm_get_length(get_source(reader)); | |
37 | } | |
38 | ||
39 | static int64_t get_position(pcm_reader_t *reader) | |
40 | { | |
41 | return pcm_get_position(get_source(reader)); | |
42 | } | |
43 | ||
44 | static int read_frames(pcm_reader_t *reader, void *buffer, unsigned nframes) | |
45 | { | |
46 | pcm_sint16_converter_t *self = (pcm_sint16_converter_t *)reader; | |
47 | const pcm_sample_description_t *sfmt = pcm_get_format(self->src); | |
48 | unsigned bytes = nframes * sfmt->bytes_per_frame; | |
49 | ||
50 | if (self->capacity < bytes) { | |
51 | void *p = realloc(self->pivot, bytes); | |
52 | if (!p) return -1; | |
53 | self->pivot = p; | |
54 | self->capacity = bytes; | |
55 | } | |
56 | nframes = pcm_read_frames(self->src, self->pivot, nframes); | |
57 | if (pcm_convert_to_native_sint16(sfmt, self->pivot, nframes, buffer) < 0) | |
58 | return -1; | |
59 | return nframes; | |
60 | } | |
61 | ||
62 | static void teardown(pcm_reader_t **reader) | |
63 | { | |
64 | pcm_sint16_converter_t *self = (pcm_sint16_converter_t *)*reader; | |
65 | pcm_teardown(&self->src); | |
66 | free(self->pivot); | |
67 | free(self); | |
68 | *reader = 0; | |
69 | } | |
70 | ||
71 | static pcm_reader_vtbl_t my_vtable = { | |
72 | get_format, get_length, get_position, read_frames, teardown | |
73 | }; | |
74 | ||
75 | pcm_reader_t *pcm_open_sint16_converter(pcm_reader_t *reader) | |
76 | { | |
77 | pcm_sint16_converter_t *self = 0; | |
78 | pcm_sample_description_t *fmt; | |
79 | ||
80 | if ((self = calloc(1, sizeof(pcm_sint16_converter_t))) == 0) | |
81 | return 0; | |
82 | self->src = reader; | |
83 | self->vtbl = &my_vtable; | |
84 | memcpy(&self->format, pcm_get_format(reader), sizeof(self->format)); | |
85 | fmt = &self->format; | |
86 | #if WORDS_BIGENDIAN | |
87 | fmt->sample_type = PCM_TYPE_SINT_BE; | |
88 | #else | |
89 | fmt->sample_type = PCM_TYPE_SINT; | |
90 | #endif | |
91 | fmt->bits_per_channel = 16; | |
92 | fmt->bytes_per_frame = 2 * fmt->channels_per_frame; | |
93 | return (pcm_reader_t *)self; | |
94 | } |