]> git.sesse.net Git - vlc/blob - modules/audio_output/file.c
4ec06747e128df4e3dcb2cedf0596b5277aa8598
[vlc] / modules / audio_output / file.c
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.4 2002/08/12 22:12:51 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/aout.h>
33
34 #include "aout_internal.h"
35
36 #define FRAME_SIZE 2048
37
38 /*****************************************************************************
39  * Local prototypes.
40  *****************************************************************************/
41 static int     Open        ( vlc_object_t * );
42 static void    Close       ( vlc_object_t * );
43 static int     SetFormat   ( aout_instance_t * );
44 static void    Play        ( aout_instance_t *, aout_buffer_t * );
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 #define FORMAT_TEXT N_("Output format")
50 #define FORMAT_LONGTEXT N_("one of \"u8\", \"s8\", \"u16\", \"s16\"," \
51                            " \"u16_le\", \"s16_le\", \"u16_be\"," \
52                            " \"s16_be\", \"fixed32\", \"float32\" or \"spdif\"")
53
54 static char *format_list[] = { "u8", "s8", "u16", "s16", "u16_le", "s16_le",
55                                "u16_be", "s16_be", "fixed32", "float32",
56                                "spdif", NULL };
57 static int format_int[] = { AOUT_FMT_U8, AOUT_FMT_S8, AOUT_FMT_U16_NE,
58                             AOUT_FMT_S16_NE, AOUT_FMT_U16_LE, AOUT_FMT_S16_LE,
59                             AOUT_FMT_U16_BE, AOUT_FMT_S16_BE, AOUT_FMT_FIXED32,
60                             AOUT_FMT_FLOAT32, AOUT_FMT_A52 };
61
62 #define PATH_TEXT N_("Path of the output file")
63 #define PATH_LONGTEXT N_("By default samples.raw")
64
65 vlc_module_begin();
66     add_category_hint( N_("Audio"), NULL );
67     add_string_from_list( "format", "s16", format_list, NULL,
68                           FORMAT_TEXT, FORMAT_LONGTEXT );
69     add_string( "path", "samples.raw", NULL, PATH_TEXT, PATH_LONGTEXT );
70     set_description( _("file output module") );
71     set_capability( "audio output", 0 );
72     add_shortcut( "file" );
73     set_callbacks( Open, Close );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Open: open a dummy audio device
78  *****************************************************************************/
79 static int Open( vlc_object_t * p_this )
80 {
81     aout_instance_t * p_aout = (aout_instance_t *)p_this;
82     FILE * p_file;
83     char * psz_name = config_GetPsz( p_this, "path" );
84
85     (FILE *)p_aout->output.p_sys = p_file = fopen( psz_name, "wb" );
86     free( psz_name );
87     if ( p_file == NULL ) return -1;
88
89     p_aout->output.pf_setformat = SetFormat;
90     p_aout->output.pf_play = Play;
91
92     return VLC_SUCCESS;
93 }
94
95 /*****************************************************************************
96  * Close: close our file
97  *****************************************************************************/
98 static void Close( vlc_object_t * p_this )
99 {
100     aout_instance_t * p_aout = (aout_instance_t *)p_this;
101
102     fclose( (FILE *)p_aout->output.p_sys );
103 }
104
105 /*****************************************************************************
106  * SetFormat: pretend to set the dsp output format
107  *****************************************************************************/
108 static int SetFormat( aout_instance_t * p_aout )
109 {
110     char * psz_format = config_GetPsz( p_aout, "format" );
111     char ** ppsz_compare = format_list;
112     int i = 0;
113
114     while ( *ppsz_compare != NULL )
115     {
116         if ( !strncmp( *ppsz_compare, psz_format, strlen(*ppsz_compare) ) )
117         {
118             break;
119         }
120         ppsz_compare++; i++;
121     }
122
123     if ( *ppsz_compare == NULL )
124     {
125         msg_Err( p_aout, "Cannot understand the format string (%s)",
126                  psz_format );
127         return -1;
128     }
129
130     p_aout->output.output.i_format = format_int[i];
131     p_aout->output.i_nb_samples = FRAME_SIZE;
132     return 0;
133 }
134
135 /*****************************************************************************
136  * Play: pretend to play a sound
137  *****************************************************************************/
138 static void Play( aout_instance_t * p_aout, aout_buffer_t * p_buffer )
139 {
140     if( fwrite( p_buffer->p_buffer,
141                 aout_FormatToSize( &p_aout->output.output,
142                                    p_buffer->i_nb_samples ), 1,
143                 (FILE *)p_aout->output.p_sys ) != 1 )
144     {
145         msg_Err( p_aout, "write error (%s)", strerror(errno) );
146     }
147
148     aout_BufferFree( p_buffer );
149 }
150