7 #include "filesndfile.h"
10 #include "mainerror.h"
12 FileSndFile::FileSndFile(Asset *asset, File *file)
13 : FileBase(asset, file)
21 FileSndFile::~FileSndFile()
23 if(temp_double) delete [] temp_double;
26 int FileSndFile::check_sig(Asset *asset)
31 SNDFILE *fd = sf_open(asset->path, SFM_READ, &fd_config);
41 void FileSndFile::asset_to_format()
45 case FILE_PCM: fd_config.format = SF_FORMAT_RAW; break;
46 case FILE_WAV: fd_config.format = SF_FORMAT_WAV; break;
47 case FILE_AU: fd_config.format = SF_FORMAT_AU; break;
48 case FILE_AIFF: fd_config.format = SF_FORMAT_AIFF; break;
51 // Not all of these are allowed in all sound formats.
52 // Raw can't be float.
56 if(asset->format == FILE_WAV)
57 fd_config.format |= SF_FORMAT_PCM_U8;
60 fd_config.format |= SF_FORMAT_PCM_S8;
62 fd_config.format |= SF_FORMAT_PCM_U8;
66 fd_config.format |= SF_FORMAT_PCM_16;
68 if(asset->byte_order || asset->format == FILE_WAV)
69 fd_config.format |= SF_ENDIAN_LITTLE;
71 fd_config.format |= SF_ENDIAN_BIG;
75 fd_config.format |= SF_FORMAT_PCM_24;
77 if(asset->byte_order || asset->format == FILE_WAV)
78 fd_config.format |= SF_ENDIAN_LITTLE;
80 fd_config.format |= SF_ENDIAN_BIG;
84 fd_config.format |= SF_FORMAT_PCM_32;
86 if(asset->byte_order || asset->format == FILE_WAV)
87 fd_config.format |= SF_ENDIAN_LITTLE;
89 fd_config.format |= SF_ENDIAN_BIG;
93 fd_config.format |= SF_FORMAT_ULAW;
97 fd_config.format |= SF_FORMAT_FLOAT;
101 if(fd_config.format == FILE_WAV)
102 fd_config.format |= SF_FORMAT_MS_ADPCM;
104 fd_config.format |= SF_FORMAT_IMA_ADPCM;
105 fd_config.format |= SF_FORMAT_PCM_16;
109 fd_config.seekable = 1;
110 fd_config.samplerate = asset->sample_rate;
111 fd_config.channels = asset->channels;
112 //printf("FileSndFile::asset_to_format %x %d %d\n", fd_config.format, fd_config.pcmbitwidth, fd_config.channels);
115 void FileSndFile::format_to_asset()
117 //printf("FileSndFile::format_to_asset 1\n");
118 // User supplies values if PCM
119 if(asset->format == 0)
121 asset->byte_order = 0;
123 switch(fd_config.format & SF_FORMAT_TYPEMASK)
126 asset->format = FILE_WAV;
127 asset->byte_order = 1;
130 case SF_FORMAT_AIFF: asset->format = FILE_AIFF; break;
131 case SF_FORMAT_AU: asset->format = FILE_AU; break;
132 case SF_FORMAT_RAW: asset->format = FILE_PCM; break;
133 case SF_FORMAT_PAF: asset->format = FILE_SND; break;
134 case SF_FORMAT_SVX: asset->format = FILE_SND; break;
135 case SF_FORMAT_NIST: asset->format = FILE_SND; break;
138 switch(fd_config.format & SF_FORMAT_SUBMASK)
140 case SF_FORMAT_FLOAT:
141 asset->bits = BITSFLOAT;
144 asset->bits = BITSULAW;
146 case SF_FORMAT_IMA_ADPCM:
147 case SF_FORMAT_MS_ADPCM:
148 asset->bits = BITS_ADPCM;
150 case SF_FORMAT_PCM_16:
154 case SF_FORMAT_PCM_24:
158 case SF_FORMAT_PCM_32:
162 case SF_FORMAT_PCM_S8:
164 asset->bits = BITSLINEAR8;
166 case SF_FORMAT_PCM_U8:
168 asset->bits = BITSLINEAR8;
172 switch(fd_config.format & SF_FORMAT_ENDMASK)
174 case SF_ENDIAN_LITTLE:
175 asset->byte_order = 1;
178 asset->byte_order = 0;
182 asset->channels = fd_config.channels;
185 asset->audio_data = 1;
186 asset->audio_length = fd_config.frames;
187 if(!asset->sample_rate)
188 asset->sample_rate = fd_config.samplerate;
189 //printf("FileSndFile::format_to_asset %x %d %d %x\n", fd_config.format & SF_FORMAT_TYPEMASK, fd_config.pcmbitwidth, fd_config.samples, fd_config.format & SF_FORMAT_SUBMASK);
193 int FileSndFile::open_file(int rd, int wr)
201 if(asset->format == FILE_PCM)
204 fd = sf_open(asset->path, SFM_READ, &fd_config);
205 // Already given by user
206 if(fd) format_to_asset();
210 fd = sf_open(asset->path, SFM_READ, &fd_config);
211 // Doesn't calculate the length
212 if(fd) format_to_asset();
219 printf("FileSNDFile::open 1\n");
221 printf("FileSNDFile::open 1\n");
222 fd = sf_open(asset->path, SFM_WRITE, &fd_config);
223 printf("FileSNDFile::open 10 %p\n", fd);
229 eprintf("%s", sf_strerror(0));
235 int FileSndFile::close_file()
239 FileBase::close_file();
240 fd_config.format = 0;
244 int FileSndFile::set_audio_position(int64_t sample)
246 // Commented out /* && psf->dataoffset */ in sndfile.c: 761
247 if(sf_seek(fd, sample, SEEK_SET) < 0)
249 eprintf("sf_seek() to sample %lld failed, reason: %s\n", sample, sf_strerror(fd));
255 int FileSndFile::read_samples(double *buffer, int64_t len)
259 //printf("FileSndFile::read_samples %lld %lld\n", file->current_sample, len);
260 // Get temp buffer for interleaved channels
261 if(len <= 0 || len > 1000000)
262 eprintf("len=%d\n", len);
265 eprintf("buffer=%p\n", buffer);
267 if(temp_allocated && temp_allocated < len)
269 delete [] temp_double;
276 temp_allocated = len;
277 temp_double = new double[len * asset->channels];
280 result = !sf_read_double(fd, temp_double, len * asset->channels);
283 eprintf("fd=%p temp_double=%p len=%d asset=%p asset->channels=%d\n",
284 fd, temp_double, len, asset, asset->channels);
286 // Extract single channel
287 for(int i = 0, j = file->current_channel;
289 i++, j += asset->channels)
291 buffer[i] = temp_double[j];
297 int FileSndFile::write_samples(double **buffer, int64_t len)
301 // Get temp buffer for interleaved channels
302 //printf("FileSndFile::read_samples 1\n");
303 if(temp_allocated && temp_allocated < len)
306 delete [] temp_double;
310 //printf("FileSndFile::read_samples 2\n");
313 temp_allocated = len;
314 temp_double = new double[len * asset->channels];
317 // Interleave channels
318 for(int i = 0; i < asset->channels; i++)
320 for(int j = 0; j < len; j++)
322 double sample = buffer[i][j];
323 // Libsndfile does not limit values
324 //if(sample > 1.0 || sample < -1.0) printf("FileSndFile::write_samples %f\n", sample);
325 //printf("FileSndFile::write_samples %d %d\n", asset->bits, BITSFLOAT);
326 if(asset->bits != BITSFLOAT) CLAMP(sample, -1.0, (32767.0 / 32768.0));
327 temp_double[j * asset->channels + i] = sample;
331 result = !sf_writef_double(fd, temp_double, len);
336 void FileSndFile::get_parameters(BC_WindowBase *parent_window,
338 BC_WindowBase* &format_window,
344 SndFileConfig *window = new SndFileConfig(parent_window, asset);
345 format_window = window;
346 window->create_objects();
347 window->run_window();
352 SndFileConfig::SndFileConfig(BC_WindowBase *parent_window, Asset *asset)
353 : BC_Window(PROGRAM_NAME ": Audio Compression",
354 parent_window->get_abs_cursor_x(1),
355 parent_window->get_abs_cursor_y(1),
359 this->parent_window = parent_window;
363 SndFileConfig::~SndFileConfig()
365 if(bits_popup) delete bits_popup;
367 int SndFileConfig::create_objects()
372 switch(asset->format)
377 add_tool(new BC_Title(x, y, _("Compression:")));
379 if(asset->format == FILE_WAV)
380 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 1, 1, 0);
382 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 0, 0, 0);
384 bits_popup->create_objects();
389 if(asset->format != FILE_AU)
390 add_subwindow(new BC_CheckBox(x, y, &asset->dither, _("Dither")));
392 if(asset->format == FILE_PCM)
394 add_subwindow(new BC_CheckBox(x, y, &asset->signed_, _("Signed")));
396 add_subwindow(new BC_Title(x, y, _("Byte order:")));
397 add_subwindow(hilo = new SndFileHILO(this, x + 100, y));
398 add_subwindow(lohi = new SndFileLOHI(this, x + 170, y));
400 add_subwindow(new BC_OKButton(this));
404 int SndFileConfig::close_event()
412 SndFileHILO::SndFileHILO(SndFileConfig *gui, int x, int y)
413 : BC_Radial(x, y, gui->asset->byte_order == 0, _("Hi Lo"))
417 int SndFileHILO::handle_event()
419 gui->asset->byte_order = 0;
420 gui->lohi->update(0);
427 SndFileLOHI::SndFileLOHI(SndFileConfig *gui, int x, int y)
428 : BC_Radial(x, y, gui->asset->byte_order == 1, _("Lo Hi"))
432 int SndFileLOHI::handle_event()
434 gui->asset->byte_order = 1;
435 gui->hilo->update(0);