]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
Remove unneeded msg_Err.
[vlc] / modules / audio_output / file.c
1 /*****************************************************************************
2  * file.c : audio output which writes the samples to a file
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@netcourrier.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_aout.h>
37 #include <vlc_codecs.h> /* WAVEHEADER */
38 #include <vlc_charset.h>
39
40 #define FRAME_SIZE 2048
41 #define A52_FRAME_NB 1536
42
43 /*****************************************************************************
44  * aout_sys_t: audio output method descriptor
45  *****************************************************************************
46  * This structure is part of the audio output thread descriptor.
47  * It describes the direct sound specific properties of an audio device.
48  *****************************************************************************/
49 struct aout_sys_t
50 {
51     FILE     * p_file;
52     bool b_add_wav_header;
53
54     WAVEHEADER waveh;                      /* Wave header of the output file */
55 };
56
57 #define CHANNELS_MAX 6
58 static int pi_channels_maps[CHANNELS_MAX+1] =
59 {
60     0,
61     AOUT_CHAN_CENTER,
62     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
63     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
64     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
65      | AOUT_CHAN_REARRIGHT,
66     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
67      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
68     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
69      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
70 };
71
72 /*****************************************************************************
73  * Local prototypes.
74  *****************************************************************************/
75 static int     Open        ( vlc_object_t * );
76 static void    Close       ( vlc_object_t * );
77 static void    Play        ( aout_instance_t * );
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 #define FORMAT_TEXT N_("Output format")
83 #define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
84     "\"u16_le\", \"s16_le\", \"u16_be\", \"s16_be\", \"fixed32\", " \
85     "\"float32\" or \"spdif\"")
86 #define CHANNELS_TEXT N_("Number of output channels")
87 #define CHANNELS_LONGTEXT N_("By default, all the channels of the incoming " \
88     "will be saved but you can restrict the number of channels here.")
89
90 #define WAV_TEXT N_("Add WAVE header")
91 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
92                         "header to the file.")
93
94 static const char *const format_list[] = { "u8", "s8", "u16", "s16", "u16_le",
95                                      "s16_le", "u16_be", "s16_be", "fixed32",
96                                      "float32", "spdif" };
97 static const int format_int[] = { VLC_FOURCC('u','8',' ',' '),
98                                   VLC_FOURCC('s','8',' ',' '),
99                                   AOUT_FMT_U16_NE, AOUT_FMT_S16_NE,
100                                   VLC_FOURCC('u','1','6','l'),
101                                   VLC_FOURCC('s','1','6','l'),
102                                   VLC_FOURCC('u','1','6','b'),
103                                   VLC_FOURCC('s','1','6','b'),
104                                   VLC_FOURCC('f','i','3','2'),
105                                   VLC_FOURCC('f','l','3','2'),
106                                   VLC_FOURCC('s','p','i','f') };
107
108 #define FILE_TEXT N_("Output file")
109 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
110
111 vlc_module_begin();
112     set_description( N_("File audio output") );
113     set_shortname( N_("File") );
114     set_category( CAT_AUDIO );
115     set_subcategory( SUBCAT_AUDIO_AOUT );
116
117     add_string( "audiofile-format", "s16", NULL,
118                 FORMAT_TEXT, FORMAT_LONGTEXT, true );
119         change_string_list( format_list, 0, 0 );
120     add_integer( "audiofile-channels", 0, NULL,
121                  CHANNELS_TEXT, CHANNELS_LONGTEXT, true );
122     add_file( "audiofile-file", "audiofile.wav", NULL, FILE_TEXT,
123               FILE_LONGTEXT, false );
124         change_unsafe();
125     add_bool( "audiofile-wav", 1, NULL, WAV_TEXT, WAV_LONGTEXT, true );
126
127     set_capability( "audio output", 0 );
128     add_shortcut( "file" );
129     add_shortcut( "audiofile" );
130     set_callbacks( Open, Close );
131 vlc_module_end();
132
133 /*****************************************************************************
134  * Open: open a dummy audio device
135  *****************************************************************************/
136 static int Open( vlc_object_t * p_this )
137 {
138     aout_instance_t * p_aout = (aout_instance_t *)p_this;
139     char * psz_name, * psz_format;
140     const char ** ppsz_compare = format_list;
141     vlc_value_t val;
142     int i_channels, i = 0;
143
144     var_Create( p_this, "audiofile-file", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
145     var_Get( p_this, "audiofile-file", &val );
146     psz_name = val.psz_string;
147     if( !psz_name || !*psz_name )
148     {
149         msg_Err( p_aout, "you need to specify an output file name" );
150         free( psz_name );
151         return VLC_EGENERIC;
152     }
153
154     /* Allocate structure */
155     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
156     if( p_aout->output.p_sys == NULL )
157         return VLC_ENOMEM;
158
159     if( !strcmp( psz_name, "-" ) )
160         p_aout->output.p_sys->p_file = stdout;
161     else
162         p_aout->output.p_sys->p_file = utf8_fopen( psz_name, "wb" );
163
164     free( psz_name );
165     if ( p_aout->output.p_sys->p_file == NULL )
166     {
167         free( p_aout->output.p_sys );
168         return VLC_EGENERIC;
169     }
170
171     p_aout->output.pf_play = Play;
172
173     /* Audio format */
174     var_Create( p_this, "audiofile-format", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
175     var_Get( p_this, "audiofile-format", &val );
176     psz_format = val.psz_string;
177
178     while ( *ppsz_compare != NULL )
179     {
180         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
181         {
182             break;
183         }
184         ppsz_compare++; i++;
185     }
186
187     if ( *ppsz_compare == NULL )
188     {
189         msg_Err( p_aout, "cannot understand the format string (%s)",
190                  psz_format );
191         if( p_aout->output.p_sys->p_file != stdout )
192             fclose( p_aout->output.p_sys->p_file );
193         free( p_aout->output.p_sys );
194         return VLC_EGENERIC;
195     }
196
197     p_aout->output.output.i_format = format_int[i];
198     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
199     {
200         p_aout->output.i_nb_samples = A52_FRAME_NB;
201         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
202         p_aout->output.output.i_frame_length = A52_FRAME_NB;
203         aout_VolumeNoneInit( p_aout );
204     }
205     else
206     {
207         p_aout->output.i_nb_samples = FRAME_SIZE;
208         aout_VolumeSoftInit( p_aout );
209     }
210
211     /* Channels number */
212     var_Create( p_this, "audiofile-channels",
213                 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
214     var_Get( p_this, "audiofile-channels", &val );
215     i_channels = val.i_int;
216
217     if( i_channels > 0 && i_channels <= CHANNELS_MAX )
218     {
219         p_aout->output.output.i_physical_channels =
220             pi_channels_maps[i_channels];
221     }
222
223     /* WAV header */
224     var_Create( p_this, "audiofile-wav", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
225     var_Get( p_this, "audiofile-wav", &val );
226     p_aout->output.p_sys->b_add_wav_header = val.b_bool;
227
228     if( p_aout->output.p_sys->b_add_wav_header )
229     {
230         /* Write wave header */
231         WAVEHEADER *wh = &p_aout->output.p_sys->waveh;
232
233         memset( wh, 0, sizeof(wh) );
234
235         switch( p_aout->output.output.i_format )
236         {
237         case VLC_FOURCC('f','l','3','2'):
238             wh->Format     = WAVE_FORMAT_IEEE_FLOAT;
239             wh->BitsPerSample = sizeof(float) * 8;
240             break;
241         case VLC_FOURCC('u','8',' ',' '):
242             wh->Format     = WAVE_FORMAT_PCM;
243             wh->BitsPerSample = 8;
244             break;
245         case VLC_FOURCC('s','1','6','l'):
246         default:
247             wh->Format     = WAVE_FORMAT_PCM;
248             wh->BitsPerSample = 16;
249             break;
250         }
251
252         wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
253         wh->Length = 0;                    /* temp, to be filled in as we go */
254         wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
255         wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
256         wh->SubChunkLength = 16;
257
258         wh->Modus = aout_FormatNbChannels( &p_aout->output.output );
259         wh->SampleFreq = p_aout->output.output.i_rate;
260         wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
261         wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
262
263         wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
264         wh->DataLength = 0;                /* temp, to be filled in as we go */
265
266         /* Header -> little endian format */
267         SetWLE( &wh->Format, wh->Format );
268         SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
269         SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
270         SetWLE( &wh->Modus, wh->Modus );
271         SetDWLE( &wh->SampleFreq, wh->SampleFreq );
272         SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
273         SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
274
275         if( fwrite( wh, sizeof(WAVEHEADER), 1,
276                     p_aout->output.p_sys->p_file ) != 1 )
277         {
278             msg_Err( p_aout, "write error (%m)" );
279         }
280     }
281
282     return 0;
283 }
284
285 /*****************************************************************************
286  * Close: close our file
287  *****************************************************************************/
288 static void Close( vlc_object_t * p_this )
289 {
290     aout_instance_t * p_aout = (aout_instance_t *)p_this;
291
292     msg_Dbg( p_aout, "closing audio file" );
293
294     if( p_aout->output.p_sys->b_add_wav_header )
295     {
296         /* Update Wave Header */
297         p_aout->output.p_sys->waveh.Length =
298             p_aout->output.p_sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
299
300         /* Write Wave Header */
301         if( fseek( p_aout->output.p_sys->p_file, 0, SEEK_SET ) )
302         {
303             msg_Err( p_aout, "seek error (%m)" );
304         }
305
306         /* Header -> little endian format */
307         SetDWLE( &p_aout->output.p_sys->waveh.Length,
308                  p_aout->output.p_sys->waveh.Length );
309         SetDWLE( &p_aout->output.p_sys->waveh.DataLength,
310                  p_aout->output.p_sys->waveh.DataLength );
311
312         if( fwrite( &p_aout->output.p_sys->waveh, sizeof(WAVEHEADER), 1,
313                     p_aout->output.p_sys->p_file ) != 1 )
314         {
315             msg_Err( p_aout, "write error (%m)" );
316         }
317     }
318
319     if( p_aout->output.p_sys->p_file != stdout )
320         fclose( p_aout->output.p_sys->p_file );
321     free( p_aout->output.p_sys );
322 }
323
324 /*****************************************************************************
325  * Play: pretend to play a sound
326  *****************************************************************************/
327 static void Play( aout_instance_t * p_aout )
328 {
329     aout_buffer_t * p_buffer;
330
331     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
332
333     if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
334                 p_aout->output.p_sys->p_file ) != 1 )
335     {
336         msg_Err( p_aout, "write error (%m)" );
337     }
338
339     if( p_aout->output.p_sys->b_add_wav_header )
340     {
341         /* Update Wave Header */
342         p_aout->output.p_sys->waveh.DataLength += p_buffer->i_nb_bytes;
343     }
344
345     aout_BufferFree( p_buffer );
346 }