File: DBAudio_Sampler.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 (372 lines) | stat: -rw-r--r-- 7,764 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
/*

Author:  Bob Dean
Copyright (c) 1999, 2000, 2001


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 <sys/types.h>
#include <limits.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <sys/shm.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 */
		extern  int shmid, sysshmid;
		extern dbfsd_data * sysdata;
		extern local_channel * local_channels;
		/* static local_channel * cue_channels; */
		extern local_channel * ch;

		extern int debug_level; /* declared in debug module */

		/*
		  DBAudio_Sample_Record - enable sample recording for current audio stream
		 */
		int DBAudio_Sampler_Record()
		{
			int err;

			err = SUCCESS;

			/* init channel sample control variables */
			ch->sampler_size = 0;
			ch->sampler_readoffset = 0;
			ch->sampler_startoffset = 0;

			/* set record */
			ch->sampler_state = SAMPLER_RECORD;

			return err;
		}


		/*
		  DBAudio_Sample_Stop - stop sample recording/playback for current audio stream
		 */
		int DBAudio_Sampler_Stop()
		{
			int err;

			err = SUCCESS;

			ch->sampler_state = SAMPLER_READY;

			return err;
		}

		/*
		  DBAudio_Sampler_Play_Loop - enable looping sample playback
		 */
		int DBAudio_Sampler_Loop()
		{
			int err;

			err = SUCCESS;

			/* if we are already playing a single sample, reset.
			 otherwise, start loop at current position in sample playback */
			/* Note that the loop state is not checked for, this will allow
               the user to easially restart the loop at the begining of the
               sample. Sometimes a neat DJ effect. */
			if (ch->sampler_state != SAMPLER_PLAY_SINGLE)
			{
				ch->sampler_readoffset = ch->sampler_startoffset;
			}

			ch->sampler_state = SAMPLER_PLAY_LOOP;

			return err;
		}


		/*
		  DBAudio_Sampler_Play_Single - enable a single sample playback
		 */
		int DBAudio_Sampler_Single()
		{
			int err;

			err = SUCCESS;

			/* sampler mode is not checked here, as it is assumed that 
               the User wants the single shot of a sample to override all others */
			ch->sampler_readoffset = ch->sampler_startoffset;
			ch->sampler_state = SAMPLER_PLAY_SINGLE;

			return err;
		}

		/*
		  DBAudio_Sample_Get_Offsets - get the start and end offsets within the
               sample buffer
		 */
		int DBAudio_Sampler_Get_Offsets(int * start_offset, int * end_offset)
		{
			int err;

			err = SUCCESS;

			/* convert char buffer offsets into number of samples */
			*start_offset = ch->sampler_startoffset / 4;
			*end_offset = ch->sampler_endoffset / 4;

			return err;
		}

		/*
		  DBAudio_Sample_Set-OFfsets - set the start and end offsets within the
                sample buffer
		 */
		int DBAudio_Sampler_Set_Offsets(int start_offset, int end_offset)
		{
			int err;

			err = SUCCESS;

			/* convert number of samples to char buffer indexes */
			start_offset *= 4;
			end_offset *= 4;
			
			if (start_offset > end_offset)
			{
				err = ERROR_BAD_PARAM;
				goto cleanup;
			}

			if (start_offset > ch->sampler_size)
			{
				err = ERROR_BAD_PARAM;
				goto cleanup;
			}

			ch->sampler_startoffset = start_offset;
			ch->sampler_endoffset = end_offset;

		cleanup:

			return err;
		}


		/*
		  DBAudio_Sampler_Get_Size - Get total number of samples in sample buffer
		 */
		int DBAudio_Sampler_Get_Size(int * size)
		{
			int err;

			err = SUCCESS;

			/* convert size of char buffer to number of samples */
			*size = ch->sampler_size / 4;

			return err;
		}

		/*
		  DBAudio_Sampler_Get_State - Get the current state of the sampler
		 */
	    sampler_state DBAudio_Sampler_Get_State()
		{
			return ch->sampler_state;
		}

		/*
		  DBAudio_Sample_Save - save sample using provided filename
		*/
		int DBAudio_Sampler_Save(char * filename)
		{
			int    err;
			int    fid;
			char   header[512];
			int    count;
			char * tempbuf;

			err = SUCCESS;
			fid = 0;

			/* open output file */
			if ((fid = open(filename,O_CREAT | O_WRONLY,0777)) == -1)
			{
				perror("DBAudiolib Sample Save: file creation failed.");
				err = -1;
				goto cleanup;
			}

/* 			Debug("\nSAVE SAMPLE HEADER: "); */
/* 			Debug("time:        %f",ch->sampler_time); */
/* 			Debug("bufsize:     %d",ch->sampler_bufsize); */
/* 			Debug("size:        %d",ch->sampler_size); */
/* 			Debug("readoffset:  %d",ch->sampler_readoffset); */
/* 			Debug("startoffset: %d",ch->sampler_startoffset); */
/* 			Debug("endoffset:   %d",ch->sampler_endoffset); */

			/* create header */
			sprintf(header,"%s %f %d %d %d %d %d %c",
					"DBMix ",
					ch->sampler_time,
					ch->sampler_bufsize,
					ch->sampler_size,
					ch->sampler_readoffset,
					ch->sampler_startoffset,
					ch->sampler_endoffset,
					'\0');

			/* write header */
			if ((err = write(fid,header,512)) == -1)
			{
				perror("DBAudiolib Sample Save: header write failed");
				goto cleanup;
			}
			
			count = 0;
			tempbuf = ch->sampler_buf;

			/* write sample */
			while (tempbuf < (ch->sampler_buf + ch->sampler_bufsize))
			{
				count = 4096;
				
				if ((err = write(fid,tempbuf,count)) == -1)
				{
					perror("DBAudiolib Sample Save: sample write failed");
					goto cleanup;
				}

				/* incr pointer by number of bytes written */
				tempbuf += err;
			}

			/* tell mixer that op is done */
			sysdata->sampler_op_flag = 0;

		cleanup:

			if (fid != 0)
			{
				close(fid);
			}

			return err;
		}


		/*
		  DBAudio_Sample_Load - load sample using provided filename
		 */
		int DBAudio_Sampler_Load(char * filename)
		{
			int    err;
			int    fid;
			char   header[512];
			char   dbmixname[128];
			char * tempbuf;
			int    count;
			int    num_left;
			
			err = SUCCESS;
			fid = 0;

			/* open output file */
			if ((fid = open(filename,O_RDONLY)) == -1)
			{
				err = -1;
				perror("DBAudiolib Sample Load: open failed");
				goto cleanup;
			}

			/* read header */
			if ((err = read(fid,header,512)) == -1)
			{
				perror("DBAudiolib Sample Load: header read failed.");
				goto cleanup;
			}

			/* parse header */
			sscanf(header,"%s %f %d %d %d %d %d",
				   dbmixname,
				   &(ch->sampler_time),
				   &(ch->sampler_bufsize),
				   &(ch->sampler_size),
				   &(ch->sampler_readoffset),
				   &(ch->sampler_startoffset),
				   &(ch->sampler_endoffset));

			count = 0;
			tempbuf = ch->sampler_buf;
			num_left = ch->sampler_bufsize;

			while (num_left > 0)
			{
				if (num_left > 4096)
				{
					count = 4096;
				}
				else
				{
					count = num_left;
				}

				if ((err = read(fid,tempbuf,count)) == -1)
				{
					perror("DBAudiolib Sample Load: read failed!!!");
					goto cleanup;
				}

				tempbuf += err;
				num_left -= err;
			}

			/* tell mixer that operation is done */
			sysdata->sampler_op_flag = 0;

			ch->sampler_state = SAMPLER_READY;

		cleanup:

			if (fid != 0)
			{
				close(fid);
			}

			return err;	
		}


#ifdef __cplusplus
	}
#endif