summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/audio/audioIO_AIX.cpp
blob: 15316852e0eb5e77c28696b6e5bf65481663b451 (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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
 * AIX audio - griff@acm.org 02aug2000
 * tested on 43P 260 with builtin audio
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
/* A conflict within AIX 4.3.3 <sys/> headers and probably others as well.
 * I guess nobody ever uses audio... Shame over AIX header files.  */
#include <sys/machine.h>
#undef BIG_ENDIAN
#include <sys/audio.h>

static int audio_fd;

static void debugUpdate( unsigned long& flags, long& bsize );

#ifndef AUDIO_BIG_ENDIAN 
#define AUDIO_BIG_ENDIAN BIG_ENDIAN
#endif



int audioConstruct() {
  printf("audioConstruct AIX ********\n");
  audio_fd=-1;
  return true;
}


void audioDestruct() {
  
}

int audioOpen()
{
    char devname[14];
    for ( int dev=0; dev<4; dev++ )
    {
	for ( int chan=1; chan<8; chan++ )
	{
	    sprintf(devname,"/dev/paud%d/%d",dev,chan);
	    audio_fd = open (devname, O_WRONLY, 0);
	    if ( audio_fd >= 0 )
	    {
		return 1;
	    }
	    sprintf(devname,"/dev/baud%d/%d",dev,chan);
	    audio_fd = open (devname, O_WRONLY, 0);
	    if ( audio_fd >= 0 )
	    {
		return 1;
	    }
	}
    }

    fprintf(stderr, "Could not open AIX audio device, faking\n" );
    return 1;
}

int getAudioBufferSize()
{
    audio_buffer  paud_bufinfo;

    if( audio_fd < 0 ) return 1024*65;

    if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 )
    {
	perror("ioctl getAudioBufferSize using default");
        return 1024*65;
    }

    /*
     * Do you need the total capacity or the current capacity?
     * This is the total capacity:
     */
    return paud_bufinfo.write_buf_cap;
    /*
     * This is the current capacity:
     * return (paud_bufinfo.write_buf_cap - paud_bufinfo.write_buf_size);
     */
}

void audioInit(int sampleSize,int frequency, int stereo, int sign, int bigendian )
{
    // int           format;
    int           bytes_per_sample;
    audio_init    paud_init;
    audio_buffer  paud_bufinfo;
    // audio_status  paud_status;
    audio_control paud_control;
    audio_change  paud_change;

    if( audio_fd < 0 ) return;

    /*
     * We can't set the buffer size - just ask the device for the maximum
     * that we can have.
     */
    if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 )
    {
        perror("Couldn't get audio buffer information");
        return;
    }

    /*
     * Fields in the audio_init structure:
     *
     * Ignored by us:
     *
     * paud.loadpath[LOAD_PATH]; * DSP code to load, MWave chip only?
     * paud.slot_number;         * slot number of the adapter
     * paud.device_id;           * adapter identification number
     *
     * Input:
     *
     * paud.srate;           * the sampling rate in Hz
     * paud.bits_per_sample; * 8, 16, 32, ...
     * paud.bsize;           * block size for this rate
     * paud.mode;            * ADPCM, PCM, MU_LAW, A_LAW, SOURCE_MIX
     * paud.channels;        * 1=mono, 2=stereo
     * paud.flags;           * FIXED - fixed length data
     *                       * LEFT_ALIGNED, RIGHT_ALIGNED (var len only)
     *                       * TWOS_COMPLEMENT - 2's complement data
     *                       * SIGNED - signed? comment seems wrong in sys/audio.h
     *                       * BIG_ENDIAN
     * paud.operation;       * PLAY, RECORD
     *
     * Output:
     *
     * paud.flags;           * PITCH            - pitch is supported
     *                       * INPUT            - input is supported
     *                       * OUTPUT           - output is supported
     *                       * MONITOR          - monitor is supported
     *                       * VOLUME           - volume is supported
     *                       * VOLUME_DELAY     - volume delay is supported
     *                       * BALANCE          - balance is supported
     *                       * BALANCE_DELAY    - balance delay is supported
     *                       * TREBLE           - treble control is supported
     *                       * BASS             - bass control is supported
     *                       * BESTFIT_PROVIDED - best fit returned
     *                       * LOAD_CODE        - DSP load needed
     * paud.rc;              * NO_PLAY         - DSP code can't do play requests
     *                       * NO_RECORD       - DSP code can't do record requests
     *                       * INVALID_REQUEST - request was invalid
     *                       * CONFLICT        - conflict with open's flags
     *                       * OVERLOADED      - out of DSP MIPS or memory
     * paud.position_resolution; * smallest increment for position
     */

    paud_init.srate = frequency;
    paud_init.mode = PCM;
    paud_init.operation = PLAY;
    paud_init.channels = (stereo?2:1);

    /*
     * options in AIX:
     * paud_init.bits_per_sample: 8 | 16
     * paud_init.flags: AUDIO_BIG_ENDIAN (not used here)
     *                  SIGNED (always used here)
     *                  TWOS_COMPLEMENT (always on for Linux dsp porting?)
     *                  FIXED           <- that's right for SDL
     *                  or LEFT_ALIGNED <- that's right for mpeglib
     *                  or RIGHT_ALIGNED
     * paud_init.bsize: sample byte size,
     *                  bits_per_sample * (stereo?2:1)     - for SDL
     *                  bits_per_sample * (stereo?2:1) * 2 - for mpeglib
     */
    if ( sampleSize == 8 )
    {
        /* AFMT_S8 in linux dsp */
        bytes_per_sample = 2; // why not 1 ?
        paud_init.bits_per_sample = 8;
        paud_init.flags = TWOS_COMPLEMENT | LEFT_ALIGNED;
    }
    else
    {
        /* AFMT_S16_LE in linux dsp */
        bytes_per_sample = 4; // why not 2 ?
        paud_init.bits_per_sample = 16;
        paud_init.flags = TWOS_COMPLEMENT | LEFT_ALIGNED;
    }
    if( sign )      paud_init.flags |= SIGNED;
    if( bigendian ) paud_init.flags |= AUDIO_BIG_ENDIAN;

    paud_init.bsize = bytes_per_sample * (stereo?2:1);

