r1006: configure: Use libx264_pic instead of libx264 if available.
[cinelerra_cv/mob.git] / cinelerra / filesndfile.C
blob32322e4cbd47cef045164fe877b27153630223ea
1 #include "asset.h"
2 #include "assets.h"
3 #include "bcsignals.h"
4 #include "bitspopup.h"
5 #include "clip.h"
6 #include "file.h"
7 #include "filesndfile.h"
8 #include "language.h"
9 #include "mwindow.inc"
10 #include "mainerror.h"
12 FileSndFile::FileSndFile(Asset *asset, File *file)
13  : FileBase(asset, file)
15         temp_double = 0;
16         temp_allocated = 0;
17         fd_config.format = 0;
18         fd = 0;
21 FileSndFile::~FileSndFile()
23         if(temp_double) delete [] temp_double;
26 int FileSndFile::check_sig(Asset *asset)
28         int result = 0;
29         SF_INFO fd_config;
30         fd_config.format = 0;
31         SNDFILE *fd = sf_open(asset->path, SFM_READ, &fd_config);
32         if(fd)
33         {
34                 sf_close(fd);
35                 result = 1;
36         }
38         return result;
41 void FileSndFile::asset_to_format()
43         switch(asset->format)
44         {
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;
49         }
51 // Not all of these are allowed in all sound formats.
52 // Raw can't be float.
53         switch(asset->bits)
54         {
55                 case BITSLINEAR8:
56                         if(asset->format == FILE_WAV)
57                                 fd_config.format |= SF_FORMAT_PCM_U8;
58                         else
59                         if(asset->signed_)
60                                 fd_config.format |= SF_FORMAT_PCM_S8;
61                         else
62                                 fd_config.format |= SF_FORMAT_PCM_U8;
63                         break;
65                 case BITSLINEAR16:
66                         fd_config.format |= SF_FORMAT_PCM_16;
68                         if(asset->byte_order || asset->format == FILE_WAV)
69                                 fd_config.format |= SF_ENDIAN_LITTLE;
70                         else
71                                 fd_config.format |= SF_ENDIAN_BIG;
72                         break;
74                 case BITSLINEAR24:
75                         fd_config.format |= SF_FORMAT_PCM_24;
77                         if(asset->byte_order || asset->format == FILE_WAV)
78                                 fd_config.format |= SF_ENDIAN_LITTLE;
79                         else
80                                 fd_config.format |= SF_ENDIAN_BIG;
81                         break;
83                 case BITSLINEAR32:
84                         fd_config.format |= SF_FORMAT_PCM_32;
86                         if(asset->byte_order || asset->format == FILE_WAV)
87                                 fd_config.format |= SF_ENDIAN_LITTLE;
88                         else
89                                 fd_config.format |= SF_ENDIAN_BIG;
90                         break;
92                 case BITSULAW: 
93                         fd_config.format |= SF_FORMAT_ULAW; 
94                         break;
96                 case BITSFLOAT: 
97                         fd_config.format |= SF_FORMAT_FLOAT; 
98                         break;
100                 case BITS_ADPCM: 
101                         if(fd_config.format == FILE_WAV)
102                                 fd_config.format |= SF_FORMAT_MS_ADPCM;
103                         else
104                                 fd_config.format |= SF_FORMAT_IMA_ADPCM; 
105                         fd_config.format |= SF_FORMAT_PCM_16;
106                         break;
107         }
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)
120         {
121                 asset->byte_order = 0;
122                 asset->signed_ = 1;
123                 switch(fd_config.format & SF_FORMAT_TYPEMASK)
124                 {
125                         case SF_FORMAT_WAV:  
126                                 asset->format = FILE_WAV;  
127                                 asset->byte_order = 1;
128                                 asset->header = 44;
129                                 break;
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;
136                 }
138                 switch(fd_config.format & SF_FORMAT_SUBMASK)
139                 {
140                         case SF_FORMAT_FLOAT: 
141                                 asset->bits = BITSFLOAT; 
142                                 break;
143                         case SF_FORMAT_ULAW: 
144                                 asset->bits = BITSULAW; 
145                                 break;
146                         case SF_FORMAT_IMA_ADPCM:
147                         case SF_FORMAT_MS_ADPCM:
148                                 asset->bits = BITS_ADPCM;
149                                 break;
150                         case SF_FORMAT_PCM_16:
151                                 asset->signed_ = 1;
152                                 asset->bits = 16;
153                                 break;
154                         case SF_FORMAT_PCM_24:
155                                 asset->signed_ = 1;
156                                 asset->bits = 24;
157                                 break;
158                         case SF_FORMAT_PCM_32:
159                                 asset->signed_ = 1;
160                                 asset->bits = 32;
161                                 break;
162                         case SF_FORMAT_PCM_S8:
163                                 asset->signed_ = 1;
164                                 asset->bits = BITSLINEAR8;
165                                 break;
166                         case SF_FORMAT_PCM_U8:
167                                 asset->signed_ = 0;
168                                 asset->bits = BITSLINEAR8;
169                                 break;
170                 }
172                 switch(fd_config.format & SF_FORMAT_ENDMASK)
173                 {
174                         case SF_ENDIAN_LITTLE:
175                                 asset->byte_order = 1;
176                                 break;
177                         case SF_ENDIAN_BIG:
178                                 asset->byte_order = 0;
179                                 break;
180                 }
182                 asset->channels = fd_config.channels;
183         }
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);
190 //asset->dump();
193 int FileSndFile::open_file(int rd, int wr)
195         int result = 0;
196         this->rd = rd;
197         this->wr = wr;
199         if(rd)
200         {
201                 if(asset->format == FILE_PCM)
202                 {
203                         asset_to_format();
204                         fd = sf_open(asset->path, SFM_READ, &fd_config);
205 // Already given by user
206                         if(fd) format_to_asset();
207                 }
208                 else
209                 {
210                         fd = sf_open(asset->path, SFM_READ, &fd_config);
211 // Doesn't calculate the length
212                         if(fd) format_to_asset();
213                 }
214 SET_TRACE
215         }
216         else
217         if(wr)
218         {
219 printf("FileSNDFile::open 1\n");
220                 asset_to_format();
221 printf("FileSNDFile::open 1\n");
222                 fd = sf_open(asset->path, SFM_WRITE, &fd_config);
223 printf("FileSNDFile::open 10 %p\n", fd);
224         }
226         if(!fd) 
227         {
228                 result = 1;
229                 eprintf("%s", sf_strerror(0));
230         }
232         return result;
235 int FileSndFile::close_file()
237         if(fd) sf_close(fd);
238         fd = 0;
239         FileBase::close_file();
240         fd_config.format = 0;
241         return 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)
248         {
249                 eprintf("sf_seek() to sample %lld failed, reason: %s\n", sample, sf_strerror(fd));
250                 return 1;
251         }
252         return 0;
255 int FileSndFile::read_samples(double *buffer, int64_t len)
257         int result = 0;
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);
264         if(!buffer)
265                 eprintf("buffer=%p\n", buffer);
267         if(temp_allocated && temp_allocated < len)
268         {
269                 delete [] temp_double;
270                 temp_double = 0;
271                 temp_allocated = 0;
272         }
274         if(!temp_allocated)
275         {
276                 temp_allocated = len;
277                 temp_double = new double[len * asset->channels];
278         }
280         result = !sf_read_double(fd, temp_double, len * asset->channels);
282         if(result)
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; 
288                 i < len;
289                 i++, j += asset->channels)
290         {
291                 buffer[i] = temp_double[j];
292         }
294         return result;
297 int FileSndFile::write_samples(double **buffer, int64_t len)
299         int result = 0;
301 // Get temp buffer for interleaved channels
302 //printf("FileSndFile::read_samples 1\n");
303         if(temp_allocated && temp_allocated < len)
304         {
305                 temp_allocated = 0;
306                 delete [] temp_double;
307                 temp_double = 0;
308         }
310 //printf("FileSndFile::read_samples 2\n");
311         if(!temp_allocated)
312         {
313                 temp_allocated = len;
314                 temp_double = new double[len * asset->channels];
315         }
317 // Interleave channels
318         for(int i = 0; i < asset->channels; i++)
319         {
320                 for(int j = 0; j < len; j++)
321                 {
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;
328                 }
329         }
331         result = !sf_writef_double(fd, temp_double, len);
333         return result;
336 void FileSndFile::get_parameters(BC_WindowBase *parent_window, 
337                 Asset *asset, 
338                 BC_WindowBase* &format_window,
339                 int audio_options,
340                 int video_options)
342         if(audio_options)
343         {
344                 SndFileConfig *window = new SndFileConfig(parent_window, asset);
345                 format_window = window;
346                 window->create_objects();
347                 window->run_window();
348                 delete window;
349         }
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),
356         250,
357         250)
359         this->parent_window = parent_window;
360         this->asset = asset;
363 SndFileConfig::~SndFileConfig()
365         if(bits_popup) delete bits_popup;
367 int SndFileConfig::create_objects()
369         int x = 10, y = 10;
371         bits_popup = 0;
372         switch(asset->format)
373         {
374                 case FILE_WAV:
375                 case FILE_PCM:
376                 case FILE_AIFF:
377                         add_tool(new BC_Title(x, y, _("Compression:")));
378                         y += 25;
379                         if(asset->format == FILE_WAV)
380                                 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 1, 1, 0);
381                         else
382                                 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 0, 0, 0);
383                         y += 40;
384                         bits_popup->create_objects();
385                         break;
386         }
388         x = 10;
389         if(asset->format != FILE_AU)
390                 add_subwindow(new BC_CheckBox(x, y, &asset->dither, _("Dither")));
391         y += 30;
392         if(asset->format == FILE_PCM)
393         {
394                 add_subwindow(new BC_CheckBox(x, y, &asset->signed_, _("Signed")));
395                 y += 35;
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));
399         }
400         add_subwindow(new BC_OKButton(this));
401         return 0;
404 int SndFileConfig::close_event()
406         set_done(0);
407         return 1;
412 SndFileHILO::SndFileHILO(SndFileConfig *gui, int x, int y)
413  : BC_Radial(x, y, gui->asset->byte_order == 0, _("Hi Lo"))
415         this->gui = gui;
417 int SndFileHILO::handle_event()
419         gui->asset->byte_order = 0;
420         gui->lohi->update(0);
421         return 1;
427 SndFileLOHI::SndFileLOHI(SndFileConfig *gui, int x, int y)
428  : BC_Radial(x, y, gui->asset->byte_order == 1, _("Lo Hi"))
430         this->gui = gui;
432 int SndFileLOHI::handle_event()
434         gui->asset->byte_order = 1;
435         gui->hilo->update(0);
436         return 1;