void safe_decoder_delete_(FLAC__StreamDecoder *decoder) { if(decoder) { safe_decoder_finish_(decoder); FLAC__stream_decoder_delete(decoder); } }
void safe_decoder_delete_(FLAC__FileDecoder *decoder) { if(decoder) { safe_decoder_finish_(decoder); FLAC__file_decoder_delete(decoder); } }
FLAC__bool safe_decoder_init_(const char *filename, FLAC__StreamDecoder *decoder) { if(decoder == 0) return false; safe_decoder_finish_(decoder); FLAC__stream_decoder_set_md5_checking(decoder, false); FLAC__stream_decoder_set_metadata_ignore_all(decoder); FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO); FLAC__stream_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT); if(stream_data_.is_http_source) { flac_http_open(filename, 0); if(FLAC__stream_decoder_init_stream(decoder, http_read_callback_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, write_callback_, metadata_callback_, error_callback_, /*client_data=*/&stream_data_) != FLAC__STREAM_DECODER_INIT_STATUS_OK) return false; } else { if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, /*client_data=*/&stream_data_) != FLAC__STREAM_DECODER_INIT_STATUS_OK) return false; } if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) return false; return true; }
/*********************************************************************** * local routines **********************************************************************/ FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder) { if(decoder == 0) { MessageBox(mod_.hMainWindow, "Decoder instance is NULL", "ERROR initializing decoder", 0); return false; } safe_decoder_finish_(decoder); FLAC__file_decoder_set_md5_checking(decoder, false); FLAC__file_decoder_set_filename(decoder, filename); FLAC__file_decoder_set_write_callback(decoder, write_callback_); FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_); FLAC__file_decoder_set_error_callback(decoder, error_callback_); FLAC__file_decoder_set_client_data(decoder, &file_info_); if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) { MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)], "ERROR initializing decoder", 0); return false; } file_info_.abort_flag = false; if(!FLAC__file_decoder_process_until_end_of_metadata(decoder)) { MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)], "ERROR processing metadata", 0); return false; } if(file_info_.abort_flag) { /* metadata callback already popped up the error dialog */ return false; } return true; }
void FLAC_XMMS__stop(void) { if(stream_data_.is_playing) { stream_data_.is_playing = false; if(stream_data_.play_thread_open) { stream_data_.play_thread_open = false; pthread_join(decode_thread_, NULL); } flac_ip.output->close_audio(); safe_decoder_finish_(decoder_); } }
void stop() { if(thread_handle != INVALID_HANDLE_VALUE) { killDecodeThread = 1; if(WaitForSingleObject(thread_handle, INFINITE) == WAIT_TIMEOUT) { MessageBox(mod_.hMainWindow, "error asking thread to die!\n", "error killing decode thread", 0); TerminateThread(thread_handle, 0); } CloseHandle(thread_handle); thread_handle = INVALID_HANDLE_VALUE; } safe_decoder_finish_(decoder_); mod_.outMod->Close(); mod_.SAVSADeInit(); }
void *play_loop_(void *arg) { unsigned written_time_last = 0, bh_index_last_w = 0, bh_index_last_o = BITRATE_HIST_SIZE, blocksize = 1; FLAC__uint64 decode_position_last = 0, decode_position_frame_last = 0, decode_position_frame = 0; (void)arg; while(stream_data_.is_playing) { if(!stream_data_.eof) { while(sample_buffer_last_ - sample_buffer_first_ < SAMPLES_PER_WRITE) { unsigned s; s = sample_buffer_last_ - sample_buffer_first_; if(FLAC__stream_decoder_get_state(decoder_) == FLAC__STREAM_DECODER_END_OF_STREAM) { stream_data_.eof = true; break; } else if(!FLAC__stream_decoder_process_single(decoder_)) { /*@@@ this should probably be a dialog */ fprintf(stderr, "libxmms-flac: READ ERROR processing frame\n"); stream_data_.eof = true; break; } blocksize = sample_buffer_last_ - sample_buffer_first_ - s; decode_position_frame_last = decode_position_frame; if(stream_data_.is_http_source || !FLAC__stream_decoder_get_decode_position(decoder_, &decode_position_frame)) decode_position_frame = 0; } if(sample_buffer_last_ - sample_buffer_first_ > 0) { const unsigned n = min(sample_buffer_last_ - sample_buffer_first_, SAMPLES_PER_WRITE); int bytes = n * stream_data_.channels * stream_data_.sample_format_bytes_per_sample; FLAC__byte *sample_buffer_start = sample_buffer_ + sample_buffer_first_ * stream_data_.channels * stream_data_.sample_format_bytes_per_sample; unsigned written_time, bh_index_w; FLAC__uint64 decode_position; sample_buffer_first_ += n; flac_ip.add_vis_pcm(flac_ip.output->written_time(), stream_data_.sample_format, stream_data_.channels, bytes, sample_buffer_start); while(flac_ip.output->buffer_free() < (int)bytes && stream_data_.is_playing && stream_data_.seek_to_in_sec == -1) xmms_usleep(10000); if(stream_data_.is_playing && stream_data_.seek_to_in_sec == -1) flac_ip.output->write_audio(sample_buffer_start, bytes); /* compute current bitrate */ written_time = flac_ip.output->written_time(); bh_index_w = written_time / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE; if(bh_index_w != bh_index_last_w) { bh_index_last_w = bh_index_w; decode_position = decode_position_frame - (double)(sample_buffer_last_ - sample_buffer_first_) * (double)(decode_position_frame - decode_position_frame_last) / (double)blocksize; bitrate_history_[(bh_index_w + BITRATE_HIST_SIZE - 1) % BITRATE_HIST_SIZE] = decode_position > decode_position_last && written_time > written_time_last ? 8000 * (decode_position - decode_position_last) / (written_time - written_time_last) : stream_data_.sample_rate * stream_data_.channels * stream_data_.bits_per_sample; decode_position_last = decode_position; written_time_last = written_time; } } else { stream_data_.eof = true; xmms_usleep(10000); } } else xmms_usleep(10000); if(!stream_data_.is_http_source && stream_data_.seek_to_in_sec != -1) { const double distance = (double)stream_data_.seek_to_in_sec * 1000.0 / (double)stream_data_.length_in_msec; FLAC__uint64 target_sample = (FLAC__uint64)(distance * (double)stream_data_.total_samples); if(stream_data_.total_samples > 0 && target_sample >= stream_data_.total_samples) target_sample = stream_data_.total_samples - 1; if(FLAC__stream_decoder_seek_absolute(decoder_, target_sample)) { flac_ip.output->flush(stream_data_.seek_to_in_sec * 1000); bh_index_last_w = bh_index_last_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE; if(!FLAC__stream_decoder_get_decode_position(decoder_, &decode_position_frame)) decode_position_frame = 0; stream_data_.eof = false; sample_buffer_first_ = sample_buffer_last_ = 0; } else if(FLAC__stream_decoder_get_state(decoder_) == FLAC__STREAM_DECODER_SEEK_ERROR) { /*@@@ this should probably be a dialog */ fprintf(stderr, "libxmms-flac: SEEK ERROR\n"); FLAC__stream_decoder_flush(decoder_); stream_data_.eof = false; sample_buffer_first_ = sample_buffer_last_ = 0; } stream_data_.seek_to_in_sec = -1; } else { /* display the right bitrate from history */ unsigned bh_index_o = flac_ip.output->output_time() / BITRATE_HIST_SEGMENT_MSEC % BITRATE_HIST_SIZE; if(bh_index_o != bh_index_last_o && bh_index_o != bh_index_last_w && bh_index_o != (bh_index_last_w + 1) % BITRATE_HIST_SIZE) { bh_index_last_o = bh_index_o; flac_ip.set_info(stream_data_.title, stream_data_.length_in_msec, bitrate_history_[bh_index_o], stream_data_.sample_rate, stream_data_.channels); } } } safe_decoder_finish_(decoder_); /* are these two calls necessary? */ flac_ip.output->buffer_free(); flac_ip.output->buffer_free(); g_free(stream_data_.title); pthread_exit(NULL); return 0; /* to silence the compiler warning about not returning a value */ }
void FLAC_XMMS__play_file(char *filename) { FILE *f; sample_buffer_first_ = sample_buffer_last_ = 0; audio_error_ = false; stream_data_.abort_flag = false; stream_data_.is_playing = false; stream_data_.is_http_source = is_http_source(filename); stream_data_.eof = false; stream_data_.play_thread_open = false; stream_data_.has_replaygain = false; if(!is_http_source(filename)) { if(0 == (f = fopen(filename, "r"))) return; fclose(f); } if(decoder_ == 0) return; if(!safe_decoder_init_(filename, decoder_)) return; if(stream_data_.has_replaygain && flac_cfg.output.replaygain.enable) { if(flac_cfg.output.resolution.replaygain.bps_out == 8) { stream_data_.sample_format = FMT_U8; stream_data_.sample_format_bytes_per_sample = 1; } else if(flac_cfg.output.resolution.replaygain.bps_out == 16) { stream_data_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE; stream_data_.sample_format_bytes_per_sample = 2; } else { /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */ fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", flac_cfg.output.resolution.replaygain.bps_out); safe_decoder_finish_(decoder_); return; } } else { if(stream_data_.bits_per_sample == 8) { stream_data_.sample_format = FMT_U8; stream_data_.sample_format_bytes_per_sample = 1; } else if(stream_data_.bits_per_sample == 16 || (stream_data_.bits_per_sample == 24 && flac_cfg.output.resolution.normal.dither_24_to_16)) { stream_data_.sample_format = (is_big_endian_host_) ? FMT_S16_BE : FMT_S16_LE; stream_data_.sample_format_bytes_per_sample = 2; } else { /*@@@ need some error here like wa2: MessageBox(mod_.hMainWindow, "ERROR: plugin can only handle 8/16-bit samples\n", "ERROR: plugin can only handle 8/16-bit samples", 0); */ fprintf(stderr, "libxmms-flac: can't handle %d bit output\n", stream_data_.bits_per_sample); safe_decoder_finish_(decoder_); return; } } FLAC__replaygain_synthesis__init_dither_context(&stream_data_.dither_context, stream_data_.sample_format_bytes_per_sample * 8, flac_cfg.output.resolution.replaygain.noise_shaping); stream_data_.is_playing = true; if(flac_ip.output->open_audio(stream_data_.sample_format, stream_data_.sample_rate, stream_data_.channels) == 0) { audio_error_ = true; safe_decoder_finish_(decoder_); return; } stream_data_.title = flac_format_song_title(filename); flac_ip.set_info(stream_data_.title, stream_data_.length_in_msec, stream_data_.sample_rate * stream_data_.channels * stream_data_.bits_per_sample, stream_data_.sample_rate, stream_data_.channels); stream_data_.seek_to_in_sec = -1; stream_data_.play_thread_open = true; pthread_create(&decode_thread_, NULL, play_loop_, NULL); }