1 /*****************************************************************************
2 * file.c : audio output which writes the samples to a file
3 *****************************************************************************
4 * Copyright (C) 2002 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
38 #include <vlc_codecs.h> /* WAVEHEADER */
39 #include <vlc_charset.h>
41 #define FRAME_SIZE 2048
42 #define A52_FRAME_NB 1536
44 /*****************************************************************************
45 * aout_sys_t: audio output method descriptor
46 *****************************************************************************
47 * This structure is part of the audio output thread descriptor.
48 * It describes the direct sound specific properties of an audio device.
49 *****************************************************************************/
53 bool b_add_wav_header;
55 WAVEHEADER waveh; /* Wave header of the output file */
58 #define CHANNELS_MAX 6
59 static const int pi_channels_maps[CHANNELS_MAX+1] =
63 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
64 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
65 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
66 | AOUT_CHAN_REARRIGHT,
67 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
68 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
69 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
70 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Open ( vlc_object_t * );
77 static void Close ( vlc_object_t * );
78 static void Play ( aout_instance_t * );
80 /*****************************************************************************
82 *****************************************************************************/
83 #define FORMAT_TEXT N_("Output format")
84 #define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
85 "\"u16_le\", \"s16_le\", \"u16_be\", \"s16_be\", \"fixed32\", " \
86 "\"float32\" or \"spdif\"")
87 #define CHANNELS_TEXT N_("Number of output channels")
88 #define CHANNELS_LONGTEXT N_("By default, all the channels of the incoming " \
89 "will be saved but you can restrict the number of channels here.")
91 #define WAV_TEXT N_("Add WAVE header")
92 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
93 "header to the file.")
95 static const char *const format_list[] = { "u8", "s8", "u16", "s16", "u16_le",
96 "s16_le", "u16_be", "s16_be", "fixed32",
98 static const int format_int[] = { VLC_CODEC_U8,
100 VLC_CODEC_U16N, VLC_CODEC_S16N,
109 #define FILE_TEXT N_("Output file")
110 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
113 set_description( N_("File audio output") )
114 set_shortname( N_("File") )
115 set_category( CAT_AUDIO )
116 set_subcategory( SUBCAT_AUDIO_AOUT )
118 add_string( "audiofile-format", "s16", NULL,
119 FORMAT_TEXT, FORMAT_LONGTEXT, true )
120 change_string_list( format_list, 0, 0 )
121 add_integer( "audiofile-channels", 0, NULL,
122 CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
123 add_file( "audiofile-file", "audiofile.wav", NULL, FILE_TEXT,
124 FILE_LONGTEXT, false )
125 add_bool( "audiofile-wav", true, NULL, WAV_TEXT, WAV_LONGTEXT, true )
127 set_capability( "audio output", 0 )
128 add_shortcut( "file" )
129 add_shortcut( "audiofile" )
130 set_callbacks( Open, Close )
133 /*****************************************************************************
134 * Open: open a dummy audio device
135 *****************************************************************************/
136 static int Open( vlc_object_t * p_this )
138 aout_instance_t * p_aout = (aout_instance_t *)p_this;
139 char * psz_name, * psz_format;
140 const char * const * ppsz_compare = format_list;
141 int i_channels, i = 0;
143 psz_name = var_CreateGetString( p_this, "audiofile-file" );
144 if( !psz_name || !*psz_name )
146 msg_Err( p_aout, "you need to specify an output file name" );
151 /* Allocate structure */
152 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
153 if( p_aout->output.p_sys == NULL )
156 if( !strcmp( psz_name, "-" ) )
157 p_aout->output.p_sys->p_file = stdout;
159 p_aout->output.p_sys->p_file = utf8_fopen( psz_name, "wb" );
162 if ( p_aout->output.p_sys->p_file == NULL )
164 free( p_aout->output.p_sys );
168 p_aout->output.pf_play = Play;
171 psz_format = var_CreateGetString( p_this, "audiofile-format" );
173 while ( *ppsz_compare != NULL )
175 if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
182 if ( *ppsz_compare == NULL )
184 msg_Err( p_aout, "cannot understand the format string (%s)",
186 if( p_aout->output.p_sys->p_file != stdout )
187 fclose( p_aout->output.p_sys->p_file );
188 free( p_aout->output.p_sys );
194 p_aout->output.output.i_format = format_int[i];
195 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
197 p_aout->output.i_nb_samples = A52_FRAME_NB;
198 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
199 p_aout->output.output.i_frame_length = A52_FRAME_NB;
200 aout_VolumeNoneInit( p_aout );
204 p_aout->output.i_nb_samples = FRAME_SIZE;
205 aout_VolumeSoftInit( p_aout );
208 /* Channels number */
209 i_channels = var_CreateGetInteger( p_this, "audiofile-channels" );
211 if( i_channels > 0 && i_channels <= CHANNELS_MAX )
213 p_aout->output.output.i_physical_channels =
214 pi_channels_maps[i_channels];
218 p_aout->output.p_sys->b_add_wav_header = var_CreateGetBool( p_this,
221 if( p_aout->output.p_sys->b_add_wav_header )
223 /* Write wave header */
224 WAVEHEADER *wh = &p_aout->output.p_sys->waveh;
226 memset( wh, 0, sizeof(wh) );
228 switch( p_aout->output.output.i_format )
231 wh->Format = WAVE_FORMAT_IEEE_FLOAT;
232 wh->BitsPerSample = sizeof(float) * 8;
235 wh->Format = WAVE_FORMAT_PCM;
236 wh->BitsPerSample = 8;
240 wh->Format = WAVE_FORMAT_PCM;
241 wh->BitsPerSample = 16;
245 wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
246 wh->Length = 0; /* temp, to be filled in as we go */
247 wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
248 wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
249 wh->SubChunkLength = 16;
251 wh->Modus = aout_FormatNbChannels( &p_aout->output.output );
252 wh->SampleFreq = p_aout->output.output.i_rate;
253 wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
254 wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
256 wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
257 wh->DataLength = 0; /* temp, to be filled in as we go */
259 /* Header -> little endian format */
260 SetWLE( &wh->Format, wh->Format );
261 SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
262 SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
263 SetWLE( &wh->Modus, wh->Modus );
264 SetDWLE( &wh->SampleFreq, wh->SampleFreq );
265 SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
266 SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
268 if( fwrite( wh, sizeof(WAVEHEADER), 1,
269 p_aout->output.p_sys->p_file ) != 1 )
271 msg_Err( p_aout, "write error (%m)" );
278 /*****************************************************************************
279 * Close: close our file
280 *****************************************************************************/
281 static void Close( vlc_object_t * p_this )
283 aout_instance_t * p_aout = (aout_instance_t *)p_this;
285 msg_Dbg( p_aout, "closing audio file" );
287 if( p_aout->output.p_sys->b_add_wav_header )
289 /* Update Wave Header */
290 p_aout->output.p_sys->waveh.Length =
291 p_aout->output.p_sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
293 /* Write Wave Header */
294 if( fseek( p_aout->output.p_sys->p_file, 0, SEEK_SET ) )
296 msg_Err( p_aout, "seek error (%m)" );
299 /* Header -> little endian format */
300 SetDWLE( &p_aout->output.p_sys->waveh.Length,
301 p_aout->output.p_sys->waveh.Length );
302 SetDWLE( &p_aout->output.p_sys->waveh.DataLength,
303 p_aout->output.p_sys->waveh.DataLength );
305 if( fwrite( &p_aout->output.p_sys->waveh, sizeof(WAVEHEADER), 1,
306 p_aout->output.p_sys->p_file ) != 1 )
308 msg_Err( p_aout, "write error (%m)" );
312 if( p_aout->output.p_sys->p_file != stdout )
313 fclose( p_aout->output.p_sys->p_file );
314 free( p_aout->output.p_sys );
317 /*****************************************************************************
318 * Play: pretend to play a sound
319 *****************************************************************************/
320 static void Play( aout_instance_t * p_aout )
322 aout_buffer_t * p_buffer;
324 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
326 if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
327 p_aout->output.p_sys->p_file ) != 1 )
329 msg_Err( p_aout, "write error (%m)" );
332 if( p_aout->output.p_sys->b_add_wav_header )
334 /* Update Wave Header */
335 p_aout->output.p_sys->waveh.DataLength += p_buffer->i_nb_bytes;
338 aout_BufferFree( p_buffer );