#if 0
    debugUpdate(paud_init.flags, paud_init.bsize);

    printf("CG: sampleSize              = %d\n", sampleSize);
    printf("CG: frequency               = %d\n", frequency);
    printf("CG: stereo                  = %s\n", (stereo)?"y":"n");
    printf("CG: mode                    = %s\n", "PCM");
    printf("CG: channels                = %d\n", paud_init.channels);
    printf("CG: bsize                   = %d\n", paud_init.bsize);
    printf("CG: bits_per_sample         = %d\n", paud_init.bits_per_sample);
    printf("CG: flags & BIG_ENDIAN      = %s\n", ((paud_init.flags&AUDIO_BIG_ENDIAN)?"y":"n"));
    printf("CG: flags & SIGNED          = %s\n", ((paud_init.flags&SIGNED)?"y":"n"));
    printf("CG: flags & TWOS_COMPLEMENT = %s\n", ((paud_init.flags&TWOS_COMPLEMENT)?"y":"n"));
    printf("CG: flags & FIXED           = %s\n", ((paud_init.flags&FIXED)?"y":"n"));
    printf("CG: flags & LEFT_ALIGNED    = %s\n", ((paud_init.flags&LEFT_ALIGNED)?"y":"n"));
    printf("CG: flags & RIGHT_ALIGNED   = %s\n", ((paud_init.flags&RIGHT_ALIGNED)?"y":"n"));
