prevent popup deactivation while button_down
[goodguy/history.git] / cinelerra-5.0 / cinelerra / filesndfile.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "asset.h"
23 #include "assets.h"
24 #include "bcsignals.h"
25 #include "bitspopup.h"
26 #include "clip.h"
27 #include "file.h"
28 #include "filesndfile.h"
29 #include "format.inc"
30 #include "language.h"
31 #include "mwindow.inc"
32 #include "mainerror.h"
33
34 FileSndFile::FileSndFile(Asset *asset, File *file)
35  : FileBase(asset, file)
36 {
37         temp_double = 0;
38         temp_allocated = 0;
39         fd_config.format = 0;
40         fd = 0;
41 //      if(asset->format == FILE_UNKNOWN)
42 //              asset->format = FILE_PCM;
43 }
44
45 FileSndFile::~FileSndFile()
46 {
47         if(temp_double) delete [] temp_double;
48 }
49
50 int FileSndFile::check_sig(Asset *asset)
51 {
52         int result = 0;
53         SF_INFO fd_config;
54         fd_config.format = 0;
55         SNDFILE *fd = sf_open(asset->path, SFM_READ, &fd_config);
56         if(fd)
57         {
58                 sf_close(fd);
59                 result = 1;
60         }
61
62         return result;
63 }
64
65 void FileSndFile::asset_to_format()
66 {
67         switch(asset->format)
68         {
69                 case FILE_PCM:  fd_config.format = SF_FORMAT_RAW;  break;
70                 case FILE_WAV:  fd_config.format = SF_FORMAT_WAV;  break;
71                 case FILE_AU:   fd_config.format = SF_FORMAT_AU;   break;
72                 case FILE_AIFF: fd_config.format = SF_FORMAT_AIFF; break;
73         }
74
75 // Not all of these are allowed in all sound formats.
76 // Raw can't be float.
77         switch(asset->bits)
78         {
79                 case BITSLINEAR8:
80                         if(asset->format == FILE_WAV)
81                                 fd_config.format |= SF_FORMAT_PCM_U8;
82                         else
83                         if(asset->signed_)
84                                 fd_config.format |= SF_FORMAT_PCM_S8;
85                         else
86                                 fd_config.format |= SF_FORMAT_PCM_U8;
87                         break;
88
89                 case BITSLINEAR16:
90 // Only signed is supported
91                         fd_config.format |= SF_FORMAT_PCM_16;
92
93                         if(asset->byte_order || asset->format == FILE_WAV)
94                                 fd_config.format |= SF_ENDIAN_LITTLE;
95                         else
96                                 fd_config.format |= SF_ENDIAN_BIG;
97                         break;
98
99                 case BITSLINEAR24:
100                         fd_config.format |= SF_FORMAT_PCM_24;
101
102                         if(asset->byte_order || asset->format == FILE_WAV)
103                                 fd_config.format |= SF_ENDIAN_LITTLE;
104                         else
105                                 fd_config.format |= SF_ENDIAN_BIG;
106                         break;
107
108                 case BITSLINEAR32:
109                         fd_config.format |= SF_FORMAT_PCM_32;
110
111                         if(asset->byte_order || asset->format == FILE_WAV)
112                                 fd_config.format |= SF_ENDIAN_LITTLE;
113                         else
114                                 fd_config.format |= SF_ENDIAN_BIG;
115                         break;
116
117                 case BITSULAW:
118                         fd_config.format |= SF_FORMAT_ULAW;
119                         break;
120
121                 case BITSFLOAT:
122                         fd_config.format |= SF_FORMAT_FLOAT;
123                         break;
124
125                 case BITS_ADPCM:
126                         if(fd_config.format == FILE_WAV)
127                                 fd_config.format |= SF_FORMAT_MS_ADPCM;
128                         else
129                                 fd_config.format |= SF_FORMAT_IMA_ADPCM;
130                         fd_config.format |= SF_FORMAT_PCM_16;
131                         break;
132         }
133
134         fd_config.seekable = 1;
135         fd_config.samplerate = asset->sample_rate;
136         fd_config.channels  = asset->channels;
137 //printf("FileSndFile::asset_to_format %x %d %d\n", fd_config.format, fd_config.pcmbitwidth, fd_config.channels);
138 }
139
140 void FileSndFile::format_to_asset()
141 {
142 //printf("FileSndFile::format_to_asset 1\n");
143 // User supplies values if PCM
144         if(asset->format == 0)
145         {
146                 asset->byte_order = 0;
147                 asset->signed_ = 1;
148                 switch(fd_config.format & SF_FORMAT_TYPEMASK)
149                 {
150                         case SF_FORMAT_WAV:
151                                 asset->format = FILE_WAV;
152                                 asset->byte_order = 1;
153                                 asset->header = 44;
154                                 break;
155                         case SF_FORMAT_AIFF: asset->format = FILE_AIFF; break;
156                         case SF_FORMAT_AU:   asset->format = FILE_AU;   break;
157                         case SF_FORMAT_RAW:  asset->format = FILE_PCM;  break;
158                         case SF_FORMAT_PAF:  asset->format = FILE_SND;  break;
159                         case SF_FORMAT_SVX:  asset->format = FILE_SND;  break;
160                         case SF_FORMAT_NIST: asset->format = FILE_SND;  break;
161                 }
162
163                 switch(fd_config.format & SF_FORMAT_SUBMASK)
164                 {
165                         case SF_FORMAT_FLOAT:
166                                 asset->bits = BITSFLOAT;
167                                 break;
168                         case SF_FORMAT_ULAW:
169                                 asset->bits = BITSULAW;
170                                 break;
171                         case SF_FORMAT_IMA_ADPCM:
172                         case SF_FORMAT_MS_ADPCM:
173                                 asset->bits = BITS_ADPCM;
174                                 break;
175                         case SF_FORMAT_PCM_16:
176                                 asset->signed_ = 1;
177                                 asset->bits = 16;
178                                 break;
179                         case SF_FORMAT_PCM_24:
180                                 asset->signed_ = 1;
181                                 asset->bits = 24;
182                                 break;
183                         case SF_FORMAT_PCM_32:
184                                 asset->signed_ = 1;
185                                 asset->bits = 32;
186                                 break;
187                         case SF_FORMAT_PCM_S8:
188                                 asset->signed_ = 1;
189                                 asset->bits = BITSLINEAR8;
190                                 break;
191                         case SF_FORMAT_PCM_U8:
192                                 asset->signed_ = 0;
193                                 asset->bits = BITSLINEAR8;
194                                 break;
195                 }
196
197                 switch(fd_config.format & SF_FORMAT_ENDMASK)
198                 {
199                         case SF_ENDIAN_LITTLE:
200                                 asset->byte_order = 1;
201                                 break;
202                         case SF_ENDIAN_BIG:
203                                 asset->byte_order = 0;
204                                 break;
205                 }
206
207                 asset->channels = fd_config.channels;
208         }
209
210         asset->audio_data = 1;
211         asset->audio_length = fd_config.frames;
212         if(!asset->sample_rate)
213                 asset->sample_rate = fd_config.samplerate;
214 //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);
215 //asset->dump();
216 }
217
218 int FileSndFile::open_file(int rd, int wr)
219 {
220         int result = 0;
221
222         if(rd)
223         {
224                 if(asset->format == FILE_PCM)
225                 {
226                         asset_to_format();
227                         fd = sf_open(asset->path, SFM_READ, &fd_config);
228 // Already given by user
229                         if(fd) format_to_asset();
230                 }
231                 else
232                 {
233                         fd = sf_open(asset->path, SFM_READ, &fd_config);
234 // Doesn't calculate the length
235                         if(fd) format_to_asset();
236                 }
237 SET_TRACE
238         }
239         else
240         if(wr)
241         {
242                 asset_to_format();
243                 fd = sf_open(asset->path, SFM_WRITE, &fd_config);
244         }
245
246         if(!fd)
247         {
248                 result = 1;
249                 eprintf("%s", sf_strerror(0));
250         }
251
252         return result;
253 }
254
255 int FileSndFile::close_file()
256 {
257         if(fd) sf_close(fd);
258         fd = 0;
259         FileBase::close_file();
260         fd_config.format = 0;
261         return 0;
262 }
263
264 int FileSndFile::set_audio_position(int64_t sample)
265 {
266 // Commented out /* && psf->dataoffset */ in sndfile.c: 761
267         if(sf_seek(fd, sample, SEEK_SET) < 0)
268         {
269                 eprintf(_("sf_seek() to sample %jd failed, reason: %s\n"), sample, sf_strerror(fd));
270                 sf_perror(fd);
271                 return 1;
272         }
273         return 0;
274 }
275
276 int FileSndFile::read_samples(double *buffer, int64_t len)
277 {
278         int result = 0;
279
280 //printf("FileSndFile::read_samples " _LD " " _LD "\n", file->current_sample, len);
281 // Get temp buffer for interleaved channels
282         if(len <= 0 || len > 1000000)
283                 eprintf("FileSndFile::read_samples len=" _LD "\n", len);
284
285         if(!buffer)
286                 eprintf(_("buffer=%p\n"), buffer);
287
288         if(temp_allocated && temp_allocated < len)
289         {
290                 delete [] temp_double;
291                 temp_double = 0;
292                 temp_allocated = 0;
293         }
294
295         if(!temp_allocated)
296         {
297                 temp_allocated = len;
298                 temp_double = new double[len * asset->channels];
299         }
300
301         result = !sf_read_double(fd, temp_double, len * asset->channels);
302
303         if(result)
304                 eprintf(_("FileSndFile::read_samples fd=%p temp_double=%p"
305                         " len=" _LD " asset=%p asset->channels=%d\n"),
306                         fd, temp_double, len, asset, asset->channels);
307
308 // Extract single channel
309         for(int i = 0, j = file->current_channel;
310                 i < len;
311                 i++, j += asset->channels)
312         {
313                 buffer[i] = temp_double[j];
314         }
315
316         return result;
317 }
318
319 int FileSndFile::write_samples(double **buffer, int64_t len)
320 {
321         int result = 0;
322
323 // Get temp buffer for interleaved channels
324 //printf("FileSndFile::read_samples 1\n");
325         if(temp_allocated && temp_allocated < len)
326         {
327                 temp_allocated = 0;
328                 delete [] temp_double;
329                 temp_double = 0;
330         }
331
332 //printf("FileSndFile::read_samples 2\n");
333         if(!temp_allocated)
334         {
335                 temp_allocated = len;
336                 temp_double = new double[len * asset->channels];
337         }
338
339 // Interleave channels
340         for(int i = 0; i < asset->channels; i++)
341         {
342                 for(int j = 0; j < len; j++)
343                 {
344                         double sample = buffer[i][j];
345 // Libsndfile does not limit values
346 //if(sample > 1.0 || sample < -1.0) printf("FileSndFile::write_samples %f\n", sample);
347 //printf("FileSndFile::write_samples %d %d\n", asset->bits, BITSFLOAT);
348                         if(asset->bits != BITSFLOAT) CLAMP(sample, -1.0, (32767.0 / 32768.0));
349                         temp_double[j * asset->channels + i] = sample;
350                 }
351         }
352
353         result = !sf_writef_double(fd, temp_double, len);
354
355         return result;
356 }
357
358 void FileSndFile::get_parameters(BC_WindowBase *parent_window,
359                 Asset *asset,
360                 BC_WindowBase* &format_window,
361                 int audio_options,
362                 int video_options)
363 {
364         if(audio_options)
365         {
366                 SndFileConfig *window = new SndFileConfig(parent_window, asset);
367                 format_window = window;
368                 window->create_objects();
369                 window->run_window();
370                 delete window;
371         }
372 }
373
374 SndFileConfig::SndFileConfig(BC_WindowBase *parent_window, Asset *asset)
375  : BC_Window(_(PROGRAM_NAME ": Audio Compression"),
376         parent_window->get_abs_cursor_x(1),
377         parent_window->get_abs_cursor_y(1),
378         250,
379         250)
380 {
381         this->parent_window = parent_window;
382         this->asset = asset;
383 }
384
385 SndFileConfig::~SndFileConfig()
386 {
387         if(bits_popup)
388         {
389                 lock_window("SndFileConfig::~SndFileConfig");
390                 delete bits_popup;
391                 unlock_window();
392         }
393 }
394
395 void SndFileConfig::create_objects()
396 {
397         lock_window("SndFileConfig::create_objects()");
398         int x = 10, y = 10;
399
400         bits_popup = 0;
401         switch(asset->format)
402         {
403                 case FILE_WAV:
404                 case FILE_PCM:
405                 case FILE_AIFF:
406                         add_tool(new BC_Title(x, y, _("Compression:")));
407                         y += 25;
408                         if(asset->format == FILE_WAV)
409                                 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 1, 1, 0);
410                         else
411                                 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 0, 0, 0);
412                         y += 40;
413                         bits_popup->create_objects();
414                         break;
415         }
416
417         x = 10;
418         if(asset->format != FILE_AU)
419                 add_subwindow(new BC_CheckBox(x, y, &asset->dither, _("Dither")));
420         y += 30;
421         if(asset->format == FILE_PCM)
422         {
423                 add_subwindow(new BC_CheckBox(x, y, &asset->signed_, _("Signed")));
424                 y += 35;
425                 add_subwindow(new BC_Title(x, y, _("Byte order:")));
426                 add_subwindow(hilo = new SndFileHILO(this, x + 100, y));
427                 add_subwindow(lohi = new SndFileLOHI(this, x + 170, y));
428         }
429         add_subwindow(new BC_OKButton(this));
430         show_window(1);
431         unlock_window();
432 }
433
434 int SndFileConfig::close_event()
435 {
436         set_done(0);
437         return 1;
438 }
439
440
441
442 SndFileHILO::SndFileHILO(SndFileConfig *gui, int x, int y)
443  : BC_Radial(x, y, gui->asset->byte_order == 0, _("Hi Lo"))
444 {
445         this->gui = gui;
446 }
447 int SndFileHILO::handle_event()
448 {
449         gui->asset->byte_order = 0;
450         gui->lohi->update(0);
451         return 1;
452 }
453
454
455
456
457 SndFileLOHI::SndFileLOHI(SndFileConfig *gui, int x, int y)
458  : BC_Radial(x, y, gui->asset->byte_order == 1, _("Lo Hi"))
459 {
460         this->gui = gui;
461 }
462 int SndFileLOHI::handle_event()
463 {
464         gui->asset->byte_order = 1;
465         gui->hilo->update(0);
466         return 1;
467 }
468
469