1 /*****************************************************************************
2 * file.c : audio output which writes the samples to a file
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: file.c,v 1.29 2004/02/06 18:15:44 gbazin Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
35 #include "aout_internal.h"
38 #define FRAME_SIZE 2048
39 #define A52_FRAME_NB 1536
41 typedef struct WAVEHEADER
43 uint32_t MainChunkID; // it will be 'RIFF'
45 uint32_t ChunkTypeID; // it will be 'WAVE'
46 uint32_t SubChunkID; // it will be 'fmt '
47 uint32_t SubChunkLength;
52 uint16_t BytesPerSample;
53 uint16_t BitsPerSample;
54 uint32_t DataChunkID; // it will be 'data'
58 /*****************************************************************************
59 * aout_sys_t: audio output method descriptor
60 *****************************************************************************
61 * This structure is part of the audio output thread descriptor.
62 * It describes the direct sound specific properties of an audio device.
63 *****************************************************************************/
67 vlc_bool_t b_add_wav_header;
69 WAVEHEADER waveh; /* Wave header of the output file */
72 #define CHANNELS_MAX 6
73 static int pi_channels_maps[CHANNELS_MAX+1] =
77 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
78 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
79 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
80 | AOUT_CHAN_REARRIGHT,
81 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
82 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
83 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
84 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close ( vlc_object_t * );
92 static void Play ( aout_instance_t * );
94 /*****************************************************************************
96 *****************************************************************************/
97 #define FORMAT_TEXT N_("Output format")
98 #define FORMAT_LONGTEXT N_("One of \"u8\", \"s8\", \"u16\", \"s16\", " \
99 "\"u16_le\", \"s16_le\", \"u16_be\", \"s16_be\", \"fixed32\", " \
100 "\"float32\" or \"spdif\"")
101 #define CHANNELS_TEXT N_("Output channels number")
102 #define CHANNELS_LONGTEXT N_("By default, all the channels of the incoming " \
103 "will be saved but you can restrict the number of channels here.")
105 #define WAV_TEXT N_("Add wave header")
106 #define WAV_LONGTEXT N_("Instead of writing a raw file, you can add a wav " \
107 "header to the file")
109 static char *format_list[] = { "u8", "s8", "u16", "s16", "u16_le", "s16_le",
110 "u16_be", "s16_be", "fixed32", "float32",
112 static int format_int[] = { VLC_FOURCC('u','8',' ',' '),
113 VLC_FOURCC('s','8',' ',' '),
114 AOUT_FMT_U16_NE, AOUT_FMT_S16_NE,
115 VLC_FOURCC('u','1','6','l'),
116 VLC_FOURCC('s','1','6','l'),
117 VLC_FOURCC('u','1','6','b'),
118 VLC_FOURCC('s','1','6','b'),
119 VLC_FOURCC('f','i','3','2'),
120 VLC_FOURCC('f','l','3','2'),
121 VLC_FOURCC('s','p','i','f') };
123 #define FILE_TEXT N_("Output file")
124 #define FILE_LONGTEXT N_("File to which the audio samples will be written to")
127 set_description( N_("File audio output") );
129 add_string( "audiofile-format", "s16", NULL,
130 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_TRUE );
131 change_string_list( format_list, 0, 0 );
132 add_integer( "audiofile-channels", 0, NULL,
133 CHANNELS_TEXT, CHANNELS_LONGTEXT, VLC_TRUE );
134 add_file( "audiofile-file", "audiofile.wav", NULL, FILE_TEXT,
135 FILE_LONGTEXT, VLC_FALSE );
136 add_bool( "audiofile-wav", 1, NULL, WAV_TEXT, WAV_LONGTEXT, VLC_TRUE );
138 set_capability( "audio output", 0 );
139 add_shortcut( "file" );
140 add_shortcut( "audiofile" );
141 set_callbacks( Open, Close );
144 /*****************************************************************************
145 * Open: open a dummy audio device
146 *****************************************************************************/
147 static int Open( vlc_object_t * p_this )
149 aout_instance_t * p_aout = (aout_instance_t *)p_this;
150 char * psz_name, * psz_format;
151 char ** ppsz_compare = format_list;
153 int i_channels, i = 0;
155 var_Create( p_this, "audiofile-file", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
156 var_Get( p_this, "audiofile-file", &val );
157 psz_name = val.psz_string;
158 if( !psz_name || !*psz_name )
160 msg_Err( p_aout, "you need to specify an output file name" );
161 if( psz_name ) free( psz_name );
165 /* Allocate structure */
166 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
167 if( p_aout->output.p_sys == NULL )
169 msg_Err( p_aout, "out of memory" );
173 p_aout->output.p_sys->p_file = fopen( psz_name, "wb" );
175 if ( p_aout->output.p_sys->p_file == NULL )
177 free( p_aout->output.p_sys );
181 p_aout->output.pf_play = Play;
184 var_Create( p_this, "audiofile-format", VLC_VAR_STRING|VLC_VAR_DOINHERIT );
185 var_Get( p_this, "audiofile-format", &val );
186 psz_format = val.psz_string;
188 while ( *ppsz_compare != NULL )
190 if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
197 if ( *ppsz_compare == NULL )
199 msg_Err( p_aout, "cannot understand the format string (%s)",
201 fclose( p_aout->output.p_sys->p_file );
202 free( p_aout->output.p_sys );
206 p_aout->output.output.i_format = format_int[i];
207 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
209 p_aout->output.i_nb_samples = A52_FRAME_NB;
210 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
211 p_aout->output.output.i_frame_length = A52_FRAME_NB;
212 aout_VolumeNoneInit( p_aout );
216 p_aout->output.i_nb_samples = FRAME_SIZE;
217 aout_VolumeSoftInit( p_aout );
220 /* Channels number */
221 var_Create( p_this, "audiofile-channels",
222 VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
223 var_Get( p_this, "audiofile-channels", &val );
224 i_channels = val.i_int;
226 if( i_channels > 0 && i_channels <= CHANNELS_MAX )
228 p_aout->output.output.i_physical_channels =
229 pi_channels_maps[i_channels];
233 var_Create( p_this, "audiofile-wav", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
234 var_Get( p_this, "audiofile-wav", &val );
235 p_aout->output.p_sys->b_add_wav_header = val.b_bool;
237 if( p_aout->output.p_sys->b_add_wav_header )
239 /* Write wave header */
240 WAVEHEADER *wh = &p_aout->output.p_sys->waveh;
242 memset( wh, 0, sizeof(wh) );
244 switch( p_aout->output.output.i_format )
246 case VLC_FOURCC('f','l','3','2'):
247 wh->Format = WAVE_FORMAT_IEEE_FLOAT;
248 wh->BitsPerSample = sizeof(float) * 8;
250 case VLC_FOURCC('u','8',' ',' '):
251 wh->Format = WAVE_FORMAT_PCM;
252 wh->BitsPerSample = 8;
254 case VLC_FOURCC('s','1','6','l'):
256 wh->Format = WAVE_FORMAT_PCM;
257 wh->BitsPerSample = 16;
261 wh->MainChunkID = VLC_FOURCC('R', 'I', 'F', 'F');
262 wh->Length = 0; /* temp, to be filled in as we go */
263 wh->ChunkTypeID = VLC_FOURCC('W', 'A', 'V', 'E');
264 wh->SubChunkID = VLC_FOURCC('f', 'm', 't', ' ');
265 wh->SubChunkLength = 16;
267 wh->Modus = aout_FormatNbChannels( &p_aout->output.output );
268 wh->SampleFreq = p_aout->output.output.i_rate;
269 wh->BytesPerSample = wh->Modus * ( wh->BitsPerSample / 8 );
270 wh->BytesPerSec = wh->BytesPerSample * wh->SampleFreq;
272 wh->DataChunkID = VLC_FOURCC('d', 'a', 't', 'a');
273 wh->DataLength = 0; /* temp, to be filled in as we go */
275 /* Header -> little endian format */
276 SetWLE( &wh->Format, wh->Format );
277 SetWLE( &wh->BitsPerSample, wh->BitsPerSample );
278 SetDWLE( &wh->SubChunkLength, wh->SubChunkLength );
279 SetWLE( &wh->Modus, wh->Modus );
280 SetDWLE( &wh->SampleFreq, wh->SampleFreq );
281 SetWLE( &wh->BytesPerSample, wh->BytesPerSample );
282 SetDWLE( &wh->BytesPerSec, wh->BytesPerSec );
284 if( fwrite( wh, sizeof(WAVEHEADER), 1,
285 p_aout->output.p_sys->p_file ) != 1 )
287 msg_Err( p_aout, "write error (%s)", strerror(errno) );
294 /*****************************************************************************
295 * Close: close our file
296 *****************************************************************************/
297 static void Close( vlc_object_t * p_this )
299 aout_instance_t * p_aout = (aout_instance_t *)p_this;
301 msg_Dbg( p_aout, "closing audio file" );
303 if( p_aout->output.p_sys->b_add_wav_header )
305 /* Update Wave Header */
306 p_aout->output.p_sys->waveh.Length =
307 p_aout->output.p_sys->waveh.DataLength + sizeof(WAVEHEADER) - 4;
309 /* Write Wave Header */
310 if( fseek( p_aout->output.p_sys->p_file, 0, SEEK_SET ) )
312 msg_Err( p_aout, "seek error (%s)", strerror(errno) );
315 /* Header -> little endian format */
316 SetDWLE( &p_aout->output.p_sys->waveh.Length,
317 p_aout->output.p_sys->waveh.Length );
318 SetDWLE( &p_aout->output.p_sys->waveh.DataLength,
319 p_aout->output.p_sys->waveh.DataLength );
321 if( fwrite( &p_aout->output.p_sys->waveh, sizeof(WAVEHEADER), 1,
322 p_aout->output.p_sys->p_file ) != 1 )
324 msg_Err( p_aout, "write error (%s)", strerror(errno) );
328 fclose( p_aout->output.p_sys->p_file );
329 free( p_aout->output.p_sys );
332 /*****************************************************************************
333 * Play: pretend to play a sound
334 *****************************************************************************/
335 static void Play( aout_instance_t * p_aout )
337 aout_buffer_t * p_buffer;
339 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
341 if( fwrite( p_buffer->p_buffer, p_buffer->i_nb_bytes, 1,
342 p_aout->output.p_sys->p_file ) != 1 )
344 msg_Err( p_aout, "write error (%s)", strerror(errno) );
347 if( p_aout->output.p_sys->b_add_wav_header )
349 /* Update Wave Header */
350 p_aout->output.p_sys->waveh.DataLength += p_buffer->i_nb_bytes;
353 aout_BufferFree( p_buffer );