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 *****************************************************************************/
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
39 #include <vlc_codecs.h> /* WAVEHEADER */
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 Play ( audio_output_t *, block_t * );
78 static void Flush ( audio_output_t *, bool );
80 /*****************************************************************************
82 *****************************************************************************/
83 #define FORMAT_TEXT N_("Output format")
85 #define CHANNELS_TEXT N_("Number of output channels")
86 #define CHANNELS_LONGTEXT N_("By default (0), all the channels of the incoming " \
87 "will be saved but you can restrict the number of channels here.")
89 #define WAV_TEXT N_("Add WAVE header")
90 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a WAV " \
91 "header to the file.")
93 static const char *const format_list[] = {
95 #ifndef WORDS_BIGENDIAN
100 static const int format_int[] = {
101 VLC_CODEC_U8, VLC_CODEC_S16N,
102 #ifndef WORDS_BIGENDIAN
108 #define FILE_TEXT N_("Output file")
109 #define FILE_LONGTEXT N_("File to which the audio samples will be written to. (\"-\" for stdout")
112 set_description( N_("File audio output") )
113 set_shortname( N_("File") )
114 set_category( CAT_AUDIO )
115 set_subcategory( SUBCAT_AUDIO_AOUT )
117 add_savefile( "audiofile-file", "audiofile.wav", FILE_TEXT,
118 FILE_LONGTEXT, false )
119 add_string( "audiofile-format", "s16",
120 FORMAT_TEXT, FORMAT_TEXT, true )
121 change_string_list( format_list, format_list )
122 add_integer( "audiofile-channels", 0,
123 CHANNELS_TEXT, CHANNELS_LONGTEXT, true )
124 change_integer_range( 0, 6 )
125 add_bool( "audiofile-wav", true, WAV_TEXT, WAV_LONGTEXT, true )
127 set_capability( "audio output", 0 )
128 add_shortcut( "file", "audiofile" )
129 set_callbacks( Open, NULL )
132 static int Start( audio_output_t *p_aout, audio_sample_format_t *restrict fmt )
134 char * psz_name, * psz_format;
135 const char * const * ppsz_compare = format_list;
136 int i_channels, i = 0;
138 psz_name = var_InheritString( p_aout, "audiofile-file" );
141 msg_Err( p_aout, "you need to specify an output file name" );
146 /* Allocate structure */
147 p_aout->sys = malloc( sizeof( aout_sys_t ) );
148 if( p_aout->sys == NULL )
151 if( !strcmp( psz_name, "-" ) )
152 p_aout->sys->p_file = stdout;
154 p_aout->sys->p_file = vlc_fopen( psz_name, "wb" );
157 if ( p_aout->sys->p_file == NULL )
163 p_aout->time_get = NULL;
165 p_aout->pause = NULL;
166 p_aout->flush = Flush;
169 psz_format = var_InheritString( p_aout, "audiofile-format" );
170 if ( !psz_format ) abort(); /* FIXME */
172 while ( *ppsz_compare != NULL )
174 if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
181 if ( *ppsz_compare == NULL )
183 msg_Err( p_aout, "cannot understand the format string (%s)",
185 if( p_aout->sys->p_file != stdout )
186 fclose( p_aout->sys->p_file );
193 fmt->i_format = format_int[i];
194 if ( AOUT_FMT_SPDIF( fmt ) )
196 fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
197 fmt->i_frame_length = A52_FRAME_NB;
200 /* Channels number */
201 i_channels = var_InheritInteger( p_aout, "audiofile-channels" );
202 if( i_channels > 0 && i_channels <= CHANNELS_MAX )
204 fmt->i_physical_channels = pi_channels_maps[i_channels];
208 p_aout->sys->b_add_wav_header = var_InheritBool( p_aout, "audiofile-wav" );
209 if( p_aout->sys->b_add_wav_header )
211 /* Write wave header */
212 WAVEHEADER *wh = &p_aout->sys->waveh;
214 memset( wh, 0, sizeof(*wh) );
216 switch( fmt->i_format )
218 #ifndef WORDS_BIGENDIAN
220 wh->Format = WAVE_FORMAT_IEEE_FLOAT;
221 wh->BitsPerSample = sizeof(float) * 8;
225 wh->Format = WAVE_FORMAT_PCM;
226 wh->BitsPerSample = 8;
229 wh->Format = WAVE_FORMAT_PCM;
230 wh->BitsPerSample = 16;
234 wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
235 wh->Length = 0; /* temp, to be filled in as we go */
236 wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
237 wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
238 wh->SubChunkLength = 16;
240 wh->Modus = aout_FormatNbChannels( fmt );
241 wh->SampleFreq = fmt->i_rate;
242 wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
243 wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
245 wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
246 wh->DataLength = 0; /* temp, to be filled in as we go */
248 /* Header -> little endian format */
249 SetWLE( &wh->Format, wh->Format );
250 SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
251 SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
252 SetWLE( &wh->Modus, wh->Modus );
253 SetDWLE( &wh->SampleFreq, wh->SampleFreq );
254 SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
255 SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
257 if( fwrite( wh, sizeof(WAVEHEADER), 1,
258 p_aout->sys->p_file ) != 1 )
260 msg_Err( p_aout, "write error: %s", vlc_strerror_c(errno) );
267 /*****************************************************************************
268 * Close: close our file
269 *****************************************************************************/
270 static void Stop( audio_output_t *p_aout )
272 msg_Dbg( p_aout, "closing audio file" );
274 if( p_aout->sys->b_add_wav_header )
276 /* Update Wave Header */
277 p_aout->sys->waveh.Length =
278 p_aout->sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
280 /* Write Wave Header */
281 if( fseek( p_aout->sys->p_file, 0, SEEK_SET ) )
283 msg_Err( p_aout, "seek error: %s", vlc_strerror_c(errno) );
286 /* Header -> little endian format */
287 SetDWLE( &p_aout->sys->waveh.Length,
288 p_aout->sys->waveh.Length );
289 SetDWLE( &p_aout->sys->waveh.DataLength,
290 p_aout->sys->waveh.DataLength );
292 if( fwrite( &p_aout->sys->waveh, sizeof(WAVEHEADER), 1,
293 p_aout->sys->p_file ) != 1 )
295 msg_Err( p_aout, "write error: %s", vlc_strerror_c(errno) );
299 if( p_aout->sys->p_file != stdout )
300 fclose( p_aout->sys->p_file );
304 /*****************************************************************************
305 * Play: pretend to play a sound
306 *****************************************************************************/
307 static void Play( audio_output_t * p_aout, block_t *p_buffer )
309 if( fwrite( p_buffer->p_buffer, p_buffer->i_buffer, 1,
310 p_aout->sys->p_file ) != 1 )
312 msg_Err( p_aout, "write error: %s", vlc_strerror_c(errno) );
315 if( p_aout->sys->b_add_wav_header )
317 /* Update Wave Header */
318 p_aout->sys->waveh.DataLength += p_buffer->i_buffer;
321 block_Release( p_buffer );
324 static void Flush( audio_output_t *aout, bool wait )
326 if( fflush( aout->sys->p_file ) )
327 msg_Err( aout, "flush error: %s", vlc_strerror_c(errno) );
331 static int Open(vlc_object_t *obj)
333 audio_output_t *aout = (audio_output_t *)obj;
337 aout->volume_set = NULL;
338 aout->mute_set = NULL;