File: loopBucket.c

package info (click to toggle)
nas 1.9.4-9.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,644 kB
  • sloc: ansic: 52,144; makefile: 31,377; sh: 8,077; perl: 1,104; yacc: 252; cpp: 216; lex: 68
file content (112 lines) | stat: -rw-r--r-- 2,934 bytes parent folder | download | duplicates (9)
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
/**
 * loopBucket - loads a sound file into a bucket loops it continuously
 *
 * usage: loopBucket file
 *
 * Demonstrates the usage of some low-level audio library functions
 * and actions.
 *
 * $NCDId: @(#)loopBucket.c,v 1.2 1994/05/09 18:42:38 greg Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <audio/audiolib.h>
#include <audio/soundlib.h>

static void
fatalError(const char *message, const char *arg)
{
    fprintf(stderr, message, arg);
    fprintf(stderr, "\n");
    exit(1);
}

static AuBool
EventHandler(AuServer *aud, AuEvent *ev, AuEventHandlerRec *handler)
{
    AuBool         *done = (AuBool *) handler->data;
    AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;

    if (ev->type == AuEventTypeElementNotify &&
	event->kind == AuElementNotifyKindState &&
	event->cur_state == AuStateStop)
	*done = AuTrue;

    return AuTrue;
}

int
main(int argc, char **argv)
{
    AuServer       *aud;
    AuBucketID      bucket;
    AuBucketAttributes *ba;
    AuDeviceID      device = AuNone;
    AuFlowID        flow;
    AuElement       elements[2];
    AuEventHandlerRec *handler;
    AuElementAction actions[2];
    AuBool          done = AuFalse;
    char           *file = argv[1];
    int             i;

    if (argc < 2)
	fatalError("usage: loopBucket filename", NULL);

    if (!(aud = AuOpenServer(NULL, 0, NULL, 0, NULL, NULL)))
	exit(1);

    if (!(bucket = AuSoundCreateBucketFromFile(aud, file, AuAccessAllMasks, &ba,
					       NULL)))
	exit(1);

    /* look for an output device */
    for (i = 0; i < AuServerNumDevices(aud); i++)
	if ((AuDeviceKind(AuServerDevice(aud, i)) ==
	     AuComponentKindPhysicalOutput) &&
	 AuDeviceNumTracks(AuServerDevice(aud, i)) == AuBucketNumTracks(ba))
	{
	    device = AuDeviceIdentifier(AuServerDevice(aud, i));
	    break;
	}

    if (device == AuNone)
	fatalError("Couldn't find an output device", NULL);

    if (!(flow = AuCreateFlow(aud, NULL)))
	fatalError("Couldn't create flow", NULL);

    AuMakeChangeStateAction(&actions[0], AuStateStop, AuStateStart,
			    AuReasonEOF, flow, AuElementAll, AuStateStart);

    AuMakeElementImportBucket(&elements[0], AuBucketSampleRate(ba), bucket,
			      AuUnlimitedSamples, 0, 1, actions);
    AuMakeElementExportDevice(&elements[1], 0, device, AuBucketSampleRate(ba),
			      AuUnlimitedSamples, 0, NULL);

    AuSetElements(aud, flow, AuTrue, 2, elements, NULL);

    AuFreeBucketAttributes(aud, 1, ba);

    if (!(handler = AuRegisterEventHandler(aud, AuEventHandlerIDMask,
					   0, flow, EventHandler,
					   (AuPointer) &done)))
	fatalError("Couldn't register event handler", NULL);

    AuStartFlow(aud, flow, NULL);

    while (!done)
    {
	AuEvent         ev;

	AuNextEvent(aud, AuTrue, &ev);
	AuDispatchEvent(aud, &ev);
    }

    AuUnregisterEventHandler(aud, handler);
    AuDestroyFlow(aud, flow, NULL);
    AuDestroyBucket(aud, bucket, NULL);
    AuCloseServer(aud);
    return 0;
}