1 #ifndef AUDIO_H
2 #define AUDIO_H
3 
4 #include <stdlib.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
10 /** Sound formats.
11  *
12  * Sound format bits. Only one can be set in the format, the exception is
13  * when we want to hold a list of supported formats - they can be bitwise-or'd.
14  */
15 enum sfmt_fmt
16 {
17 	SFMT_S8 =	0x00000001, /*!< signed 8-bit */
18 	SFMT_U8	=	0x00000002, /*!< unsigned 8-bit */
19 	SFMT_S16 =	0x00000004, /*!< signed 16-bit */
20 	SFMT_U16 =	0x00000008, /*!< unsigned 16-bit */
21 	SFMT_S32 =	0x00000010, /*!< signed 24-bit (LSB is 0) */
22 	SFMT_U32 =	0x00000020, /*!< unsigned 24-bit (LSB set to 0) */
23 	SFMT_FLOAT =	0x00000040, /*!< float in range -1.0 to 1.0 */
24 };
25 
26 /** Sample endianness.
27  *
28  * Sample endianness - one of them must be set for 16-bit and 24-bit formats.
29  */
30 enum sfmt_endianness
31 {
32 	SFMT_LE =	0x00001000, /*!< little-endian */
33 	SFMT_BE =	0x00002000, /*!< big-endian */
34 
35 /** Define native endianness to SFMT_LE or SFMT_BE. */
36 #ifdef WORDS_BIGENDIAN
37 	SFMT_NE =	SFMT_BE
38 #else
39 	SFMT_NE =	SFMT_LE
40 #endif
41 };
42 
43 /** @name Masks for the sample format.
44  *
45  * Masks used to extract only one type of information from the sound format.
46  */
47 /*@{*/
48 #define SFMT_MASK_FORMAT		0x00000fff /*!< sample format */
49 #define SFMT_MASK_ENDIANNESS	0x00003000 /*!< sample endianness */
50 /*@}*/
51 
52 /** Return a value other than 0 if the sound format seems to be proper. */
53 #define sound_format_ok(f) (((f) & SFMT_MASK_FORMAT) \
54 		&& (((f) & (SFMT_S8 | SFMT_U8 | SFMT_FLOAT)) \
55 			|| (f) & SFMT_MASK_ENDIANNESS))
56 
57 /** Change the sample format to new_fmt (without endianness). */
58 #define sfmt_set_fmt(f, new_fmt) (((f) & ~SFMT_MASK_FORMAT) | (new_fmt))
59 
60 /** Change the sample format endianness to endian. */
61 #define sfmt_set_endian(f, endian) (((f) & ~SFMT_MASK_ENDIANNESS) | (endian))
62 
63 /** Sound parameters.
64  *
65  * A structure describing sound parameters. The format is always PCM signed,
66  * native endian for this machine.
67  */
68 struct sound_params
69 {
70 	int channels; /*!< Number of channels: 1 or 2 */
71 	int rate; /*!< Rate in Hz */
72 	long fmt; /*!< Format of the samples (SFMT_* bits) */
73 };
74 
75 /** Output driver capabilities.
76  *
77  * A structure describing the output driver capabilities.
78  */
79 struct output_driver_caps
80 {
81 	int min_channels; /*!< Minimum number of channels */
82 	int max_channels; /*!< Maximum number of channels */
83 	long formats; /*!< Supported sample formats (or'd sfmt_fmt mask
84 			with endianness') */
85 };
86 
87 /** \struct hw_funcs
88  * Functions to control the audio "driver".
89  *
90  * The structure holds pointers to functions that must be provided by the audio
91  * "driver". All functions are executed only by one thread, so you don't need
92  * to worry if they are thread safe.
93  */
94 struct hw_funcs
95 {
96 	/** Initialize the driver.
97 	 *
98 	 * This function is invoked only once when the MOC server starts.
99 	 *
100 	 * \param caps Capabilities of the driver which must be filled by the
101 	 * function.
102 	 * \return 1 on success and 0 otherwise.
103 	 */
104 	int (*init) (struct output_driver_caps *caps);
105 
106 	/** Clean up at exit.
107 	 *
108 	 * This function is invoked only once when the MOC server exits. The
109 	 * audio device is not in use at this moment. The function should close
110 	 * any opened devices and free any resources the driver allocated.
111 	 * After this function was used, no other functions will be invoked.
112 	 */
113 	void (*shutdown) ();
114 
115 	/** Open the sound device.
116 	 *
117 	 * This function should open the sound device with the proper
118 	 * parameters. The function should return 1 on success and 0 otherwise.
119 	 * After returning 1 functions like play(), get_buff_fill() can be used.
120 	 *
121 	 * The sample rate of the driver can differ from the requested rate.
122 	 * If so, get_rate() should return the actual rate.
123 	 *
124 	 * \param sound_params Pointer to the sound_params structure holding
125 	 * the required poarameters.
126 	 * \return 1 on success and 0 otherwise.
127 	 */
128 	int (*open) (struct sound_params *sound_params);
129 
130 	/** Close the device.
131 	 *
132 	 * Request for closing the device.
133 	 */
134 	void (*close) ();
135 
136 	/** Play sound.
137 	 *
138 	 * Play sound provided in the buffer. The sound is in the format
139 	 * requested when the open() function was invoked. The function should
140 	 * play all sound in the buffer.
141 	 *
142 	 * \param buff Pointer to the buffer with the sound.
143 	 * \param size Size (in bytes) of the buffer.
144 	 *
145 	 * \return The number of bytes played or a value less than zero on
146 	 * error.
147 	 */
148 	int (*play) (const char *buff, const size_t size);
149 
150 	/** Read the volume setting.
151 	 *
152 	 * Read the current volume setting. This must work regardless if the
153 	 * functions open()/close() where used.
154 	 *
155 	 * \return Volume value from 0% to 100%.
156 	 */
157 	int (*read_mixer) ();
158 
159 	/** Set the volume setting.
160 	 *
161 	 * Set the volume. This must work regardless if the functions
162 	 * open()/close() where used.
163 	 *
164 	 * \param vol Volume from 0% to 100%.
165 	 */
166 	void (*set_mixer) (int vol);
167 
168 	/** Read the hardware/internal buffer fill.
169 	 *
170 	 * The function should return the number of bytes of any
171 	 * hardware or internal buffers are filled. For example: if we play()
172 	 * 4KB, but only 1KB was really played (could be heard by the user),
173 	 * the function should return 3072 (3KB).
174 	 *
175 	 * \return Current hardware/internal buffer fill in bytes.
176 	 */
177 	int (*get_buff_fill) ();
178 
179 	/** Stop playing immediately.
180 	 *
181 	 * Request that the sound should not be played. This should involve
182 	 * flushing any internal buffer filled with data sent by the play()
183 	 * function and resetting the device to flush its buffer (if possible).
184 	 *
185 	 * \return 1 on success or 0 otherwise.
186 	 */
187 	int (*reset) ();
188 
189 	/** Get the current sample rate setting.
190 	 *
191 	 * Get the actual sample rate setting of the audio driver.
192 	 *
193 	 * \return Sample rate in Hz.
194 	 */
195 	int (*get_rate) ();
196 
197 	/** Toggle the mixer channel.
198 	 *
199 	 * Toggle between the first and the second mixer channel.
200 	 */
201 	void (*toggle_mixer_channel) ();
202 
203 	/** Get the mixer channel's name.
204 	 *
205 	 * Get the currently used mixer channel's name.
206 	 *
207 	 * \return malloc()ed channel's name.
208 	 */
209 	char * (*get_mixer_channel_name) ();
210 };
211 
212 /* Are the parameters p1 and p2 equal? */
213 #define sound_params_eq(p1, p2) ((p1).fmt == (p2).fmt \
214 		&& (p1).channels == (p2).channels && (p1).rate == (p2).rate)
215 
216 /* Maximum size of a string needed to hold the value returned by sfmt_str(). */
217 #define SFMT_STR_MAX	265
218 
219 char *sfmt_str (const long format, char *msg, const size_t buf_size);
220 int sfmt_Bps (const long format);
221 int sfmt_same_bps (const long fmt1, const long fmt2);
222 
223 void audio_stop ();
224 void audio_play (const char *fname);
225 void audio_next ();
226 void audio_prev ();
227 void audio_pause ();
228 void audio_unpause ();
229 void audio_initialize ();
230 void audio_exit ();
231 void audio_seek (const int sec);
232 void audio_jump_to (const int sec);
233 
234 int audio_open (struct sound_params *sound_params);
235 int audio_send_buf (const char *buf, const size_t size);
236 int audio_send_pcm (const char *buf, const size_t size);
237 void audio_reset ();
238 int audio_get_bpf ();
239 int audio_get_bps ();
240 int audio_get_buf_fill ();
241 void audio_close ();
242 int audio_get_time ();
243 int audio_get_state ();
244 int audio_get_prev_state ();
245 void audio_plist_add (const char *file);
246 void audio_plist_clear ();
247 char *audio_get_sname ();
248 void audio_set_mixer (const int val);
249 int audio_get_mixer ();
250 void audio_plist_delete (const char *file);
251 int audio_get_ftime (const char *file);
252 void audio_plist_set_time (const char *file, const int time);
253 void audio_state_started_playing ();
254 int audio_plist_get_serial ();
255 void audio_plist_set_serial (const int serial);
256 struct file_tags *audio_get_curr_tags ();
257 char *audio_get_mixer_channel_name ();
258 void audio_toggle_mixer_channel ();
259 void audio_plist_move (const char *file1, const char *file2);
260 void audio_queue_add (const char *file);
261 void audio_queue_delete (const char *file);
262 void audio_queue_clear ();
263 void audio_queue_move (const char *file1, const char *file2);
264 struct plist* audio_queue_get_contents ();
265 
266 #ifdef __cplusplus
267 }
268 #endif
269 
270 #endif
271