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 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/*
Author: Bob Dean
Copyright (c) 1999, 2000
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public Licensse as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <limits.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <glib.h>
#include <math.h>
#include <dbsoundcard.h>
#include <dbchannel.h>
#include <dbdebug.h>
#include <dbaudiolib.h>
#include "prototypes.h"
/* variables to access DBMix channels shared memory */
int shmid, sysshmid;
dbfsd_data * sysdata = NULL;
local_channel * local_channels = NULL;
/* static local_channel * cue_channels; */
local_channel * ch = NULL;
/* variables to describe the input audio stream */
int format;
int sample_rate;
int num_channels;
int paused;
/* globally static audio write variables */
float local_pitch;
float sample1, sample2;
float float_index;
int sampleindex;
int outlen;
signed short output_buf[OUTPUT_BUFSIZE]; /* buffer used to hold main output to dbfsd */
signed short cue_buf[OUTPUT_BUFSIZE]; /* buffer used to hold cue output to dbfsd */
signed short conv_buf[OUTPUT_BUFSIZE]; /* buffer used to hold format converted input */
extern int debug_level; /* declared in debug module */
/*
DBAudio_Init - initializes this instance. If an error occurs, returns FAILURE
otherwise, returns SUCCESS.
Parameters:
fmt - format of input data (see OSS Programmer's Manual
http://www.opensound.com/pguide/index.html)
rte - input data rate expressed as a whole integer. i.e. 44.1 kHz = 44100
numch - number of channels in the input data
type - DBMix channel type, see enum channel_type_e in channel.h
chindex - if 0 returns the first available channel, otherwise tries to use the
channel associated with (chindex -1). If this channel is not free,
FAILURE is returned.
Pre Condition:
- None
Post Condition
- Shared memory channel data created by dbfsd is attached to this process..
- From this data, determine the next free/unused channel. The pointer
to this channel is stored in the static global ch variable.
- Open communication and signal pipes for communicating with dbfsd
Note: this function makes use of goto statements. More specifically,
a label called cleanup exists at the end of the function allowing
a single cleanup point in the function. This is a result of the
coding standards my group at work enforces. Despite what you were
taught about the evils of goto, this method of their use makes the
code cleaner and more elegant, otherwise the exact same shmdt()
statements would be scattered half a dozen times through out the code.
The single "goto cleanup" line replaces 4-10 lines of repetitive
cleanup code each time it is used.
*/
int DBAudio_Init(char * name, int fmt, int rte, int numch,
enum channel_type_e type, int chindex)
{
char init_buffer[PIPE_BUF];
int templen;
#ifdef DBMIX_DEBUG
debug_level = 1;
#else
debug_level = 0;
#endif
/* init errno to SUCCESS to maintain error state */
errno = SUCCESS;
if (ch != NULL)
{
goto cleanup;
}
if (local_channels != NULL)
{
goto cleanup;
}
if (sysdata != NULL)
{
goto cleanup;
}
/* init variables used by DBAudio_Write() */
local_pitch = 1.0;
sample1 = sample2 = 0.0;
float_index = 0.0;
sampleindex = 0;
outlen = 0;
/* verify format */
if (fmt != 0)
{
format = fmt;
}
else
{
format = AFMT_S16_NE;
}
/* verify sample rate */
if (rte == 0)
{
sample_rate = DB_SAMPLE_RATE;
}
else
{
if ((rte >= 8000) && (rte < 49000))
{
sample_rate = rte;
}
else
{
errno = ERROR_BAD_SAMPLERATE;
goto cleanup;
}
}
/* verify number of channels */
switch(numch)
{
case 0:
num_channels = 2;
break; /* default to stereo */
case 1:
num_channels = 1;
break; /* mono */
case 2:
num_channels = 2;
break; /* stereo */
default:
errno = ERROR_BAD_NUMCH;
goto cleanup;
}
/* Get system data */
{
sysshmid = shmget((key_t) DB_SYSTEM_SM_KEY, sizeof(dbfsd_data),
0666 | O_RDWR );
if (sysshmid == -1)
{
Error("DBAudioLib ERROR: could not create shared memory for system data.\n Hint: Make sure that dbfsd has been started.\n errno: ");
errno = ERROR_INIT_FAILURE;
goto cleanup;
}
sysdata = shmat(sysshmid,(void *)0, 0);
if ((int)sysdata == -1)
{
Error("DBAudioLib ERROR: error attaching system data shared memory.");
errno = ERROR_INIT_FAILURE;
goto cleanup;
}
/* verify that there is a free channel into dbfsd */
if (sysdata->free_channel_index == -1)
{
ch = NULL;
Error("DBAudio_Init: no free channels.");
if (shmdt(sysdata) == -1)
{
Error("DBAudio_Init: could not detach system data memory segment.");
errno = ERROR_INIT_FAILURE;
goto cleanup;
}
errno = ERROR_NO_FREE_CHANNELS;
goto cleanup;
}
}
/* End get system data */
/* retrieve the channel memory id */
{
shmid = shmget((key_t) DB_CHANNELS_SM_KEY,
(sysdata->num_channels * sizeof(local_channel)), 0666);
if (shmid == -1)
{
Error("DBAudioLib ERROR: error creating channel shared memory.");
errno = ERROR_INIT_FAILURE;
goto cleanup;
}
else
{
Debug("DBAudioLib: shmid is: %d ",shmid);
}
/* attach the channel memory segment*/
local_channels = (local_channel *) shmat(shmid,(void *)0, 0);
if ((int)local_channels == -1)
{
Error("DBAudioLib ERROR: error attaching channel shared memory.");
errno = ERROR_INIT_FAILURE;
goto cleanup;
}
/* get channel id */
if (chindex == 0)
{
ch = &(local_channels[sysdata->free_channel_index]);
}
else
{
/* chindex is begins at 1, so decrement to directly reference the
channel array */
chindex--;
if ((chindex < 0) || (chindex > sysdata->num_channels))
{
errno = ERROR_BAD_CHANNEL_ID;
goto cleanup;
}
if (local_channels[chindex].free_channel)
{
ch = &(local_channels[chindex]);
}
}
Debug("DBAudioLib: opening comm pipe: %s",ch->comm_filename);
/* open comm pipe to server */
if ((ch->client_comm_fd = open(ch->comm_filename, O_WRONLY)) == -1)
{
Debug("%s",ch->comm_filename);
DBAudio_perror("DBAudioLib: Error opening comm pipe.");
errno = ERROR_INIT_FAILURE;
goto cleanup;
}
Debug("comm fd is: %d\n",ch->client_comm_fd);
if (CUE_ENABLED)
{
printf("flags is %d\n",ch->channel_flags);
/* open cue_pipe to be NON BLOCKING -- very important since the server does
not check the cue channel with its select() call*/
Debug("DBAudioLib: opening cue pipe: %s",ch->cue_filename);
if ((ch->client_cue_fd = open(ch->cue_filename, O_WRONLY | O_NONBLOCK)) == -1)
{
Debug("%s",ch->cue_filename);
DBAudio_perror("DBAudioLib: Error opening cue pipe.");
errno = ERROR_INIT_FAILURE;
goto cleanup;
}
Debug("Cue fd is: %d\n",ch->client_cue_fd);
}
ch->left_gain = DEFAULT_GAIN;
ch->right_gain = DEFAULT_GAIN;
ch->cue = FALSE;
ch->channel_type = type;
ch->base_pitch = DEFAULT_PITCH;
switch(ch->channel_type)
{
case PIPE_CHANNEL:
Debug("DBAudioLib: Channel type: PIPE CHANNEL");
break;
default:
break;
}
/* update channel name check against 48 to allow for channel index */
DBAudio_Set_Channel_Name(name);
} /* end retrieve the channel memory id */
DBAudio_Set_Rate(rte);
/* clear msg queue */
if (ch->msg_q_id != -1)
{
dbfsd_msg msg;
while (msgrcv(ch->msg_q_id, &msg,sizeof(dbfsd_msg) - sizeof(long int),0,IPC_NOWAIT) != -1)
{
Debug("DBAudio_Init: cleared message %d",msg.msg_type);
}
}
/* init message handler to NULL */
ch->message_handler = NULL;
/* init sample buffer */
{
ch->sampler_state = SAMPLER_OFF;
ch->sampler_bufsize = (ch->sampler_time /* number of seconds */
* (DB_SAMPLE_SIZE * DB_CHANNEL_NUM_CHANNELS) /* size of 1 stereo sample */
* DB_SAMPLE_RATE); /* num samples per second */
ch->sampler_size = 0;
ch->sampler_readoffset = 0;
ch->sampler_startoffset = 0;
ch->sampler_buf = (char *) malloc(ch->sampler_bufsize);
Debug("sampler bufsize is: %d",ch->sampler_bufsize);
}
/* write an empty buffer to the mixer to init this channel */
{
memset(init_buffer,0,PIPE_BUF);
ch->cue = TRUE;
Debug("writing init buffer... \n");
templen = PIPE_BUF;
if (DBAudio_Write(init_buffer,templen) == FAILURE)
{
DBAudio_perror("Error writing init data to channel:: ");
}
Debug("done\n");
ch->cue = FALSE;
Debug("DBAudioLib: dbaudio_init complete.");
}
cleanup:
/* ENOMSG is a side effect of opening/clearing the message queue, so remap it */
if (errno == ENOMSG) errno = SUCCESS;
/* if error, retur to initial state */
if (errno != SUCCESS)
{
ch = NULL;
if (local_channels != NULL)
{
shmdt(local_channels);
}
if (sysdata != NULL)
{
shmdt(sysdata);
}
return FAILURE;
}
return SUCCESS;
}
#ifdef __cplusplus
}
#endif
|