File: dbcat.c

package info (click to toggle)
dbmix 0.9.8-6.3
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 2,896 kB
  • sloc: ansic: 12,309; sh: 11,418; makefile: 153
file content (396 lines) | stat: -rw-r--r-- 8,461 bytes parent folder | download | duplicates (6)
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
/*
  DB Concatenate
  ==============

  DBCat takes in sound data, converts it to the 16bit unsigned 44.1kHz format
  and then sends the data to the DB Fourier Synthesis Daemon.  

  DBCat sends the data to whichever dbfsd pipe is free at the time. If no
  pipe is free, the program will exit with an error message.

  Version: B.2
  Author:  Bob Dean
  Copyright (c) 1999


   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.

*/



#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <limits.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <dlfcn.h> /* functions used to dynamically link to libs */

#include <dbaudiolib.h>
#include <dbdebug.h>

#define DBCAT_DEFAULT_NAME "DBCat"
#define DBCAT_SAMPLE_NAME  "DBSamp"

extern int debug_level;

int  loop_flag;
int  sample_flag;
int  in_fd,out_fd;
int  numch,sample_size,sample_rate,chindex;
enum channel_type_e type;

int pause_flag;

#ifdef USE_DYNAMIC_LIBRARY
DBAudioLibFunctions * dbaudiofxns;
void * h;
#endif

/***************************************************************************/
/*  Initialization functions                                               */
/***************************************************************************/

/*
  print_help - outputs help to the console. 

  inputs - none
  outputs - text to the console.
 */

void print_version()
{
	printf("DBCat - sound concatenation utility for the DB Fourier Synthesis Daemon\n");
	printf("%s\n",DBMIX_COPYRIGHT);
	printf("DBMix Version: %s\n",DBMIX_VERSION);
}

void print_help()
{
	print_version();

	printf("\n");
	printf("Supported options [defaults in brackets]:\n");
	printf("   -d    turn on debug messages to console      [Off]\n");
	printf("   -v    print version information.             [None]\n");
	printf("   -m    input data is mono.                    [Stereo]\n");
	printf("   -r N  input data has a sampling rate of N Hz [44100 Hz]\n");
	printf("   -8    input data is 8 bit unsigned samples   [16 bit signed]\n");
	printf("   -c X  use channel X if available             [first available]\n");
	printf("   -S    sample only mode.                      [none]\n");

	printf("\n");
}



/*
  parse_cmdline - steps through argv and sets all the variables

  inputs - argc argv
  outputs - sets system variables
 */
void parse_cmdline(int argc, char* argv[])
{
	int opt;
   
	in_fd = 0;
	debug_level = 0;
	numch = 2;
	sample_size = 16;
	sample_rate = 44100;
	chindex = 0;
	pause_flag = 0;
	sample_flag = 0;

	while((opt = getopt(argc,argv,"hvdr:c:m8S")) != -1)
	{
		switch(opt)
		{
			case 'd':
				debug_level = 1;
				break;
			case 'h':
				print_help();
				exit(0);
				break;
			case 'm':
				numch = 1;
				break;
			case '8':
				sample_size = 8;
				break;
			case 'r':
				sample_rate = atoi(optarg);
				Debug("sample_rate is %d\n",sample_rate);
				break;
			case 'c':
				chindex = atoi(optarg);
				Debug("chindex is %d\n",chindex);
				break;
			case 'v':
				print_version();
			    exit(0);
			case 'S':
				sample_flag = 1;
				Debug("Set sample only mode\n");
				break;
			case ':':
				Debug("option needs a value\n");
				exit(0);
			case '?':
				Debug("unknown option: %c\n",optopt);
				exit(0);
			default: break;
		}
	}
}


/*
  sigexit - changes system variables to force clean shutdown
    inputs - none
    outputs - none
 */
void sigexit(int signum)
{
	loop_flag = FALSE;

#ifdef USE_DYNAMIC_LIBRARY
	if(dbaudiofxns != NULL) dbaudiofxns->DBAudio_Close();
	dlclose(h);
#else
	DBAudio_Close();
#endif

	Debug("DBCat has completed. Meow.\n");
	exit(0);
}



/* function to sleep a thread for usec microseconds*/
void dbcat_usleep(gint usec)
{
	struct timeval tv;
	
	tv.tv_sec = usec / 1000000;
	usec -= tv.tv_sec * 1000000;
	tv.tv_usec = usec;
	select(0, NULL, NULL, NULL, &tv);
}


