| File: | read-sound-file.c |
| Warning: | line 194, column 17 This statement is never executed |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
| 2 | |
| 3 | /*** |
| 4 | This file is part of libkanberra. |
| 5 | |
| 6 | Copyright 2008 Lennart Poettering |
| 7 | |
| 8 | libkanberra is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU Lesser General Public License as |
| 10 | published by the Free Software Foundation, either version 2.1 of the |
| 11 | License, or (at your option) any later version. |
| 12 | |
| 13 | libkanberra is distributed in the hope that it will be useful, but |
| 14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | Lesser General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Lesser General Public |
| 19 | License along with libkanberra. If not, see |
| 20 | <http://www.gnu.org/licenses/>. |
| 21 | ***/ |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H1 |
| 24 | #include <config.h> |
| 25 | #endif |
| 26 | |
| 27 | #include <errno(*__errno_location ()).h> |
| 28 | |
| 29 | #include "read-sound-file.h" |
| 30 | #include "read-wav.h" |
| 31 | #include "read-vorbis.h" |
| 32 | #include "macro.h" |
| 33 | #include "malloc.h" |
| 34 | #include "kanberra.h" |
| 35 | |
| 36 | struct ka_sound_file { |
| 37 | ka_wav *wav; |
| 38 | ka_vorbis *vorbis; |
| 39 | char *filename; |
| 40 | |
| 41 | unsigned nchannels; |
| 42 | unsigned rate; |
| 43 | ka_sample_type_t type; |
| 44 | }; |
| 45 | |
| 46 | int ka_sound_file_open(ka_sound_file **_f, const char *fn) { |
| 47 | FILE *file; |
| 48 | ka_sound_file *f; |
| 49 | int ret; |
| 50 | |
| 51 | ka_return_val_if_fail(_f, KA_ERROR_INVALID)do { if ((__builtin_expect((!(_f)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "_f" , "read-sound-file.c", 51, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 52 | ka_return_val_if_fail(fn, KA_ERROR_INVALID)do { if ((__builtin_expect((!(fn)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "fn" , "read-sound-file.c", 52, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 53 | |
| 54 | if (!(f = ka_new0(ka_sound_file, 1)((ka_sound_file*) calloc(1, (sizeof(ka_sound_file)*(1)))))) |
| 55 | return KA_ERROR_OOM; |
| 56 | |
| 57 | if (!(f->filename = ka_strdupstrdup(fn))) { |
| 58 | ret = KA_ERROR_OOM; |
| 59 | goto fail; |
| 60 | } |
| 61 | |
| 62 | if (!(file = fopen(fn, "r"))) { |
| 63 | ret = errno(*__errno_location ()) == ENOENT2 ? KA_ERROR_NOTFOUND : KA_ERROR_SYSTEM; |
| 64 | goto fail; |
| 65 | } |
| 66 | |
| 67 | if ((ret = ka_wav_open(&f->wav, file)) == KA_SUCCESS) { |
| 68 | f->nchannels = ka_wav_get_nchannels(f->wav); |
| 69 | f->rate = ka_wav_get_rate(f->wav); |
| 70 | f->type = ka_wav_get_sample_type(f->wav); |
| 71 | *_f = f; |
| 72 | return KA_SUCCESS; |
| 73 | } |
| 74 | |
| 75 | if (ret == KA_ERROR_CORRUPT) { |
| 76 | |
| 77 | if (fseek(file, 0, SEEK_SET0) < 0) { |
| 78 | ret = KA_ERROR_SYSTEM; |
| 79 | goto fail; |
| 80 | } |
| 81 | |
| 82 | if ((ret = ka_vorbis_open(&f->vorbis, file)) == KA_SUCCESS) { |
| 83 | f->nchannels = ka_vorbis_get_nchannels(f->vorbis); |
| 84 | f->rate = ka_vorbis_get_rate(f->vorbis); |
| 85 | f->type = KA_SAMPLE_S16NE; |
| 86 | *_f = f; |
| 87 | return KA_SUCCESS; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fail: |
| 92 | |
| 93 | ka_freefree(f->filename); |
| 94 | ka_freefree(f); |
| 95 | |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | void ka_sound_file_close(ka_sound_file *f) { |
| 100 | ka_assert(f)do { if ((__builtin_expect((!(f)),0))) { fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s(). Aborting.\n" , "f" , "read-sound-file.c", 100, __PRETTY_FUNCTION__); abort (); } } while ((0)); |
| 101 | |
| 102 | if (f->wav) |
| 103 | ka_wav_close(f->wav); |
| 104 | if (f->vorbis) |
| 105 | ka_vorbis_close(f->vorbis); |
| 106 | |
| 107 | ka_freefree(f->filename); |
| 108 | ka_freefree(f); |
| 109 | } |
| 110 | |
| 111 | unsigned ka_sound_file_get_nchannels(ka_sound_file *f) { |
| 112 | ka_assert(f)do { if ((__builtin_expect((!(f)),0))) { fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s(). Aborting.\n" , "f" , "read-sound-file.c", 112, __PRETTY_FUNCTION__); abort (); } } while ((0)); |
| 113 | return f->nchannels; |
| 114 | } |
| 115 | |
| 116 | unsigned ka_sound_file_get_rate(ka_sound_file *f) { |
| 117 | ka_assert(f)do { if ((__builtin_expect((!(f)),0))) { fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s(). Aborting.\n" , "f" , "read-sound-file.c", 117, __PRETTY_FUNCTION__); abort (); } } while ((0)); |
| 118 | return f->rate; |
| 119 | } |
| 120 | |
| 121 | ka_sample_type_t ka_sound_file_get_sample_type(ka_sound_file *f) { |
| 122 | ka_assert(f)do { if ((__builtin_expect((!(f)),0))) { fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s(). Aborting.\n" , "f" , "read-sound-file.c", 122, __PRETTY_FUNCTION__); abort (); } } while ((0)); |
| 123 | return f->type; |
| 124 | } |
| 125 | |
| 126 | const ka_channel_position_t* ka_sound_file_get_channel_map(ka_sound_file *f) { |
| 127 | ka_assert(f)do { if ((__builtin_expect((!(f)),0))) { fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s(). Aborting.\n" , "f" , "read-sound-file.c", 127, __PRETTY_FUNCTION__); abort (); } } while ((0)); |
| 128 | |
| 129 | if (f->wav) |
| 130 | return ka_wav_get_channel_map(f->wav); |
| 131 | else |
| 132 | return ka_vorbis_get_channel_map(f->vorbis); |
| 133 | } |
| 134 | |
| 135 | int ka_sound_file_read_int16(ka_sound_file *f, int16_t *d, size_t *n) { |
| 136 | ka_return_val_if_fail(f, KA_ERROR_INVALID)do { if ((__builtin_expect((!(f)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "f" , "read-sound-file.c", 136, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 137 | ka_return_val_if_fail(d, KA_ERROR_INVALID)do { if ((__builtin_expect((!(d)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "d" , "read-sound-file.c", 137, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 138 | ka_return_val_if_fail(n, KA_ERROR_INVALID)do { if ((__builtin_expect((!(n)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "n" , "read-sound-file.c", 138, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 139 | ka_return_val_if_fail(*n > 0, KA_ERROR_INVALID)do { if ((__builtin_expect((!(*n > 0)),0))) { if (ka_debug ()) fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n" , "*n > 0" , "read-sound-file.c", 139, __PRETTY_FUNCTION__ ); return (KA_ERROR_INVALID); } } while((0)); |
| 140 | ka_return_val_if_fail(f->wav || f->vorbis, KA_ERROR_STATE)do { if ((__builtin_expect((!(f->wav || f->vorbis)),0)) ) { if (ka_debug()) fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n" , "f->wav || f->vorbis" , "read-sound-file.c", 140, __PRETTY_FUNCTION__ ); return (KA_ERROR_STATE); } } while((0)); |
| 141 | ka_return_val_if_fail(f->type == KA_SAMPLE_S16NE || f->type == KA_SAMPLE_S16RE, KA_ERROR_STATE)do { if ((__builtin_expect((!(f->type == KA_SAMPLE_S16NE || f->type == KA_SAMPLE_S16RE)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "f->type == KA_SAMPLE_S16NE || f->type == KA_SAMPLE_S16RE" , "read-sound-file.c", 141, __PRETTY_FUNCTION__); return (KA_ERROR_STATE ); } } while((0)); |
| 142 | |
| 143 | if (f->wav) |
| 144 | return ka_wav_read_s16le(f->wav, d, n); |
| 145 | else |
| 146 | return ka_vorbis_read_s16ne(f->vorbis, d, n); |
| 147 | } |
| 148 | |
| 149 | int ka_sound_file_read_uint8(ka_sound_file *f, uint8_t *d, size_t *n) { |
| 150 | ka_return_val_if_fail(f, KA_ERROR_INVALID)do { if ((__builtin_expect((!(f)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "f" , "read-sound-file.c", 150, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 151 | ka_return_val_if_fail(d, KA_ERROR_INVALID)do { if ((__builtin_expect((!(d)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "d" , "read-sound-file.c", 151, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 152 | ka_return_val_if_fail(n, KA_ERROR_INVALID)do { if ((__builtin_expect((!(n)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "n" , "read-sound-file.c", 152, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 153 | ka_return_val_if_fail(*n > 0, KA_ERROR_INVALID)do { if ((__builtin_expect((!(*n > 0)),0))) { if (ka_debug ()) fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n" , "*n > 0" , "read-sound-file.c", 153, __PRETTY_FUNCTION__ ); return (KA_ERROR_INVALID); } } while((0)); |
| 154 | ka_return_val_if_fail(f->wav && !f->vorbis, KA_ERROR_STATE)do { if ((__builtin_expect((!(f->wav && !f->vorbis )),0))) { if (ka_debug()) fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n" , "f->wav && !f->vorbis" , "read-sound-file.c", 154, __PRETTY_FUNCTION__); return (KA_ERROR_STATE); } } while ((0)); |
| 155 | ka_return_val_if_fail(f->type == KA_SAMPLE_U8, KA_ERROR_STATE)do { if ((__builtin_expect((!(f->type == KA_SAMPLE_U8)),0) )) { if (ka_debug()) fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n" , "f->type == KA_SAMPLE_U8" , "read-sound-file.c", 155, __PRETTY_FUNCTION__ ); return (KA_ERROR_STATE); } } while((0)); |
| 156 | |
| 157 | if (f->wav) |
| 158 | return ka_wav_read_u8(f->wav, d, n); |
| 159 | |
| 160 | return KA_ERROR_STATE; |
| 161 | } |
| 162 | |
| 163 | int ka_sound_file_read_arbitrary(ka_sound_file *f, void *d, size_t *n) { |
| 164 | int ret; |
| 165 | |
| 166 | ka_return_val_if_fail(f, KA_ERROR_INVALID)do { if ((__builtin_expect((!(f)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "f" , "read-sound-file.c", 166, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 167 | ka_return_val_if_fail(d, KA_ERROR_INVALID)do { if ((__builtin_expect((!(d)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "d" , "read-sound-file.c", 167, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 168 | ka_return_val_if_fail(n, KA_ERROR_INVALID)do { if ((__builtin_expect((!(n)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "n" , "read-sound-file.c", 168, __PRETTY_FUNCTION__); return (KA_ERROR_INVALID ); } } while((0)); |
| 169 | ka_return_val_if_fail(*n > 0, KA_ERROR_INVALID)do { if ((__builtin_expect((!(*n > 0)),0))) { if (ka_debug ()) fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s().\n" , "*n > 0" , "read-sound-file.c", 169, __PRETTY_FUNCTION__ ); return (KA_ERROR_INVALID); } } while((0)); |
| 170 | |
| 171 | switch (f->type) { |
| 172 | case KA_SAMPLE_S16NE: |
| 173 | case KA_SAMPLE_S16RE: { |
| 174 | size_t k; |
| 175 | |
| 176 | k = *n / sizeof(int16_t); |
| 177 | if ((ret = ka_sound_file_read_int16(f, d, &k)) == KA_SUCCESS) |
| 178 | *n = k * sizeof(int16_t); |
| 179 | |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | case KA_SAMPLE_U8: { |
| 184 | size_t k; |
| 185 | |
| 186 | k = *n; |
| 187 | if ((ret = ka_sound_file_read_uint8(f, d, &k)) == KA_SUCCESS) |
| 188 | *n = k; |
| 189 | |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | default: |
| 194 | ka_assert_not_reached()do { fprintf(stderr, "Code should not be reached at %s:%u, function %s(). Aborting.\n" , "read-sound-file.c", 194, __PRETTY_FUNCTION__); abort(); } while ((0)); |
This statement is never executed | |
| 195 | } |
| 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | off_t ka_sound_file_get_size(ka_sound_file *f) { |
| 201 | ka_return_val_if_fail(f, (off_t) -1)do { if ((__builtin_expect((!(f)),0))) { if (ka_debug()) fprintf (stderr, "Assertion '%s' failed at %s:%u, function %s().\n", "f" , "read-sound-file.c", 201, __PRETTY_FUNCTION__); return ((off_t ) -1); } } while((0)); |
| 202 | |
| 203 | if (f->wav) |
| 204 | return ka_wav_get_size(f->wav); |
| 205 | else |
| 206 | return ka_vorbis_get_size(f->vorbis); |
| 207 | } |
| 208 | |
| 209 | size_t ka_sound_file_frame_size(ka_sound_file *f) { |
| 210 | unsigned c; |
| 211 | |
| 212 | ka_assert(f)do { if ((__builtin_expect((!(f)),0))) { fprintf(stderr, "Assertion '%s' failed at %s:%u, function %s(). Aborting.\n" , "f" , "read-sound-file.c", 212, __PRETTY_FUNCTION__); abort (); } } while ((0)); |
| 213 | |
| 214 | c = ka_sound_file_get_nchannels(f); |
| 215 | |
| 216 | return c * (ka_sound_file_get_sample_type(f) == KA_SAMPLE_U8 ? 1U : 2U); |
| 217 | } |