| 1 | /* |
| 2 | Copyright 2009 Last.fm Ltd. |
| 3 | Copyright 2009 John Stamp <[email protected]> |
| 4 | |
| 5 | This file is part of liblastfm. |
| 6 | |
| 7 | liblastfm is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | liblastfm is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with liblastfm. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | #include "FlacSource.h" |
| 21 | #include <algorithm> |
| 22 | #include <cassert> |
| 23 | #include <errno.h> |
| 24 | #include <iostream> |
| 25 | #include <limits> |
| 26 | #include <algorithm> |
| 27 | #include <stdexcept> |
| 28 | |
| 29 | #include <QFile> |
| 30 | |
| 31 | |
| 32 | FLAC__StreamDecoderWriteStatus FlacSource::_write_callback(const FLAC__StreamDecoder *, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
| 33 | { |
| 34 | assert(client_data != NULL); |
| 35 | FlacSource *instance = reinterpret_cast<FlacSource *>(client_data); |
| 36 | assert(instance != NULL); |
| 37 | return instance->write_callback(frame, buffer); |
| 38 | } |
| 39 | |
| 40 | FLAC__StreamDecoderWriteStatus FlacSource::write_callback(const FLAC__Frame *frame, const FLAC__int32 * const buffer[]) |
| 41 | { |
| 42 | m_outBufLen = 0; |
| 43 | |
| 44 | if ( m_outBuf ) |
| 45 | { |
| 46 | size_t i; |
| 47 | for(i = 0; i < frame->header.blocksize; i++) |
| 48 | { |
| 49 | switch ( m_channels ) |
| 50 | { |
| 51 | case 1: |
| 52 | m_outBuf[m_outBufLen] = (FLAC__int16)buffer[0][i]; // mono |
| 53 | m_outBufLen++; |
| 54 | break; |
| 55 | case 2: |
| 56 | m_outBuf[m_outBufLen] = (FLAC__int16)buffer[0][i]; // left channel |
| 57 | m_outBuf[m_outBufLen+1] = (FLAC__int16)buffer[1][i]; // right channel |
| 58 | m_outBufLen += 2; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | m_samplePos += frame->header.blocksize; |
| 63 | } |
| 64 | |
| 65 | return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE; |
| 66 | } |
| 67 | |
| 68 | // --------------------------------------------------------------------- |
| 69 | |
| 70 | void FlacSource::_metadata_callback(const FLAC__StreamDecoder *, const FLAC__StreamMetadata *metadata, void *client_data) |
| 71 | { |
| 72 | assert(client_data != NULL); |
| 73 | FlacSource *instance = reinterpret_cast<FlacSource *>(client_data); |
| 74 | assert(instance != NULL); |
| 75 | instance->metadata_callback(metadata); |
| 76 | } |
| 77 | |
| 78 | void FlacSource::metadata_callback( const FLAC__StreamMetadata *metadata ) |
| 79 | { |
| 80 | switch ( metadata->type ) |
| 81 | { |
| 82 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 83 | m_channels = metadata->data.stream_info.channels; |
| 84 | m_totalSamples = metadata->data.stream_info.total_samples; |
| 85 | m_samplerate = metadata->data.stream_info.sample_rate; |
| 86 | m_bps = metadata->data.stream_info.bits_per_sample; |
| 87 | m_maxFrameSize = metadata->data.stream_info.max_framesize; |
| 88 | break; |
| 89 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 90 | m_commentData = FLAC__metadata_object_clone(metadata); |
| 91 | break; |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // --------------------------------------------------------------------- |
| 98 | |
| 99 | void FlacSource::_error_callback(const FLAC__StreamDecoder *, FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 100 | { |
| 101 | assert(client_data != NULL); |
| 102 | FlacSource *instance = reinterpret_cast<FlacSource *>(client_data); |
| 103 | assert(instance != NULL); |
| 104 | instance->error_callback(status); |
| 105 | } |
| 106 | |
| 107 | void FlacSource::error_callback(FLAC__StreamDecoderErrorStatus status) |
| 108 | { |
| 109 | std::cerr << "Got FLAC error: " << FLAC__StreamDecoderErrorStatusString[status] << std::endl; |
| 110 | } |
| 111 | |
| 112 | // --------------------------------------------------------------------- |
| 113 | |
| 114 | FlacSource::FlacSource() |
| 115 | : m_decoder( 0 ) |
| 116 | , m_fileSize( 0 ) |
| 117 | , m_outBuf( 0 ) |
| 118 | , m_outBufLen( 0 ) |
| 119 | , m_outBufPos( 0 ) |
| 120 | , m_samplePos( 0 ) |
| 121 | , m_maxFrameSize( 0 ) |
| 122 | , m_commentData( 0 ) |
| 123 | , m_bps( 0 ) |
| 124 | , m_channels( 0 ) |
| 125 | , m_samplerate( 0 ) |
| 126 | , m_totalSamples( 0 ) |
| 127 | , m_eof( false ) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | // --------------------------------------------------------------------- |
| 132 | |
| 133 | FlacSource::~FlacSource() |
| 134 | { |
| 135 | if ( m_decoder ) |
| 136 | { |
| 137 | FLAC__stream_decoder_finish( m_decoder ); |
| 138 | FLAC__stream_decoder_delete( m_decoder ); |
| 139 | } |
| 140 | if ( m_commentData ) |
| 141 | FLAC__metadata_object_delete( m_commentData ); |
| 142 | if ( m_outBuf ) |
| 143 | free( m_outBuf ); |
| 144 | } |
| 145 | |
| 146 | // --------------------------------------------------------------------- |
| 147 | |
| 148 | void FlacSource::init(const QString& fileName) |
| 149 | { |
| 150 | m_fileName = fileName; |
| 151 | |
| 152 | if ( !m_decoder ) |
| 153 | { |
| 154 | FILE *f = fopen(QFile::encodeName(m_fileName), "rb" ); |
| 155 | if ( f ) |
| 156 | { |
| 157 | // Need to check which init call to use; flac doesn't do that for us |
| 158 | unsigned char header[35]; |
| 159 | bool isOgg = false; |
| 160 | fread( header, 1, 35, f ); |
| 161 | if ( memcmp(header, "OggS", 4) == 0 && |
| 162 | memcmp(&header[29], "FLAC", 4) == 0 ) |
| 163 | isOgg = true; |
| 164 | |
| 165 | // getInfo() will need this to calculate bitrate |
| 166 | fseek( f, 0, SEEK_END ); |
| 167 | m_fileSize = ftell(f); |
| 168 | |
| 169 | rewind( f ); |
| 170 | |
| 171 | m_decoder = FLAC__stream_decoder_new(); |
| 172 | FLAC__stream_decoder_set_metadata_respond(m_decoder, FLAC__METADATA_TYPE_STREAMINFO); |
| 173 | FLAC__stream_decoder_set_metadata_respond(m_decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 174 | |
| 175 | int init_status; |
| 176 | if ( FLAC_API_SUPPORTS_OGG_FLAC && isOgg ) |
| 177 | init_status = FLAC__stream_decoder_init_ogg_FILE( m_decoder, f, _write_callback, _metadata_callback, _error_callback, this ); |
| 178 | else |
| 179 | init_status = FLAC__stream_decoder_init_FILE( m_decoder, f, _write_callback, _metadata_callback, _error_callback, this ); |
| 180 | |
| 181 | if(init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK) |
| 182 | return; |
| 183 | |
| 184 | FLAC__stream_decoder_process_until_end_of_metadata( m_decoder ); |
| 185 | m_outBuf = static_cast<signed short*>(malloc( sizeof(signed short)*m_maxFrameSize)); |
| 186 | |
| 187 | if ( m_bps != 16 ) |
| 188 | { |
| 189 | FLAC__stream_decoder_finish( m_decoder ); |
| 190 | FLAC__stream_decoder_delete( m_decoder ); |
| 191 | FLAC__metadata_object_delete( m_commentData ); |
| 192 | m_decoder = 0; |
| 193 | m_commentData = 0; |
| 194 | throw std::runtime_error( "ERROR: only 16 bit FLAC files are currently supported!" ); |
| 195 | } |
| 196 | } |
| 197 | else |
| 198 | throw std::runtime_error( "ERROR: cannot load FLAC file!" ); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // --------------------------------------------------------------------- |
| 203 | |
| 204 | /*QString FlacSource::getMbid() |
| 205 | { |
| 206 | if ( m_commentData ) |
| 207 | { |
| 208 | FLAC__StreamMetadata_VorbisComment *vc; |
| 209 | vc = &m_commentData->data.vorbis_comment; |
| 210 | for ( unsigned int i = 0; i < vc->num_comments; ++i ) |
| 211 | { |
| 212 | QByteArray key( (char*)(vc->comments[i].entry), vc->comments[i].length ); |
| 213 | if ( key.left(20).toLower() == "musicbrainz_trackid=" ) |
| 214 | { |
| 215 | QString val = key.mid(20); |
| 216 | return val; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return QString(); |
| 222 | }*/ |
| 223 | |
| 224 | // --------------------------------------------------------------------- |
| 225 | |
| 226 | void FlacSource::getInfo(int& lengthSecs, int& samplerate, int& bitrate, int& nchannels) |
| 227 | { |
| 228 | lengthSecs = 0; |
| 229 | samplerate = 0; |
| 230 | bitrate = 0; |
| 231 | nchannels = 0; |
| 232 | |
| 233 | if ( m_decoder ) |
| 234 | { |
| 235 | samplerate = m_samplerate; |
| 236 | nchannels = m_channels; |
| 237 | if ( samplerate > 0 ) |
| 238 | lengthSecs = static_cast<int>( static_cast<double>(m_totalSamples)/m_samplerate + 0.5); |
| 239 | |
| 240 | // Calcuate bitrate |
| 241 | if ( lengthSecs > 0 ) |
| 242 | { |
| 243 | FLAC__Metadata_SimpleIterator *it = FLAC__metadata_simple_iterator_new(); |
| 244 | FLAC__metadata_simple_iterator_init( it, QFile::encodeName(m_fileName), true, true ); |
| 245 | while( !FLAC__metadata_simple_iterator_is_last( it ) ) |
| 246 | { |
| 247 | FLAC__metadata_simple_iterator_next( it ); |
| 248 | } |
| 249 | off_t audioOffset = FLAC__metadata_simple_iterator_get_block_offset( it ) + |
| 250 | FLAC__metadata_simple_iterator_get_block_length( it ); |
| 251 | FLAC__metadata_simple_iterator_delete( it ); |
| 252 | bitrate = static_cast<int>( static_cast<double>(m_fileSize - audioOffset) * 8 / lengthSecs + 0.5 ); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // --------------------------------------------------------------------- |
| 258 | |
| 259 | void FlacSource::skip( const int mSecs ) |
| 260 | { |
| 261 | FLAC__uint64 absSample = mSecs * m_samplerate / 1000 + m_samplePos; |
| 262 | if ( !FLAC__stream_decoder_seek_absolute(m_decoder, absSample) ) |
| 263 | FLAC__stream_decoder_reset( m_decoder ); |
| 264 | m_outBufLen = 0; |
| 265 | } |
| 266 | |
| 267 | // --------------------------------------------------------------------- |
| 268 | |
| 269 | void FlacSource::skipSilence(double silenceThreshold /* = 0.0001 */) |
| 270 | { |
| 271 | silenceThreshold *= static_cast<double>( std::numeric_limits<short>::max() ); |
| 272 | for ( ;; ) |
| 273 | { |
| 274 | double sum = 0; |
| 275 | bool result = FLAC__stream_decoder_process_single( m_decoder ); |
| 276 | // there was a fatal read |
| 277 | if ( !result ) |
| 278 | break; |
| 279 | |
| 280 | switch ( m_channels ) |
| 281 | { |
| 282 | case 1: |
| 283 | for (size_t j = 0; j < m_outBufLen; ++j) |
| 284 | sum += abs( m_outBuf[j] ); |
| 285 | break; |
| 286 | case 2: |
| 287 | for ( size_t j = 0; j < m_outBufLen; j+=2 ) |
| 288 | sum += abs( (m_outBuf[j] >> 1) |
| 289 | + (m_outBuf[j+1] >> 1) ); |
| 290 | break; |
| 291 | } |
| 292 | if ( (sum >= silenceThreshold * static_cast<double>(m_outBufLen) ) ) |
| 293 | break; |
| 294 | } |
| 295 | m_outBufLen = 0; |
| 296 | } |
| 297 | |
| 298 | // --------------------------------------------------------------------- |
| 299 | |
| 300 | int FlacSource::updateBuffer( signed short *pBuffer, size_t bufferSize ) |
| 301 | { |
| 302 | size_t nwrit = 0; |
| 303 | |
| 304 | for ( ;; ) |
| 305 | { |
| 306 | size_t samples_to_use = std::min (bufferSize - nwrit, m_outBufLen - m_outBufPos); |
| 307 | signed short* pBufferIt = pBuffer + nwrit; |
| 308 | |
| 309 | nwrit += samples_to_use; |
| 310 | assert( nwrit <= bufferSize ); |
| 311 | memcpy( pBufferIt, m_outBuf + m_outBufPos, sizeof(signed short)*samples_to_use ); |
| 312 | |
| 313 | if ( samples_to_use < m_outBufLen - m_outBufPos ) |
| 314 | m_outBufPos = samples_to_use; |
| 315 | else |
| 316 | { |
| 317 | m_outBufPos = 0; |
| 318 | bool result = FLAC__stream_decoder_process_single( m_decoder ); |
| 319 | // there was a fatal read |
| 320 | if ( !result ) |
| 321 | { |
| 322 | std::cerr << "Fatal error decoding FLAC" << std::endl; |
| 323 | return 0; |
| 324 | } |
| 325 | else if ( FLAC__stream_decoder_get_state( m_decoder ) == FLAC__STREAM_DECODER_END_OF_STREAM ) |
| 326 | { |
| 327 | m_eof = true; |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if ( nwrit == bufferSize ) |
| 333 | return static_cast<int>(nwrit); |
| 334 | } |
| 335 | return static_cast<int>(nwrit); |
| 336 | } |
| 337 | |
| 338 | // ----------------------------------------------------------------------------- |
| 339 |