| 1 | /** |
| 2 | * Copyright (c) 2006-2023 LOVE Development Team |
| 3 | * |
| 4 | * This software is provided 'as-is', without any express or implied |
| 5 | * warranty. In no event will the authors be held liable for any damages |
| 6 | * arising from the use of this software. |
| 7 | * |
| 8 | * Permission is granted to anyone to use this software for any purpose, |
| 9 | * including commercial applications, and to alter it and redistribute it |
| 10 | * freely, subject to the following restrictions: |
| 11 | * |
| 12 | * 1. The origin of this software must not be misrepresented; you must not |
| 13 | * claim that you wrote the original software. If you use this software |
| 14 | * in a product, an acknowledgment in the product documentation would be |
| 15 | * appreciated but is not required. |
| 16 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 17 | * misrepresented as being the original software. |
| 18 | * 3. This notice may not be removed or altered from any source distribution. |
| 19 | **/ |
| 20 | |
| 21 | #include "WaveDecoder.h" |
| 22 | |
| 23 | #include <string.h> |
| 24 | #include "common/config.h" |
| 25 | #include "common/Exception.h" |
| 26 | |
| 27 | namespace love |
| 28 | { |
| 29 | namespace sound |
| 30 | { |
| 31 | namespace lullaby |
| 32 | { |
| 33 | |
| 34 | // Callbacks |
| 35 | static wuff_sint32 read_callback(void *userdata, wuff_uint8 *buffer, size_t *size) |
| 36 | { |
| 37 | WaveFile *input = (WaveFile *) userdata; |
| 38 | size_t bytes_left = input->size - input->offset; |
| 39 | size_t target_size = *size < bytes_left ? *size : bytes_left; |
| 40 | memcpy(buffer, input->data + input->offset, target_size); |
| 41 | input->offset += target_size; |
| 42 | *size = target_size; |
| 43 | return WUFF_SUCCESS; |
| 44 | } |
| 45 | |
| 46 | static wuff_sint32 seek_callback(void *userdata, wuff_uint64 offset) |
| 47 | { |
| 48 | WaveFile *input = (WaveFile *)userdata; |
| 49 | input->offset = (size_t) (offset < input->size ? offset : input->size); |
| 50 | return WUFF_SUCCESS; |
| 51 | } |
| 52 | |
| 53 | static wuff_sint32 tell_callback(void *userdata, wuff_uint64 *offset) |
| 54 | { |
| 55 | WaveFile *input = (WaveFile *)userdata; |
| 56 | *offset = input->offset; |
| 57 | return WUFF_SUCCESS; |
| 58 | } |
| 59 | |
| 60 | wuff_callback WaveDecoderCallbacks = {read_callback, seek_callback, tell_callback}; |
| 61 | |
| 62 | |
| 63 | WaveDecoder::WaveDecoder(Data *data, int bufferSize) |
| 64 | : Decoder(data, bufferSize) |
| 65 | { |
| 66 | dataFile.data = (char *) data->getData(); |
| 67 | dataFile.size = data->getSize(); |
| 68 | dataFile.offset = 0; |
| 69 | |
| 70 | int wuff_status = wuff_open(&handle, &WaveDecoderCallbacks, &dataFile); |
| 71 | if (wuff_status < 0) |
| 72 | throw love::Exception("Could not open WAVE" ); |
| 73 | |
| 74 | try |
| 75 | { |
| 76 | wuff_status = wuff_stream_info(handle, &info); |
| 77 | if (wuff_status < 0) |
| 78 | throw love::Exception("Could not retrieve WAVE stream info" ); |
| 79 | |
| 80 | if (info.channels > 2) |
| 81 | throw love::Exception("Multichannel audio not supported" ); |
| 82 | |
| 83 | if (info.format != WUFF_FORMAT_PCM_U8 && info.format != WUFF_FORMAT_PCM_S16) |
| 84 | { |
| 85 | wuff_status = wuff_format(handle, WUFF_FORMAT_PCM_S16); |
| 86 | if (wuff_status < 0) |
| 87 | throw love::Exception("Could not set output format" ); |
| 88 | } |
| 89 | } |
| 90 | catch (love::Exception &) |
| 91 | { |
| 92 | wuff_close(handle); |
| 93 | throw; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | WaveDecoder::~WaveDecoder() |
| 98 | { |
| 99 | wuff_close(handle); |
| 100 | } |
| 101 | |
| 102 | bool WaveDecoder::accepts(const std::string &ext) |
| 103 | { |
| 104 | static const std::string supported[] = |
| 105 | { |
| 106 | "wav" , "" |
| 107 | }; |
| 108 | |
| 109 | for (int i = 0; !(supported[i].empty()); i++) |
| 110 | { |
| 111 | if (supported[i].compare(ext) == 0) |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | love::sound::Decoder *WaveDecoder::clone() |
| 119 | { |
| 120 | return new WaveDecoder(data.get(), bufferSize); |
| 121 | } |
| 122 | |
| 123 | int WaveDecoder::decode() |
| 124 | { |
| 125 | size_t size = 0; |
| 126 | |
| 127 | while (size < (size_t) bufferSize) |
| 128 | { |
| 129 | size_t bytes = bufferSize-size; |
| 130 | int wuff_status = wuff_read(handle, (wuff_uint8 *) buffer+size, &bytes); |
| 131 | |
| 132 | if (wuff_status < 0) |
| 133 | return 0; |
| 134 | else if (bytes == 0) |
| 135 | { |
| 136 | eof = true; |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | size += bytes; |
| 141 | } |
| 142 | |
| 143 | return (int) size; |
| 144 | } |
| 145 | |
| 146 | bool WaveDecoder::seek(double s) |
| 147 | { |
| 148 | int wuff_status = wuff_seek(handle, (wuff_uint64) (s * info.sample_rate)); |
| 149 | |
| 150 | if (wuff_status >= 0) |
| 151 | { |
| 152 | eof = false; |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | bool WaveDecoder::rewind() |
| 160 | { |
| 161 | int wuff_status = wuff_seek(handle, 0); |
| 162 | |
| 163 | if (wuff_status >= 0) |
| 164 | { |
| 165 | eof = false; |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | bool WaveDecoder::isSeekable() |
| 173 | { |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | int WaveDecoder::getChannelCount() const |
| 178 | { |
| 179 | return info.channels; |
| 180 | } |
| 181 | |
| 182 | int WaveDecoder::getBitDepth() const |
| 183 | { |
| 184 | return info.bits_per_sample == 8 ? 8 : 16; |
| 185 | } |
| 186 | |
| 187 | int WaveDecoder::getSampleRate() const |
| 188 | { |
| 189 | return info.sample_rate; |
| 190 | } |
| 191 | |
| 192 | double WaveDecoder::getDuration() |
| 193 | { |
| 194 | return (double) info.length / (double) info.sample_rate; |
| 195 | } |
| 196 | |
| 197 | } // lullaby |
| 198 | } // sound |
| 199 | } // love |
| 200 | |