Welcome to mirror list, hosted at ThFree Co, Russian Federation.

bs2bstream.c « src - github.com/alexmarsev/libbs2b.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58c759432790365426eed8629258cbfb6a671617 (plain)
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
/*-
 * Copyright (c) 2005 Boris Mikhaylov
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

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

#if defined( _O_BINARY ) || defined( _O_RAW )
#include <io.h>
#endif

#include "bs2b.h"

static void print_usage( char *progname )
{
	fprintf( stderr, "\n"
		"Bauer stereophonic-to-binaural DSP stream converter. Version %s\n"
		"Stereo interleaved LPCM raw data stdin-stdout converting.\n\n",
		BS2B_VERSION_STR );
	fprintf( stderr,
		"Usage : %s [-h] [-u] [-e E] [-b B] [-r R] [-l L|(L1 L2)]\n",
		progname );
	fprintf( stderr,
		"-h - this help.\n"
		"-u - unsigned data. Default is signed.\n"
		"-e - endians, E = b|l|n (big|little|native). Default is native.\n"
		"-b - bits per integer sample, B = 8|16|24|32. Default is 16 bit.\n"
		"-r - sample rate, R = <value by kHz>. Default is %.3f kHz.\n"
		"-l - crossfeed level, L = d|c|m:\n"
		"     d - default preset     - 700Hz/260us, 4.5 dB;\n"
		"     c - Chu Moy's preset   - 700Hz/260us, 6.0 dB;\n"
		"     m - Jan Meier's preset - 650Hz/280us, 9.5 dB.\n"
		"     Or L1 = [%d..%d] mB of feed level (%d..%d dB)\n"
		"     and L2 = [%d..%d] Hz of cut frequency.\n",
		BS2B_DEFAULT_SRATE / 1000.0,
		BS2B_MINFEED, BS2B_MAXFEED, BS2B_MINFEED / 10, BS2B_MAXFEED / 10,
		BS2B_MINFCUT, BS2B_MAXFCUT );
} /* print_usage() */