void dbcat_message_handler(dbfsd_msg msg)
{	
	switch (msg.msg_type)
	{
		case DBMSG_PAUSE:
			pause_flag = 1;
			DBAudio_Pause(pause_flag);
			break;
		case DBMSG_UNPAUSE:
			pause_flag = 0;
			DBAudio_Pause(pause_flag);
			break;
		case DBMSG_PLAY:
			pause_flag = 0;
			DBAudio_Pause(pause_flag);
			break;
		case DBMSG_MUTE:

			break;
		case DBMSG_UNMUTE:

			break;
		case DBMSG_STOP:
			break;
		case DBMSG_EJECT:
			break;
		case DBMSG_REWIND:
			break;
		case DBMSG_FFORWARD:
			break;
		case DBMSG_NEXT:
			break;
		case DBMSG_PREV:
			break;
		default:
			Error("dbcat_message_handler: unknown or unsupported IPC message %d\n ",msg);
	}

	return;
}



/***************************************************************************/
/*                MAIN                                                     */
/***************************************************************************/

int main(int argc, char **argv)
{
	char   buf[PIPE_BUF*2];
	char * dbcat_name;
	int    len,error,count;

#ifdef USE_DYNAMIC_LIBRARY	
	void * (*get_fxns) (); /* define function pointer to hold pntr to DBAudio_Get_Functions() */
	
	dbaudiofxns = NULL;
#endif

	debug_level = 0;
	type = PIPE_CHANNEL;
	pause_flag = 0;

	memset(buf,0,PIPE_BUF*2);

	/*install signal handlers*/
	signal(SIGINT,sigexit);
	signal(SIGTERM,sigexit);

	parse_cmdline(argc,argv);

	if (sample_flag)
	{
		dbcat_name = DBCAT_SAMPLE_NAME;
	}
	else
	{
		dbcat_name = DBCAT_DEFAULT_NAME;
	}

#ifdef USE_DYNAMIC_LIBRARY	
	Debug("USING DLOPEN()");
	/* link against DBAudioLib and retrieve fucntion structure */
	if((h = dlopen("libdbaudiolib.so",RTLD_NOW)) == NULL) 
	{
		printf("Failed to open DBAudioLib:: %s\n",dlerror());
		return 1;
	}

	if((get_fxns = dlsym(h,"DBAudio_Get_Functions")) == NULL)
	{
		printf("Failed to retrieve pntr to DBAudio_Get_Functions: %s\n",dlerror());
		return 1;
	}

	dbaudiofxns = (DBAudioLibFunctions*)get_fxns();

	error = dbaudiofxns->DBAudio_Init(dbcat_name,sample_size,sample_rate,numch,type,chindex);
/* 	error = DBAudio_Init("Dbcat",sample_size,sample_rate,numch,type,chindex); */
#else
	Debug("USING SHARED LIBRARY");
	error = DBAudio_Init(dbcat_name,sample_size,sample_rate,numch,type,chindex);
#endif

	if(error != SUCCESS)
	{
		printf("Error. Is dbfsd running?\n");
		return 1;
	}
	
#ifdef USE_DYNAMIC_LIBRARY
	dbaudiofxns->DBAudio_Set_Message_Handler(dbcat_message_handler,DBMSG_ALL);
#else
	DBAudio_Set_Message_Handler(dbcat_message_handler,DBMSG_ALL);
#endif

	/*init variables for main loop*/
	loop_flag = TRUE;
	count = 0;

	/*main loop*/
	while(loop_flag)
    {
		char c1;

		if (pause_flag)
		{
#ifdef USE_DYNAMIC_LIBRARY
			dbaudiofxns->DBAudio_Handle_Message_Queue();
#else
			DBAudio_Handle_Message_Queue();
#endif
			usleep(50000);
		}
		else
		{
			/* if not in sample mode, read in data */
			if (!sample_flag)
			{
				/* read in data, adjusting for count bytes already in the buffer */
				if((len = read(in_fd,buf+count,PIPE_BUF-count)) < 0)
				{
					loop_flag = FALSE;
					
#ifdef USE_DYNAMIC_LIBRARY
					dbaudiofxns->DBAudio_Close();
					dlclose(h);
#else
					DBAudio_Close();
#endif
				}

				len+=count;
			}
			else
			{
				/* sample mode, write full empty buffer */
				len = PIPE_BUF*2;
			}
			
#ifdef USE_DYNAMIC_LIBRARY
			if((count = dbaudiofxns->DBAudio_Write(buf,len)) == FAILURE)
#else
			if((count = DBAudio_Write(buf,len)) == FAILURE)	
#endif
			{
				perror("DBCAT: DBAudio_Write failed::");
			}
			
			/* if we could not write the full buffer, copy the unwritten
			   data to the front of the buffer */
			if((count < len) && (count > 0))
			{
				c1 = *(buf+count);
				memmove(buf,buf+count,len - count);
				count = len - count;
			}
			else
			{
				/* if we could not write *any* data, we are probably PAUSED
				   adjust count to compensate */
				if (count == 0)
				{
					count = PIPE_BUF;
				}
				else
				{
					count = 0;
				}
			}
		}
    }

	return 0;
}