File: wave.c

package info (click to toggle)
directfb 1.7.7-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,212 kB
  • sloc: ansic: 306,760; cpp: 46,357; sh: 11,720; makefile: 5,620; perl: 662; asm: 507; xml: 116
file content (384 lines) | stat: -rw-r--r-- 10,703 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
   (c) Copyright 2012-2013  DirectFB integrated media GmbH
   (c) Copyright 2001-2013  The world wide DirectFB Open Source Community (directfb.org)
   (c) Copyright 2000-2004  Convergence (integrated media) GmbH

   All rights reserved.

   Written by Denis Oliver Kropp <dok@directfb.org>,
              Andreas Shimokawa <andi@directfb.org>,
              Marek Pikarski <mass@directfb.org>,
              Sven Neumann <neo@directfb.org>,
              Ville Syrjälä <syrjala@sci.fi> and
              Claudio Ciccani <klan@users.sf.net>.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/



#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <direct/mem.h>
#include <direct/messages.h>
#include <direct/util.h>

#include <fusionsound.h>

#include <core/sound_device.h>
#include <core/sound_driver.h>

#include <misc/sound_conf.h>


FS_SOUND_DRIVER( wave )

/******************************************************************************/

typedef struct {
     int   fd;
     
     int   bits;
     int   channels;
     
     void *buffer;
     int   buffersize;
} WaveDeviceData;

/******************************************************************************/

typedef struct {
     u32 ChunkID;
     u32 ChunkSize;
     u32 Format;
     u32 Subchunk1ID;
     u32 Subchunk1Size;
     u16 AudioFormat;
     u16 NumChannels;
     u32 SampleRate;
     u32 ByteRate;
     u16 BlockAlign;
     u16 BitsPerSample;
     u32 Subchunk2ID;
     u32 Subchunk2Size;
} WaveHeader;

#define FCC( a, b, c, d ) ((a) | ((b)<<8) | ((c)<<16) | ((d)<<24))

/******************************************************************************/


static DirectResult
device_probe( void )
{
     int  fd;
     char path[4096];

     /* load only when requested */
     if (!fs_config->driver)
          return DR_UNSUPPORTED;

     if (fs_config->device) {
          snprintf( path, sizeof(path), "%s", fs_config->device );
     }
     else {
          snprintf( path, sizeof(path),
                    "./fusionsound-%d.wav", fs_config->session );
     }

     fd = open( path, O_WRONLY | O_CREAT | O_NOCTTY | O_NONBLOCK, 0644 );
     if (fd < 0)
          return DR_UNSUPPORTED;

     close( fd );

     return DR_OK;
}

static void
device_get_driver_info( SoundDriverInfo *info )
{
     snprintf( info->name,
               FS_SOUND_DRIVER_INFO_NAME_LENGTH,
               "Wave" );

     snprintf( info->vendor,
               FS_SOUND_DRIVER_INFO_VENDOR_LENGTH,
               "directfb.org" );

     snprintf( info->url,
               FS_SOUND_DRIVER_INFO_URL_LENGTH,
               "http://www.directfb.org" );

     snprintf( info->license,
               FS_SOUND_DRIVER_INFO_LICENSE_LENGTH,
               "LGPL" );

     info->version.major = 0;
     info->version.minor = 2;

     info->device_data_size = sizeof(WaveDeviceData);
}

static DirectResult
device_open( void                  *device_data,
             SoundDeviceInfo       *device_info,
             CoreSoundDeviceConfig *config )
{
     WaveDeviceData *data   = device_data;
     WaveHeader      header;
     char            path[4096];

     if (config->format == FSSF_FLOAT)
          return DR_UNSUPPORTED;

     if (fs_config->device) {
          snprintf( path, sizeof(path), "%s", fs_config->device );
     }
     else {
          snprintf( path, sizeof(path),
                    "./fusionsound-%d.wav", fs_config->session );
     }

     data->fd = open( path, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0644 );
     if (data->fd < 0) {
          D_ERROR( "FusionSound/Device/Wave: "
                   "couldn't open '%s' for writing!\n", path );
          return DR_IO;
     }

     /* close file descriptor on exec */
     fcntl( data->fd, F_SETFD, FD_CLOEXEC );

     /* device name */
     snprintf( device_info->name,
               FS_SOUND_DEVICE_INFO_NAME_LENGTH,
               "%s", strrchr( path, '/' ) ? (strrchr( path, '/' )+1) : path );

     /* device capabilities */
     device_info->caps = 0;
     
     /* allocate output buffer */
     data->buffer = D_MALLOC( config->buffersize *
                              FS_CHANNELS_FOR_MODE(config->mode) *
                              FS_BYTES_PER_SAMPLE(config->format) );
     if (!data->buffer) {
          close( data->fd );
          return D_OOM();
     }
     
     data->buffersize = config->buffersize;


     header.ChunkID       = FCC('R','I','F','F');
     header.ChunkSize     = 0;
     header.Format        = FCC('W','A','V','E');
     header.Subchunk1ID   = FCC('f','m','t',' ');
     header.Subchunk1Size = 16;
     header.AudioFormat   = 1;
     header.NumChannels   = FS_CHANNELS_FOR_MODE(config->mode);
     header.SampleRate    = config->rate;
     header.ByteRate      = config->rate *
                            FS_CHANNELS_FOR_MODE(config->mode) *
                            FS_BYTES_PER_SAMPLE(config->format);
     header.BlockAlign    = FS_CHANNELS_FOR_MODE(config->mode) *
                            FS_BYTES_PER_SAMPLE(config->format);
     header.BitsPerSample = FS_BITS_PER_SAMPLE(config->format);
     header.Subchunk2ID   = FCC('d','a','t','a');
     header.Subchunk2Size = 0;
     
#ifdef WORDS_BIGENDIAN
     header.ChunkID       = BSWAP32(header.ChunkID);
     header.ChunkSize     = BSWAP32(header.ChunkSize);
     header.Format        = BSWAP32(header.Format);
     header.Subchunk1ID   = BSWAP32(header.Subchunk1ID);
     header.Subchunk1Size = BSWAP32(header.Subchunk1Size);
     header.AudioFormat   = BSWAP16(header.AudioFormat);
     header.NumChannels   = BSWAP16(header.NumChannels);
     header.SampleRate    = BSWAP32(header.SampleRate);
     header.ByteRate      = BSWAP32(header.ByteRate);
     header.BlockAlign    = BSWAP16(header.BlockAlign);
     header.BitsPerSample = BSWAP16(header.BitsPerSample);
     header.Subchunk2ID   = BSWAP32(header.Subchunk2ID);
     header.Subchunk2Size = BSWAP32(header.Subchunk2Size);
#endif

     if (write( data->fd, &header, sizeof(header) ) < sizeof(header)) {
          D_ERROR( "FusionSound/Device/Wave: write error!\n" );
          return DR_IO;
     }

     data->bits = FS_BITS_PER_SAMPLE(config->format);
     data->channels = FS_CHANNELS_FOR_MODE(config->mode);

     return DR_OK;
}

