1 /*****************************************************************************
2 * file.c : audio output which writes the samples to a file
3 *****************************************************************************
4 * Copyright (C) 2002 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
36 #include <vlc_codecs.h> /* WAVEHEADER */
39 #define A52_FRAME_NB 1536
41 /*****************************************************************************
42 * aout_sys_t: audio output method descriptor
43 *****************************************************************************
44 * This structure is part of the audio output thread descriptor.
45 * It describes the direct sound specific properties of an audio device.
46 *****************************************************************************/
50 bool b_add_wav_header;
52 WAVEHEADER waveh; /* Wave header of the output file */
55 #define CHANNELS_MAX 6
56 static const int pi_channels_maps[CHANNELS_MAX+1] =
60 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
61 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
62 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
63 | AOUT_CHAN_REARRIGHT,
64 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
65 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
66 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
67 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
70 /*****************************************************************************
72 *****************************************************************************/
73 static int Open ( vlc_object_t * );
74 static void Play ( audio_output_t *, block_t * );
75 static void Flush ( audio_output_t *, bool );
77 /*****************************************************************************
79 *****************************************************************************/
80 #define FORMAT_TEXT N_("Output format")
82 #define CHANNELS_TEXT N_("Number of output channels")
83 #define CHANNELS_LONGTEXT N_("By default (0), all the channels of the incoming " \
84 "will be saved but you can restrict the number of channels here.")
86 #define WAV_TEXT N_("Add WAVE header")
87 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
88 "header to the file.")
90 static const char *const format_list[] = {
92 #ifndef WORDS_BIGENDIAN
97 static const int format_int[] = {
98 VLC_CODEC_U8, VLC_CODEC_S16N,
99 #ifndef WORDS_BIGENDIAN
105 #define FILE_TEXT N_("Output file")
106 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
109 set_description( N_("File audio output") )
110 set_shortname( N_("File") )
111 set_category( CAT_AUDIO )
112 set_subcategory( SUBCAT_AUDIO_AOUT )
114 add_savefile( "audiofile-file", "audiofile.wav", FILE_TEXT,
115 FILE_LONGTEXT, false )
116 add_string( "audiofile-format", "s16",
117 FORMAT_TEXT, FORMAT_TEXT, true )
118 change_string_list( format_list, format_list )
119 add_integer( "audiofile-channels", 0,
120 CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
121 change_integer_range( 0, 6 )
122 add_bool( "audiofile-wav", true, WAV_TEXT, WAV_LONGTEXT, true )
124 set_capability( "audio output", 0 )
125 add_shortcut( "file", "audiofile" )
126 set_callbacks( Open, NULL )
129 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
131 char * psz_name, * psz_format;
132 const char * const * ppsz_compare = format_list;
133 int i_channels, i = 0;
135 psz_name = var_InheritString( p_aout, "audiofile-file" );
138 msg_Err( p_aout, "you need to specify an output file name" );
143 /* Allocate structure */
144 p_aout->sys = malloc( sizeof( aout_sys_t ) );
145 if( p_aout->sys == NULL )
148 if( !strcmp( psz_name, "-" ) )
149 p_aout->sys->p_file = stdout;
151 p_aout->sys->p_file = vlc_fopen( psz_name, "wb" );
154 if ( p_aout->sys->p_file == NULL )
160 p_aout->time_get = NULL;
162 p_aout->pause = NULL;
163 p_aout->flush = Flush;
166 psz_format = var_InheritString( p_aout, "audiofile-format" );
167 if ( !psz_format ) abort(); /* FIXME */
169 while ( *ppsz_compare != NULL )
171 if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
178 if ( *ppsz_compare == NULL )
180 msg_Err( p_aout, "cannot understand the format string (%s)",
182 if( p_aout->sys->p_file != stdout )
183 fclose( p_aout->sys->p_file );
190 fmt->i_format = format_int[i];
191 if ( AOUT_FMT_SPDIF( fmt ) )
193 fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
194 fmt->i_frame_length = A52_FRAME_NB;
197 /* Channels number */
198 i_channels = var_InheritInteger( p_aout, "audiofile-channels" );
199 if( i_channels > 0 && i_channels <= CHANNELS_MAX )
201 fmt->i_physical_channels = pi_channels_maps[i_channels];
205 p_aout->sys->b_add_wav_header = var_InheritBool( p_aout, "audiofile-wav" );
206 if( p_aout->sys->b_add_wav_header )
208 /* Write wave header */
209 WAVEHEADER *wh = &p_aout->sys->waveh;
211 memset( wh, 0, sizeof(*wh) );
213 switch( fmt->i_format )
215 #ifndef WORDS_BIGENDIAN
217 wh->Format = WAVE_FORMAT_IEEE_FLOAT;
218 wh->BitsPerSample = sizeof(float) * 8;
222 wh->Format = WAVE_FORMAT_PCM;
223 wh->BitsPerSample = 8;
226 wh->Format = WAVE_FORMAT_PCM;
227 wh->BitsPerSample = 16;
231 wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
232 wh->Length = 0; /* temp, to be filled in as we go */
233 wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
234 wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
235 wh->SubChunkLength = 16;
237 wh->Modus = aout_FormatNbChannels( fmt );
238 wh->SampleFreq = fmt->i_rate;
239 wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
240 wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
242 wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
243 wh->DataLength = 0; /* temp, to be filled in as we go */
245 /* Header -> little endian format */
246 SetWLE( &wh->Format, wh->Format );
247 SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
248 SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
249 SetWLE( &wh->Modus, wh->Modus );
250 SetDWLE( &wh->SampleFreq, wh->SampleFreq );
251 SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
252 SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
254 if( fwrite( wh, sizeof(WAVEHEADER), 1,
255 p_aout->sys->p_file ) != 1 )
257 msg_Err( p_aout, "write error (%m)" );
264 /*****************************************************************************
265 * Close: close our file
266 *****************************************************************************/
267 static void Stop( audio_output_t *p_aout )
269 msg_Dbg( p_aout, "closing audio file" );
271 if( p_aout->sys->b_add_wav_header )
273 /* Update Wave Header */
274 p_aout->sys->waveh.Length =
275 p_aout->sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
277 /* Write Wave Header */
278 if( fseek( p_aout->sys->p_file, 0, SEEK_SET ) )
280 msg_Err( p_aout, "seek error (%m)" );
283 /* Header -> little endian format */
284 SetDWLE( &p_aout->sys->waveh.Length,
285 p_aout->sys->waveh.Length );
286 SetDWLE( &p_aout->sys->waveh.DataLength,
287 p_aout->sys->waveh.DataLength );
289 if( fwrite( &p_aout->sys->waveh, sizeof(WAVEHEADER), 1,
290 p_aout->sys->p_file ) != 1 )
292 msg_Err( p_aout, "write error (%m)" );
296 if( p_aout->sys->p_file != stdout )
297 fclose( p_aout->sys->p_file );
301 /*****************************************************************************
302 * Play: pretend to play a sound
303 *****************************************************************************/
304 static void Play( audio_output_t * p_aout, block_t *p_buffer )
306 if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
307 p_aout->sys->p_file ) != 1 )
309 msg_Err( p_aout, "write error (%m)" );
312 if( p_aout->sys->b_add_wav_header )
314 /* Update Wave Header */
315 p_aout->sys->waveh.DataLength += p_buffer->i_buffer;
318 block_Release( p_buffer );
321 static void Flush( audio_output_t *aout, bool wait )
323 if( fflush( aout->sys->p_file ) )
324 msg_Err( aout, "flush error (%m)" );
328 static int Open(vlc_object_t *obj)
330 audio_output_t *aout = (audio_output_t *)obj;
334 aout->volume_set = NULL;
335 aout->mute_set = NULL;