4
Copyright (c) 1999, 2000
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public Licensse as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33
#include <sys/types.h>
44
#include <dbsoundcard.h>
46
#include <dbchannel.h>
48
#include <dbaudiolib.h>
50
#include "prototypes.h"
52
/* variables to access DBMix channels shared memory */
54
dbfsd_data * sysdata = NULL;
55
local_channel * local_channels = NULL;
56
/* static local_channel * cue_channels; */
57
local_channel * ch = NULL;
59
/* variables to describe the input audio stream */
65
/* globally static audio write variables */
67
float sample1, sample2;
72
signed short output_buf[OUTPUT_BUFSIZE]; /* buffer used to hold main output to dbfsd */
73
signed short cue_buf[OUTPUT_BUFSIZE]; /* buffer used to hold cue output to dbfsd */
74
signed short conv_buf[OUTPUT_BUFSIZE]; /* buffer used to hold format converted input */
77
extern int debug_level; /* declared in debug module */
80
DBAudio_Init - initializes this instance. If an error occurs, returns FAILURE
81
otherwise, returns SUCCESS.
84
fmt - format of input data (see OSS Programmer's Manual
85
http://www.opensound.com/pguide/index.html)
86
rte - input data rate expressed as a whole integer. i.e. 44.1 kHz = 44100
87
numch - number of channels in the input data
88
type - DBMix channel type, see enum channel_type_e in channel.h
89
chindex - if 0 returns the first available channel, otherwise tries to use the
90
channel associated with (chindex -1). If this channel is not free,
97
- Shared memory channel data created by dbfsd is attached to this process..
98
- From this data, determine the next free/unused channel. The pointer
99
to this channel is stored in the static global ch variable.
100
- Open communication and signal pipes for communicating with dbfsd
102
Note: this function makes use of goto statements. More specifically,
103
a label called cleanup exists at the end of the function allowing
104
a single cleanup point in the function. This is a result of the
105
coding standards my group at work enforces. Despite what you were
106
taught about the evils of goto, this method of their use makes the
107
code cleaner and more elegant, otherwise the exact same shmdt()
108
statements would be scattered half a dozen times through out the code.
109
The single "goto cleanup" line replaces 4-10 lines of repetitive
110
cleanup code each time it is used.
112
int DBAudio_Init(char * name, int fmt, int rte, int numch,
113
enum channel_type_e type, int chindex)
115
char init_buffer[PIPE_BUF];
124
/* init errno to SUCCESS to maintain error state */
132
if (local_channels != NULL)
142
/* init variables used by DBAudio_Write() */
144
sample1 = sample2 = 0.0;
156
format = AFMT_S16_NE;
159
/* verify sample rate */
162
sample_rate = DB_SAMPLE_RATE;
166
if ((rte >= 8000) && (rte < 49000))
172
errno = ERROR_BAD_SAMPLERATE;
177
/* verify number of channels */
182
break; /* default to stereo */
193
errno = ERROR_BAD_NUMCH;
198
/* Get system data */
200
sysshmid = shmget((key_t) DB_SYSTEM_SM_KEY, sizeof(dbfsd_data),
205
Error("DBAudioLib ERROR: could not create shared memory for system data.\n Is dbfsd running?");
206
errno = ERROR_INIT_FAILURE;
210
sysdata = shmat(sysshmid,(void *)0, 0);
212
if ((int)sysdata == -1)
214
Error("DBAudioLib ERROR: error attaching system data shared memory.");
215
errno = ERROR_INIT_FAILURE;
219
/* verify that there is a free channel into dbfsd */
220
if (sysdata->free_channel_index == -1)
224
Error("DBAudio_Init: no free channels.");
226
if (shmdt(sysdata) == -1)
228
Error("DBAudio_Init: could not detach system data memory segment.");
229
errno = ERROR_INIT_FAILURE;
233
errno = ERROR_NO_FREE_CHANNELS;
237
/* End get system data */
239
/* retrieve the channel memory id */
241
shmid = shmget((key_t) DB_CHANNELS_SM_KEY,
242
(sysdata->num_channels * sizeof(local_channel)), 0666);
246
Error("DBAudioLib ERROR: error creating channel shared memory.");
247
errno = ERROR_INIT_FAILURE;
252
Debug("DBAudioLib: shmid is: %d ",shmid);
255
/* attach the channel memory segment*/
256
local_channels = (local_channel *) shmat(shmid,(void *)0, 0);
258
if ((int)local_channels == -1)
260
Error("DBAudioLib ERROR: error attaching channel shared memory.");
262
errno = ERROR_INIT_FAILURE;
269
ch = &(local_channels[sysdata->free_channel_index]);
273
/* chindex is begins at 1, so decrement to directly reference the
277
if ((chindex < 0) || (chindex > sysdata->num_channels))
279
errno = ERROR_BAD_CHANNEL_ID;
283
if (local_channels[chindex].free_channel)
285
ch = &(local_channels[chindex]);
289
Debug("DBAudioLib: opening comm pipe: %s",ch->comm_filename);
291
/* open comm pipe to server */
292
if ((ch->client_comm_fd = open(ch->comm_filename, O_WRONLY)) == -1)
294
Debug("%s",ch->comm_filename);
296
DBAudio_perror("DBAudioLib: Error opening comm pipe.");
297
errno = ERROR_INIT_FAILURE;
301
Debug("comm fd is: %d\n",ch->client_comm_fd);
305
printf("flags is %d\n",ch->channel_flags);
307
/* open cue_pipe to be NON BLOCKING -- very important since the server does
308
not check the cue channel with its select() call*/
309
Debug("DBAudioLib: opening cue pipe: %s",ch->cue_filename);
311
if ((ch->client_cue_fd = open(ch->cue_filename, O_WRONLY | O_NONBLOCK)) == -1)
313
Debug("%s",ch->cue_filename);
314
DBAudio_perror("DBAudioLib: Error opening cue pipe.");
315
errno = ERROR_INIT_FAILURE;
319
Debug("Cue fd is: %d\n",ch->client_cue_fd);
322
ch->left_gain = DEFAULT_GAIN;
323
ch->right_gain = DEFAULT_GAIN;
325
ch->channel_type = type;
326
ch->base_pitch = DEFAULT_PITCH;
328
switch(ch->channel_type)
331
Debug("DBAudioLib: Channel type: PIPE CHANNEL");
337
/* update channel name check against 48 to allow for channel index */
338
DBAudio_Set_Channel_Name(name);
339
} /* end retrieve the channel memory id */
341
DBAudio_Set_Rate(rte);
343
/* clear msg queue */
344
if (ch->msg_q_id != -1)
348
while (msgrcv(ch->msg_q_id, &msg,sizeof(dbfsd_msg) - sizeof(long int),0,IPC_NOWAIT) != -1)
350
Debug("DBAudio_Init: cleared message %d",msg.msg_type);
354
/* init message handler to NULL */
355
ch->message_handler = NULL;
357
/* init sample buffer */
359
ch->sampler_state = SAMPLER_OFF;
360
ch->sampler_bufsize = (ch->sampler_time /* number of seconds */
361
* (DB_SAMPLE_SIZE * DB_CHANNEL_NUM_CHANNELS) /* size of 1 stereo sample */
362
* DB_SAMPLE_RATE); /* num samples per second */
363
ch->sampler_size = 0;
364
ch->sampler_readoffset = 0;
365
ch->sampler_startoffset = 0;
366
ch->sampler_buf = (char *) malloc(ch->sampler_bufsize);
368
Debug("sampler bufsize is: %d",ch->sampler_bufsize);
371
/* write an empty buffer to the mixer to init this channel */
373
memset(init_buffer,0,PIPE_BUF);
375
Debug("writing init buffer... \n");
378
if (DBAudio_Write(init_buffer,templen) == FAILURE)
380
DBAudio_perror("Error writing init data to channel:: ");
386
Debug("DBAudioLib: dbaudio_init complete.");
391
/* ENOMSG is a side effect of opening/clearing the message queue, so remap it */
392
if (errno == ENOMSG) errno = SUCCESS;
394
/* if error, retur to initial state */
395
if (errno != SUCCESS)
399
if (local_channels != NULL)
401
shmdt(local_channels);