///////////////////////////////////////////////////////
| 177 | |
| 178 | //////////////////////////////////////////////////////////// |
| 179 | bool InputSoundFile::openFromStream(InputStream& stream) |
| 180 | { |
| 181 | // If the file is already open, first close it |
| 182 | close(); |
| 183 | |
| 184 | // Find a suitable reader for the file type |
| 185 | auto reader = SoundFileFactory::createReaderFromStream(stream); |
| 186 | if (!reader) |
| 187 | { |
| 188 | // Error message generated in called function. |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | // Don't forget to reset the stream to its beginning before re-opening it |
| 193 | if (stream.seek(0) != 0) |
| 194 | { |
| 195 | err() << "Failed to open sound file from stream (cannot restart stream)" << std::endl; |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | // Pass the stream to the reader |
| 200 | const auto info = reader->open(stream); |
| 201 | if (!info) |
| 202 | { |
| 203 | err() << "Failed to open input sound file from stream (reader open failure)" << std::endl; |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | // Take ownership of reader and store a reference to the stream without taking ownership |
| 208 | m_reader = std::move(reader); |
| 209 | m_stream = {&stream, false}; |
| 210 | |
| 211 | // Retrieve the attributes of the open sound file |
| 212 | m_sampleCount = info->sampleCount; |
| 213 | m_sampleRate = info->sampleRate; |
| 214 | m_channelMap = info->channelMap; |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | //////////////////////////////////////////////////////////// |