#endif

    /*
     * We know the buffer size and the max number of subsequent writes
     * that can be pending. If more than one can pend, allow the application
     * to do something like double buffering between our write buffer and
     * the device's own buffer that we are filling with write() anyway.
     *
     * We can calculate the number of samples that fit into the audio
     * device buffer if that is necessary:
     *
     * samples_capacity = paud_bufinfo.write_buf_cap
     *                  / bytes_per_sample
     *                  / (stereo?2:1);
     * if ( paud_bufinfo.request_buf_cap != 1 ) samples_capacity /= 2;
     */

    /*
     * The AIX paud device init can't modify the values of the audio_init
     * structure that we pass to it. So we don't need any recalculation
     * of this stuff and no reinit call as in linux SDL dsp and dma code.
     *
     * /dev/paud supports all of the encoding formats, so we don't need
     * to do anything like reopening the device, either.
     */
    if ( ioctl(audio_fd, AUDIO_INIT, &paud_init) < 0 )
    {
        switch ( paud_init.rc )
        {
        case 1 :
            perror("Couldn't set audio format: DSP can't do play requests");
            return;
            break;
        case 2 :
            perror("Couldn't set audio format: DSP can't do record requests");
            return;
            break;
        case 4 :
            perror("Couldn't set audio format: request was invalid");
            return;
            break;
        case 5 :
            perror("Couldn't set audio format: conflict with open's flags");
            return;
            break;
        case 6 :
            perror("Couldn't set audio format: out of DSP MIPS or memory");
            return;
            break;
        default :
            perror("Couldn't set audio format: not documented in sys/audio.h");
            return;
            break;
        }
    }

    /*
     * Set some parameters: full volume, first speaker that we can find.
     * Ignore the other settings for now.
     */
    paud_change.input         = AUDIO_IGNORE; /* the new input source */
    paud_change.output        = OUTPUT_1;
					      /* EXTERNAL_SPEAKER,
                                               * INTERNAL_SPEAKER,
                                               * OUTPUT_1 */
    paud_change.monitor       = AUDIO_IGNORE; /* the new monitor state */
    paud_change.volume        = 0x7fffffff;   /* volume level [0-0x7fffffff] */
    paud_change.volume_delay  = AUDIO_IGNORE; /* the new volume delay */
    paud_change.balance       = 0x3fffffff;   /* the new balance */
    paud_change.balance_delay = AUDIO_IGNORE; /* the new balance delay */
    paud_change.treble        = AUDIO_IGNORE; /* the new treble state */
    paud_change.bass          = AUDIO_IGNORE; /* the new bass state */
    paud_change.pitch         = AUDIO_IGNORE; /* the new pitch state */

    paud_control.ioctl_request = AUDIO_CHANGE;
    paud_control.request_info  = (char*)&paud_change;
    if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 )
    {
        perror("Can't change audio display settings (ignoring)" );
    }

    /*
     * Tell the device to expect data. Actual start will wait for
     * the first write() call.
     */
    paud_control.ioctl_request = AUDIO_START;
    paud_control.position = 0;
    if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 )
    {
        perror("Can't start audio play");
        return;
    }
}


void audioSetVolume(int volume)
{
    long vol = (long)(volume/100.0) * 0x7fffffff;
    if( audio_fd < 0 ) return;

    audio_control paud_control;
    audio_change  paud_change;

    paud_change.input          = AUDIO_IGNORE; /* the new input source */
    paud_change.output         = OUTPUT_1;     /* EXTERNAL_SPEAKER,INTERNAL_SPEAKER,
                                                  OUTPUT_1 */
    paud_change.monitor        = AUDIO_IGNORE; /* the new monitor state */
    paud_change.volume         = vol;          /* volume level [0-0x7fffffff] */
    paud_change.volume_delay   = AUDIO_IGNORE; /* the new volume delay */
    paud_change.balance        = AUDIO_IGNORE; /* the new balance */
    paud_change.balance_delay  = AUDIO_IGNORE; /* the new balance delay */
    paud_change.treble         = AUDIO_IGNORE; /* the new treble state */
    paud_change.bass           = AUDIO_IGNORE; /* the new bass state */
    paud_change.pitch          = AUDIO_IGNORE; /* the new pitch state */

    paud_control.ioctl_request = AUDIO_CHANGE;
    paud_control.request_info  = (char*)&paud_change;

    if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 )
    {
	perror("Change audio volume failed");
    }
}

void audioFlush()
{
    if( audio_fd < 0 ) return;

    if ( ioctl(audio_fd, AUDIO_WAIT, NULL) < 0 )
    {
	perror("Flush audio buffers failed");
    }
}

void audioClose()
{
    if( audio_fd < 0 ) return;

    if ( ioctl(audio_fd, AUDIO_WAIT, NULL) < 0 )
    {
	perror("Flush audio buffers failed");
    }
    close(audio_fd);
}

