4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "FLAC/stream_decoder.h"
28 #include "FLAC/stream_encoder.h"
32 #include "bcsignals.h"
33 #include "bitspopup.h"
34 #include "byteorder.h"
38 #include "filesystem.h"
41 #include "mwindow.inc"
44 FileFLAC::FileFLAC(Asset *asset, File *file)
45 : FileBase(asset, file)
48 if(asset->format == FILE_UNKNOWN) asset->format = FILE_FLAC;
49 asset->byte_order = 0;
57 void FileFLAC::get_parameters(BC_WindowBase *parent_window,
58 Asset *asset, BC_WindowBase* &format_window,
59 int audio_options, int video_options, EDL *edl)
63 FLACConfigAudio *window = new FLACConfigAudio(parent_window, asset);
64 format_window = window;
65 window->create_objects();
73 int FileFLAC::check_sig(Asset *asset, char *test)
88 int FileFLAC::reset_parameters_derived()
102 static FLAC__StreamDecoderWriteStatus write_callback(
103 const FLAC__StreamDecoder *decoder,
104 const FLAC__Frame *frame,
105 const FLAC__int32 * const buffer[],
108 FileFLAC *ptr = (FileFLAC*)client_data;
110 if(!ptr->initialized)
112 ptr->initialized = 1;
113 ptr->asset->audio_data = 1;
114 ptr->asset->channels = FLAC__stream_decoder_get_channels(ptr->flac_decode);
115 ptr->asset->bits = FLAC__stream_decoder_get_bits_per_sample(ptr->flac_decode);
116 ptr->asset->sample_rate = FLAC__stream_decoder_get_sample_rate(ptr->flac_decode);
117 ptr->asset->audio_length = FLAC__stream_decoder_get_total_samples(ptr->flac_decode);
118 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
123 int fragment = FLAC__stream_decoder_get_blocksize(ptr->flac_decode);
124 ptr->samples_read += fragment;
125 //printf("write_callback 2 %d\n", fragment);
127 if(ptr->temp_allocated < fragment)
131 for(int i = 0; i < ptr->temp_channels; i++)
133 delete [] ptr->temp_buffer[i];
135 delete [] ptr->temp_buffer;
138 ptr->temp_channels = ptr->asset->channels;
139 ptr->temp_buffer = new int32_t*[ptr->temp_channels];
140 for(int i = 0; i < ptr->temp_channels; i++)
142 ptr->temp_buffer[i] = new int32_t[fragment];
144 ptr->temp_allocated = fragment;
147 float audio_max = (float)0x7fff;
148 if(FLAC__stream_decoder_get_bits_per_sample(ptr->flac_decode) == 24)
149 audio_max = (float)0x7fffff;
151 int nchannels = FLAC__stream_decoder_get_channels(ptr->flac_decode);
152 if( nchannels > ptr->temp_channels ) nchannels = ptr->temp_channels;
153 for(int j = 0; j < nchannels; j++)
155 float *dst = (float*)ptr->temp_buffer[j];
156 int32_t *src = (int32_t*)buffer[j];
157 for(int i = 0; i < fragment; i++)
159 *dst++ = (float)*src++ / audio_max;
163 ptr->append_history((float**)ptr->temp_buffer, fragment);
165 //printf("write_callback 3\n");
166 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
169 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
173 static void metadata_callback(const FLAC__StreamDecoder *decoder,
174 const FLAC__StreamMetadata *metadata,
177 // printf("metadata_callback\n");
180 static void error_callback(const FLAC__StreamDecoder *decoder,
181 FLAC__StreamDecoderErrorStatus status,
184 // printf("error_callback\n");
191 int FileFLAC::open_file(int rd, int wr)
197 file_bytes = FileSystem::get_size(asset->path);
198 flac_decode = FLAC__stream_decoder_new();
199 FLAC__stream_decoder_init_file(flac_decode,
209 if(!FLAC__stream_decoder_process_single(flac_decode)) break;
216 FLAC__stream_decoder_seek_absolute(flac_decode, 0);
222 flac_encode = FLAC__stream_encoder_new();
223 FLAC__stream_encoder_set_channels(flac_encode, asset->channels);
224 FLAC__stream_encoder_set_bits_per_sample(flac_encode, asset->bits);
225 FLAC__stream_encoder_set_sample_rate(flac_encode, asset->sample_rate);
226 FLAC__stream_encoder_init_file(flac_encode,
236 int FileFLAC::close_file_derived()
240 FLAC__stream_decoder_finish(flac_decode);
241 FLAC__stream_decoder_delete(flac_decode);
246 FLAC__stream_encoder_finish(flac_encode);
247 FLAC__stream_encoder_delete(flac_encode);
253 for(int i = 0; i < temp_channels; i++)
254 delete [] temp_buffer[i];
255 delete [] temp_buffer;
261 int FileFLAC::write_samples(double **buffer, int64_t len)
263 // Create temporary buffer of samples
264 if(temp_allocated < len)
268 for(int i = 0; i < asset->channels; i++)
270 delete [] temp_buffer[i];
272 delete [] temp_buffer;
275 temp_buffer = new int32_t*[asset->channels];
276 for(int i = 0; i < asset->channels; i++)
278 temp_buffer[i] = new int32_t[len];
280 temp_allocated = len;
283 float audio_max = (float)0x7fff;
288 audio_max = (float)0x7fffff;
292 for(int i = 0; i < asset->channels; i++)
294 int32_t *dst = temp_buffer[i];
295 double *src = buffer[i];
296 for(int j = 0; j < len; j++)
298 double sample = *src++ * audio_max;
299 CLAMP(sample, -audio_max, audio_max);
300 *dst++ = (int32_t)sample;
304 int result = FLAC__stream_encoder_process(flac_encode,
311 int FileFLAC::read_samples(double *buffer, int64_t len)
314 update_pcm_history(len);
316 if(decode_end != decode_start)
318 FLAC__stream_decoder_seek_absolute(flac_decode, decode_start);
319 decode_end = decode_start;
325 while(samples_read < decode_len)
327 FLAC__uint64 position;
328 FLAC__stream_decoder_get_decode_position(flac_decode, &position);
330 if((int64_t)position >= file_bytes) break;
331 if(!FLAC__stream_decoder_process_single(flac_decode)) {
341 file->current_sample,
342 file->current_channel,
346 //printf("FileFLAC::read_samples 10\n");
359 FLACConfigAudio::FLACConfigAudio(BC_WindowBase *parent_window,
361 : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
362 parent_window->get_abs_cursor_x(1),
363 parent_window->get_abs_cursor_y(1),
372 this->parent_window = parent_window;
376 FLACConfigAudio::~FLACConfigAudio()
380 void FLACConfigAudio::create_objects()
383 lock_window("FLACConfigAudio::create_objects");
384 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 0, 0, 0);
385 bits_popup->create_objects();
387 add_subwindow(new BC_OKButton(this));
392 int FLACConfigAudio::close_event()