r802: Remove renderframfsclient and renderfarmfsserver .h and .C from Makefile.am...
[cinelerra_cv/mob.git] / cinelerra / filesndfile.C
blob7cd104c4208bab6211dee480df1c0dd78752f7a8
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"
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->signed_)
57                                 fd_config.format |= SF_FORMAT_PCM_S8;
58                         else
59                                 fd_config.format |= SF_FORMAT_PCM_U8;
60                         break;
62                 case BITSLINEAR16:
63                         fd_config.format |= SF_FORMAT_PCM_16;
65                         if(asset->byte_order || asset->format == FILE_WAV)
66                                 fd_config.format |= SF_ENDIAN_LITTLE;
67                         else
68                                 fd_config.format |= SF_ENDIAN_BIG;
69                         break;
71                 case BITSLINEAR24:
72                         fd_config.format |= SF_FORMAT_PCM_24;
74                         if(asset->byte_order || asset->format == FILE_WAV)
75                                 fd_config.format |= SF_ENDIAN_LITTLE;
76                         else
77                                 fd_config.format |= SF_ENDIAN_BIG;
78                         break;
80                 case BITSLINEAR32:
81                         fd_config.format |= SF_FORMAT_PCM_32;
83                         if(asset->byte_order || asset->format == FILE_WAV)
84                                 fd_config.format |= SF_ENDIAN_LITTLE;
85                         else
86                                 fd_config.format |= SF_ENDIAN_BIG;
87                         break;
89                 case BITSULAW: 
90                         fd_config.format |= SF_FORMAT_ULAW; 
91                         break;
93                 case BITSFLOAT: 
94                         fd_config.format |= SF_FORMAT_FLOAT; 
95                         break;
97                 case BITS_ADPCM: 
98                         if(fd_config.format == FILE_WAV)
99                                 fd_config.format |= SF_FORMAT_MS_ADPCM;
100                         else
101                                 fd_config.format |= SF_FORMAT_IMA_ADPCM; 
102                         fd_config.format |= SF_FORMAT_PCM_16;
103                         break;
104         }
106         fd_config.seekable = 1;
107         fd_config.samplerate = asset->sample_rate;
108         fd_config.channels  = asset->channels;
109 //printf("FileSndFile::asset_to_format %x %d %d\n", fd_config.format, fd_config.pcmbitwidth, fd_config.channels);
112 void FileSndFile::format_to_asset()
114 //printf("FileSndFile::format_to_asset 1\n");
115 // User supplies values if PCM
116         if(asset->format == 0)
117         {
118                 asset->byte_order = 0;
119                 asset->signed_ = 1;
120                 switch(fd_config.format & SF_FORMAT_TYPEMASK)
121                 {
122                         case SF_FORMAT_WAV:  
123                                 asset->format = FILE_WAV;  
124                                 asset->byte_order = 1;
125                                 asset->header = 44;
126                                 break;
127                         case SF_FORMAT_AIFF: asset->format = FILE_AIFF; break;
128                         case SF_FORMAT_AU:   asset->format = FILE_AU;   break;
129                         case SF_FORMAT_RAW:  asset->format = FILE_PCM;  break;
130                         case SF_FORMAT_PAF:  asset->format = FILE_SND;  break;
131                         case SF_FORMAT_SVX:  asset->format = FILE_SND;  break;
132                         case SF_FORMAT_NIST: asset->format = FILE_SND;  break;
133                 }
135                 switch(fd_config.format & SF_FORMAT_SUBMASK)
136                 {
137                         case SF_FORMAT_FLOAT: 
138                                 asset->bits = BITSFLOAT; 
139                                 break;
140                         case SF_FORMAT_ULAW: 
141                                 asset->bits = BITSULAW; 
142                                 break;
143                         case SF_FORMAT_IMA_ADPCM:
144                         case SF_FORMAT_MS_ADPCM:
145                                 asset->bits = BITS_ADPCM;
146                                 break;
147                         case SF_FORMAT_PCM_16:
148                                 asset->signed_ = 1;
149                                 asset->bits = 16;
150                                 break;
151                         case SF_FORMAT_PCM_24:
152                                 asset->signed_ = 1;
153                                 asset->bits = 24;
154                                 break;
155                         case SF_FORMAT_PCM_32:
156                                 asset->signed_ = 1;
157                                 asset->bits = 32;
158                                 break;
159                         case SF_FORMAT_PCM_S8:
160                                 asset->signed_ = 1;
161                                 asset->bits = BITSLINEAR8;
162                                 break;
163                         case SF_FORMAT_PCM_U8:
164                                 asset->signed_ = 0;
165                                 asset->bits = BITSLINEAR8;
166                                 break;
167                 }
169                 switch(fd_config.format & SF_FORMAT_ENDMASK)
170                 {
171                         case SF_ENDIAN_LITTLE:
172                                 asset->byte_order = 1;
173                                 break;
174                         case SF_ENDIAN_BIG:
175                                 asset->byte_order = 0;
176                                 break;
177                 }
179                 asset->channels = fd_config.channels;
180         }
182         asset->audio_data = 1;
183         asset->audio_length = fd_config.frames;
184         if(!asset->sample_rate)
185                 asset->sample_rate = fd_config.samplerate;
186 //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);
187 //asset->dump();
190 int FileSndFile::open_file(int rd, int wr)
192         int result = 0;
193         this->rd = rd;
194         this->wr = wr;
196         if(rd)
197         {
198                 if(asset->format == FILE_PCM)
199                 {
200                         asset_to_format();
201                         fd = sf_open(asset->path, SFM_READ, &fd_config);
202 // Already given by user
203                         if(fd) format_to_asset();
204                 }
205                 else
206                 {
207                         fd = sf_open(asset->path, SFM_READ, &fd_config);
208 // Doesn't calculate the length
209                         if(fd) format_to_asset();
210                 }
211 SET_TRACE
212         }
213         else
214         if(wr)
215         {
216                 asset_to_format();
217                 fd = sf_open(asset->path, SFM_WRITE, &fd_config);
218         }
220         if(!fd) 
221         {
222                 result = 1;
223                 printf("FileSndFile::open_file: ");
224                 sf_perror(0);
225         }
227         return result;
230 int FileSndFile::close_file()
232         if(fd) sf_close(fd);
233         fd = 0;
234         FileBase::close_file();
235         fd_config.format = 0;
236         return 0;
239 int FileSndFile::set_audio_position(int64_t sample)
241 // Commented out /* && psf->dataoffset */ in sndfile.c: 761
242         if(sf_seek(fd, sample, SEEK_SET) < 0)
243         {
244                 printf("FileSndFile::set_audio_position %lld: failed\n", sample);
245                 sf_perror(fd);
246                 return 1;
247         }
248         return 0;
251 int FileSndFile::read_samples(double *buffer, int64_t len)
253         int result = 0;
255 //printf("FileSndFile::read_samples %lld %lld\n", file->current_sample, len);
256 // Get temp buffer for interleaved channels
257         if(len <= 0 || len > 1000000)
258                 printf("FileSndFile::read_samples len=%d\n", len);
260         if(!buffer)
261                 printf("FileSndFile::read_samples buffer=%p\n", buffer);
263         if(temp_allocated && temp_allocated < len)
264         {
265                 delete [] temp_double;
266                 temp_double = 0;
267                 temp_allocated = 0;
268         }
270         if(!temp_allocated)
271         {
272                 temp_allocated = len;
273                 temp_double = new double[len * asset->channels];
274         }
276         result = !sf_read_double(fd, temp_double, len * asset->channels);
278         if(result)
279                 printf("FileSndFile::read_samples fd=%p temp_double=%p len=%d asset=%p asset->channels=%d\n",
280                         fd, temp_double, len, asset, asset->channels);
282 // Extract single channel
283         for(int i = 0, j = file->current_channel; 
284                 i < len;
285                 i++, j += asset->channels)
286         {
287                 buffer[i] = temp_double[j];
288         }
290         return result;
293 int FileSndFile::write_samples(double **buffer, int64_t len)
295         int result = 0;
297 // Get temp buffer for interleaved channels
298 //printf("FileSndFile::read_samples 1\n");
299         if(temp_allocated && temp_allocated < len)
300         {
301                 temp_allocated = 0;
302                 delete [] temp_double;
303                 temp_double = 0;
304         }
306 //printf("FileSndFile::read_samples 2\n");
307         if(!temp_allocated)
308         {
309                 temp_allocated = len;
310                 temp_double = new double[len * asset->channels];
311         }
313 // Interleave channels
314         for(int i = 0; i < asset->channels; i++)
315         {
316                 for(int j = 0; j < len; j++)
317                 {
318                         double sample = buffer[i][j];
319 // Libsndfile does not limit values
320 //if(sample > 1.0 || sample < -1.0) printf("FileSndFile::write_samples %f\n", sample);
321 //printf("FileSndFile::write_samples %d %d\n", asset->bits, BITSFLOAT);
322                         if(asset->bits != BITSFLOAT) CLAMP(sample, -1.0, (32767.0 / 32768.0));
323                         temp_double[j * asset->channels + i] = sample;
324                 }
325         }
327         result = !sf_writef_double(fd, temp_double, len);
329         return result;
332 void FileSndFile::get_parameters(BC_WindowBase *parent_window, 
333                 Asset *asset, 
334                 BC_WindowBase* &format_window,
335                 int audio_options,
336                 int video_options)
338         if(audio_options)
339         {
340                 SndFileConfig *window = new SndFileConfig(parent_window, asset);
341                 format_window = window;
342                 window->create_objects();
343                 window->run_window();
344                 delete window;
345         }
348 SndFileConfig::SndFileConfig(BC_WindowBase *parent_window, Asset *asset)
349  : BC_Window(PROGRAM_NAME ": Audio Compression",
350         parent_window->get_abs_cursor_x(1),
351         parent_window->get_abs_cursor_y(1),
352         250,
353         250)
355         this->parent_window = parent_window;
356         this->asset = asset;
359 SndFileConfig::~SndFileConfig()
361         if(bits_popup) delete bits_popup;
363 int SndFileConfig::create_objects()
365         int x = 10, y = 10;
367         bits_popup = 0;
368         switch(asset->format)
369         {
370                 case FILE_WAV:
371                 case FILE_PCM:
372                 case FILE_AIFF:
373                         add_tool(new BC_Title(x, y, _("Compression:")));
374                         y += 25;
375                         if(asset->format == FILE_WAV)
376                                 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 1, 1, 0);
377                         else
378                                 bits_popup = new BitsPopup(this, x, y, &asset->bits, 0, 0, 0, 0, 0);
379                         y += 40;
380                         bits_popup->create_objects();
381                         break;
382         }
384         x = 10;
385         if(asset->format != FILE_AU)
386                 add_subwindow(new BC_CheckBox(x, y, &asset->dither, _("Dither")));
387         y += 30;
388         if(asset->format == FILE_PCM)
389         {
390                 add_subwindow(new BC_CheckBox(x, y, &asset->signed_, _("Signed")));
391                 y += 35;
392                 add_subwindow(new BC_Title(x, y, _("Byte order:")));
393                 add_subwindow(hilo = new SndFileHILO(this, x + 100, y));
394                 add_subwindow(lohi = new SndFileLOHI(this, x + 170, y));
395         }
396         add_subwindow(new BC_OKButton(this));
397         return 0;
400 int SndFileConfig::close_event()
402         set_done(0);
403         return 1;
408 SndFileHILO::SndFileHILO(SndFileConfig *gui, int x, int y)
409  : BC_Radial(x, y, gui->asset->byte_order == 0, _("Hi Lo"))
411         this->gui = gui;
413 int SndFileHILO::handle_event()
415         gui->asset->byte_order = 0;
416         gui->lohi->update(0);
417         return 1;
423 SndFileLOHI::SndFileLOHI(SndFileConfig *gui, int x, int y)
424  : BC_Radial(x, y, gui->asset->byte_order == 1, _("Lo Hi"))
426         this->gui = gui;
428 int SndFileLOHI::handle_event()
430         gui->asset->byte_order = 1;
431         gui->hilo->update(0);
432         return 1;