int audioWrite(char *buffer, int count)
{
    int written = write(audio_fd, buffer, count);
    if( written < count )
    {
        return count;
    }

    return written;
}

int
getAudioFd()
{
    return audio_fd;
}

int mixerOpen()
{
  return true;
}

void mixerClose()
{
}

void mixerSetVolume(int leftVolume,int rightVolume)
{
    long balance;

    if( audio_fd < 0 ) return;

    balance = 2 * (leftVolume-rightVolume) / (leftVolume+rightVolume);
    balance = 0x3fffffff + balance*0x3fffffff;

    audio_control paud_control;
    audio_change  paud_change;

    paud_change.input = AUDIO_IGNORE;         /* the new input source */
    paud_change.output = OUTPUT_1;            /* EXTERNAL_SPEAKER,INTERNAL_SPEAKER,
						 OUTPUT_1 */
    paud_change.monitor = AUDIO_IGNORE;       /* the new monitor state */
    paud_change.volume = AUDIO_IGNORE;        /* volume level [0-0x7fffffff] */
    paud_change.volume_delay = AUDIO_IGNORE;  /* the new volume delay */
    paud_change.balance = balance;            /* the new balance */
    paud_change.balance_delay = AUDIO_IGNORE; /* the new balance delay */
    paud_change.treble = AUDIO_IGNORE;        /* the new treble state */
    paud_change.bass = AUDIO_IGNORE;          /* the new bass state */
    paud_change.pitch = AUDIO_IGNORE;         /* the new pitch state */

    paud_control.ioctl_request = AUDIO_CHANGE;
    paud_control.request_info = (char*)&paud_change;

    if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 )
    {
	perror("Change audio volume failed");
    }
}

static void debugUpdate( unsigned long& flags, long& bsize )
{
    const char* g;

    g = getenv("AUDIO_BIG_ENDIAN");
    if ( g )
    {
	int i = atoi(g);
	if ( i == 1 )
	{
	    flags |= AUDIO_BIG_ENDIAN;
	}
	else if ( i == 0 )
	{
	    flags &= ~AUDIO_BIG_ENDIAN;
	}
	else
	{
	    printf("CG: bad AUDIO_BIG_ENDIAN env variable %s\n", g);
	}
    }

    g = getenv("SIGNED");
    if ( g )
    {
	int i = atoi(g);
	if ( i == 1 )
	{
	    flags |= SIGNED;
	}
	else if ( i == 0 )
	{
	    flags &= ~SIGNED;
	}
	else
	{
	    printf("CG: bad SIGNED env variable %s\n", g);
	}
    }

    g = getenv("TWOS_COMPLEMENT");
    if ( g )
    {
	int i = atoi(g);
	if ( i == 1 )
	{
	    flags |= TWOS_COMPLEMENT;
	}
	else if ( i == 0 )
	{
	    flags &= ~TWOS_COMPLEMENT;
	}
	else
	{
	    printf("CG: bad TWOS_COMPLEMENT env variable %s\n", g);
	}
    }

    g = getenv("FIXED");
    if ( g )
    {
	int i = atoi(g);
	if ( i == 1 )
	{
	    flags |= FIXED;
	}
	else if ( i == 0 )
	{
	    flags &= ~FIXED;
	}
	else
	{
	    printf("CG: bad FIXED env variable %s\n", g);
	}
    }

    g = getenv("LEFT_ALIGNED");
    if ( g )
    {
	int i = atoi(g);
	if ( i == 1 )
	{
	    flags |= LEFT_ALIGNED;
	}
	else if ( i == 0 )
	{
	    flags &= ~LEFT_ALIGNED;
	}
	else
	{
	    printf("CG: bad LEFT_ALIGNED env variable %s\n", g);
	}
    }

    g = getenv("RIGHT_ALIGNED");
    if ( g )
    {
	int i = atoi(g);
	if ( i == 1 )
	{
	    flags |= RIGHT_ALIGNED;
	}
	else if ( i == 0 )
	{
	    flags &= ~RIGHT_ALIGNED;
	}
	else
	{
	    printf("CG: bad RIGHT_ALIGNED env variable %s\n", g);
	}
    }

    g = getenv("BSIZE");
    if ( g )
    {
	bsize = atoi(g);
    }
}