static DirectResult
device_get_buffer( void *device_data, u8 **addr, unsigned int *avail )
{
     WaveDeviceData *data = device_data;
     
     *addr = data->buffer;
     *avail = data->buffersize;
     
     return DR_OK;
}

static DirectResult
device_commit_buffer( void *device_data, unsigned int frames )
{
     WaveDeviceData *data = device_data;
     void           *buf  = data->buffer;
     
#ifdef WORDS_BIGENDIAN
     unsigned int    i;
     
     switch (data->bits) {
          case 16:
               for (i = 0; i < frames*data->channels; i++)
                    ((u16*)buf)[i] = BSWAP16(((u16*)buf)[i]);
               break;
          case 24:
               for (i = 0; i < frames*data->channels; i++) {
                    u8 tmp = ((u8*)buf)[i*3+0];
                    ((u8*)buf)[i*3+0] = ((u8*)buf)[i*3+2];
                    ((u8*)buf)[i*3+2] = tmp;
               }
               break;
          case 32:
               for (i = 0; i < frames*data->channels; i++)
                    ((u32*)buf)[i] = BSWAP32(((u32*)buf)[i]);
               break;
          default:
               break;
     }
#endif

     write( data->fd, buf, frames * data->channels * data->bits >> 3 );
     
     return DR_OK;
}

static void
device_get_output_delay( void *device_data, int *delay )
{
     *delay = 0;
}

static DirectResult
device_get_volume( void *device_data, float *level )
{
     return DR_UNSUPPORTED;
}

static DirectResult
device_set_volume( void *device_data, float level )
{
     return DR_UNSUPPORTED;
}

static DirectResult
device_suspend( void *device_data )
{
     WaveDeviceData *data = device_data;
     struct stat     st;
     
     if (fstat( data->fd, &st ) == 0 && !S_ISFIFO(st.st_mode)) {
          close( data->fd );
          data->fd = -1;
     }
     
     return DR_OK;
}

static DirectResult
device_resume( void *device_data )
{
     WaveDeviceData *data = device_data;
     char            path[4096];
     
     if (data->fd < 0) {             
          if (fs_config->device) {
               snprintf( path, sizeof(path), "%s", fs_config->device );
          }
          else {
               snprintf( path, sizeof(path),
                         "./fusionsound-%d.wav", fs_config->session );
          }

          data->fd = open( path, O_WRONLY | O_APPEND | O_NOCTTY );
          if (data->fd < 0) {
               D_ERROR( "FusionSound/Device/Wave: couldn't reopen '%s'!\n", path );
               return DR_IO;
          }

          fcntl( data->fd, F_SETFD, FD_CLOEXEC );
     }
     
     return DR_OK;
}

static void
device_handle_fork( void             *device_data,
                    FusionForkAction  action,
                    FusionForkState   state )
{
     WaveDeviceData *data = device_data;
     
     if (action == FFA_CLOSE && state == FFS_CHILD) {
          close( data->fd );
          data->fd = -1;
     }
}

static void
device_close( void *device_data )
{
     WaveDeviceData *data = device_data;
     
     if (data->buffer)
          D_FREE( data->buffer);

     if (data->fd >= 0) {
          off_t pos = lseek( data->fd, 0, SEEK_CUR );
          if (pos > 0) {
               u32 ChunkSize     = pos - 8;
               u32 Subchunk2Size = pos - sizeof(WaveHeader);
          
#ifdef WORDS_BIGENDIAN
               ChunkSize     = BSWAP32(ChunkSize);
               Subchunk2Size = BSWAP32(Subchunk2Size);
#endif
               if (lseek( data->fd, 4, SEEK_SET ) == 4)
                    write( data->fd, &ChunkSize, 4 );

               if (lseek( data->fd, 40, SEEK_SET ) == 40)
                    write( data->fd, &Subchunk2Size, 4 );
          }

          close( data->fd );
     }
}