int main( int argc, char *argv[] )
{
	int i;
	char *progname, *tmpstr;

	t_bs2bdp bs2bdp;

	uint32_t srate = BS2B_DEFAULT_SRATE;
	uint32_t level = BS2B_DEFAULT_CLEVEL;
	int bits = 16;
	int unsigned_flag = 0;
	int endians = 'n';

	tmpstr = strrchr( argv[0], '/' );
	tmpstr = tmpstr ? tmpstr + 1 : argv[ 0 ];
	progname = strrchr( tmpstr, '\\' );
	progname = progname ? progname + 1 : tmpstr;

	for( i = 1; i < argc; i++ )
	{
		if( '-' == argv[ i ][ 0 ] )
		{
			switch( argv[ i ][ 1 ] )
			{
			case 'h':
				print_usage( progname );
				return 1;

			case 'u':
				unsigned_flag = 1;
				break;

			case 'e':
				if( ++i >= argc )
				{
					print_usage( progname );
					return 1;
				}
				endians = argv[ i ][ 0 ];
				if( endians != 'n' &&
					endians != 'b' &&
					endians != 'l' )
				{
					print_usage( progname );
					return 1;
				}
				break;

			case 'b':
				if( ++i >= argc )
				{
					print_usage( progname );
					return 1;
				}
				bits = atoi( argv[ i ] );
				if( bits != 8 &&
					bits != 16 &&
					bits != 24 &&
					bits != 32 )
				{
					print_usage( progname );
					return 1;
				}
				break;

			case 'r':
				if( ++i >= argc )
				{
					print_usage( progname );
					return 1;
				}
				srate = ( uint32_t )( atof( argv[ i ] ) * 1000.0 );
				if( srate < BS2B_MINSRATE || srate > BS2B_MAXSRATE )
				{
					print_usage( progname );
					return 1;
				}
				break;

			case 'l':
				if( ++i >= argc )
				{
					print_usage( progname );
					return 1;
				}
				switch( argv[ i ][ 0 ] )
				{
				case 'd':
					level = BS2B_DEFAULT_CLEVEL;
					break;
				case 'c':
					level = BS2B_CMOY_CLEVEL;
					break;
				case 'm':
					level = BS2B_JMEIER_CLEVEL;
					break;
				default:
					{
						int feed, fcut;

						feed = atoi( argv[ i ] );
						if( ++i >= argc )
						{
							print_usage( progname );
							return 1;
						}
						fcut = atoi( argv[ i ] );
						if( feed < BS2B_MINFEED || feed > BS2B_MAXFEED ||
							fcut < BS2B_MINFCUT || fcut > BS2B_MAXFCUT )
						{
							print_usage( progname );
							return 1;
						}
						level = ( ( uint32_t )feed << 16 ) | ( uint32_t )fcut;
					}
				} /* switch */
				break;

			default:
				print_usage( progname );
				return 1;
			} /* swith */
		}
		else
		{
			print_usage( progname );
			return 1;
		}
	} /* for */

	#if defined( _O_BINARY )
	_setmode( _fileno( stdin ),  _O_BINARY );
	_setmode( _fileno( stdout ), _O_BINARY );
	#elif defined( _O_RAW )
	_setmode( _fileno( stdin ),  _O_RAW );
	_setmode( _fileno( stdout ), _O_RAW );
	#endif

	bs2bdp = bs2b_open();

	bs2b_set_srate( bs2bdp, srate );
	bs2b_set_level( bs2bdp, level );

	fprintf( stderr,
		"Crossfeed level:  %.1f dB, %d Hz, %d us.\n"
		"LPCM stream:      %d Hz, %d bits, %s, byte order '%c'.\n",
		( double )bs2b_get_level_feed( bs2bdp ) / 10.0,
		bs2b_get_level_fcut( bs2bdp ), bs2b_get_level_delay( bs2bdp ),
		bs2b_get_srate( bs2bdp ),
		bits, unsigned_flag ? "unsigned" : "signed", endians );

	switch( bits )
	{
	case 8:
		{
			int8_t sample[ 2 ];
			while( 2 == fread( sample, sizeof( int8_t ), 2, stdin ) )
			{
				if( unsigned_flag )
					bs2b_cross_feed_u8( bs2bdp, ( uint8_t * )sample, 1 );
				else
					bs2b_cross_feed_s8( bs2bdp, sample, 1 );

				fwrite( sample, sizeof( int8_t ), 2, stdout );
			} /* while */
		}
		break;

	case 16:
		{
			int16_t sample[ 2 ];
			while( 2 == fread( sample, sizeof( int16_t ), 2, stdin ) )
			{
				switch( endians )
				{
				case 'b':
					{
						if( unsigned_flag )
							bs2b_cross_feed_u16be( bs2bdp, ( uint16_t * )sample, 1 );
						else
							bs2b_cross_feed_s16be( bs2bdp, sample, 1 );
					}
					break;

				case 'l':
					{
						if( unsigned_flag )
							bs2b_cross_feed_u16le( bs2bdp, ( uint16_t * )sample, 1 );
						else
							bs2b_cross_feed_s16le( bs2bdp, sample, 1 );
					}
					break;

				default:
					{
						if( unsigned_flag )
							bs2b_cross_feed_u16( bs2bdp, ( uint16_t * )sample, 1 );
						else
							bs2b_cross_feed_s16( bs2bdp, sample, 1 );
					}
					break;
				} /* switch( endians ) */

				fwrite( sample, sizeof( int16_t ), 2, stdout );
			} /* while */
		}
		break;

	case 24:
		{
			bs2b_int24_t sample[ 2 ];
			while( 2 == fread( sample, sizeof( bs2b_int24_t ), 2, stdin ) )
			{
				switch( endians )
				{
				case 'b':
					{
						if( unsigned_flag )
							bs2b_cross_feed_u24be( bs2bdp, ( bs2b_uint24_t * )sample, 1 );
						else
							bs2b_cross_feed_s24be( bs2bdp, sample, 1 );
					}
					break;

				case 'l':
					{
						if( unsigned_flag )
							bs2b_cross_feed_u24le( bs2bdp, ( bs2b_uint24_t * )sample, 1 );
						else
							bs2b_cross_feed_s24le( bs2bdp, sample, 1 );
					}
					break;

				default:
					{
						if( unsigned_flag )
							bs2b_cross_feed_u24( bs2bdp, ( bs2b_uint24_t * )sample, 1 );
						else
							bs2b_cross_feed_s24( bs2bdp, sample, 1 );
					}
					break;
				} /* switch( endians ) */

				fwrite( sample, sizeof( bs2b_int24_t ), 2, stdout );
			} /* while */
		}
		break;

	case 32:
		{
			int32_t sample[ 2 ];
			while( 2 == fread( sample, sizeof( int32_t ), 2, stdin ) )
			{
				switch( endians )
				{
				case 'b':
					{
						if( unsigned_flag )
							bs2b_cross_feed_u32be( bs2bdp, ( uint32_t * )sample, 1 );
						else
							bs2b_cross_feed_s32be( bs2bdp, sample, 1 );
					}
					break;

				case 'l':
					{
						if( unsigned_flag )
							bs2b_cross_feed_u32le( bs2bdp, ( uint32_t * )sample, 1 );
						else
							bs2b_cross_feed_s32le( bs2bdp, sample, 1 );
					}
					break;

				default:
					{
						if( unsigned_flag )
							bs2b_cross_feed_u32( bs2bdp, ( uint32_t * )sample, 1 );
						else
							bs2b_cross_feed_s32( bs2bdp, sample, 1 );
					}
					break;
				} /* switch( endians ) */

				fwrite( sample, sizeof( int32_t ), 2, stdout );
			} /* while */
		}
		break;

	default:
		break;
	} /* switch( bits ) */

	bs2b_close( bs2bdp );
	bs2bdp = 0;

	return 0 ;
} /* main() */