Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / AHI / Examples / Device / PlayTest / PlayTest.c
blob161113b0558ab706242b59b4c0f4e477a47927cf
1 /*
2 ** This program uses the device interface to play a sampled sound.
3 ** The input is read from THE DEFAULT INPUT, make sure you
4 ** start it with "PlayTest pri < mysample.raw" !
5 ** Where pri is a number from -128 to +127 (may be omitted)
6 ** The sample should be 8 bit signed, mono (see TYPE).
7 **
8 ** PLEASE NOTE that earlier versions of this example contained a bug
9 ** that sometimes DeleteIORequest'ed a pointer that was AllocMem'ed!
12 #include <devices/ahi.h>
13 #include <dos/dosasl.h>
14 #include <exec/memory.h>
15 #include <proto/exec.h>
16 #include <proto/dos.h>
17 #include <proto/ahi.h>
18 #include <stdlib.h>
20 #define FREQUENCY 8000
21 #define TYPE AHIST_M8S
22 #define BUFFERSIZE 20000
24 struct MsgPort *AHImp = NULL;
25 struct AHIRequest *AHIios[2] = {NULL,NULL};
26 struct AHIRequest *AHIio = NULL;
27 APTR AHIiocopy = NULL;
28 BYTE AHIDevice = -1;
30 BYTE buffer1[BUFFERSIZE];
31 BYTE buffer2[BUFFERSIZE];
33 void cleanup(LONG rc)
35 if(!AHIDevice)
36 CloseDevice((struct IORequest *)AHIio);
37 DeleteIORequest((struct IORequest *)AHIio);
38 FreeMem(AHIiocopy,sizeof(struct AHIRequest));
39 DeleteMsgPort(AHImp);
40 exit(rc);
43 int main(int argc, char *argv[])
45 BYTE *p1=buffer1,*p2=buffer2;
46 void *tmp;
47 ULONG signals,length;
48 struct AHIRequest *link = NULL;
49 LONG priority = 0;
50 BYTE pri;
52 if(argc == 2)
54 StrToLong(argv[1], &priority);
56 pri = priority;
57 Printf("Sound priority: %ld\n", pri);
59 if((AHImp=CreateMsgPort()) != NULL) {
60 if((AHIio=(struct AHIRequest *)CreateIORequest(AHImp,sizeof(struct AHIRequest))) != NULL) {
61 AHIio->ahir_Version = 4;
62 AHIDevice=OpenDevice(AHINAME,0,(struct IORequest *)AHIio,0);
66 if(AHIDevice) {
67 Printf("Unable to open %s/0 version 4\n",AHINAME);
68 cleanup(RETURN_FAIL);
71 // Make a copy of the request (for double buffering)
72 AHIiocopy = AllocMem(sizeof(struct AHIRequest), MEMF_ANY);
73 if(! AHIiocopy) {
74 cleanup(RETURN_FAIL);
76 CopyMem(AHIio, AHIiocopy, sizeof(struct AHIRequest));
77 AHIios[0]=AHIio;
78 AHIios[1]=AHIiocopy;
80 SetIoErr(0);
82 for(;;) {
84 // Fill buffer
85 length = Read(Input(),p1,BUFFERSIZE);
87 // Play buffer
88 AHIios[0]->ahir_Std.io_Message.mn_Node.ln_Pri = pri;
89 AHIios[0]->ahir_Std.io_Command = CMD_WRITE;
90 AHIios[0]->ahir_Std.io_Data = p1;
91 AHIios[0]->ahir_Std.io_Length = length;
92 AHIios[0]->ahir_Std.io_Offset = 0;
93 AHIios[0]->ahir_Frequency = FREQUENCY;
94 AHIios[0]->ahir_Type = TYPE;
95 AHIios[0]->ahir_Volume = 0x10000; // Full volume
96 AHIios[0]->ahir_Position = 0x8000; // Centered
97 AHIios[0]->ahir_Link = link;
98 SendIO((struct IORequest *) AHIios[0]);
100 if(link) {
102 // Wait until the last buffer is finished (== the new buffer is started)
103 signals=Wait(SIGBREAKF_CTRL_C | (1L << AHImp->mp_SigBit));
105 // Check for Ctrl-C and abort if pressed
106 if(signals & SIGBREAKF_CTRL_C) {
107 SetIoErr(ERROR_BREAK);
108 break;
111 // Remove the reply and abort on error
112 if(WaitIO((struct IORequest *) link)) {
113 SetIoErr(ERROR_WRITE_PROTECTED);
114 break;
118 // Check for end-of-sound, and wait until it is finished before aborting
119 if(length != BUFFERSIZE) {
120 WaitIO((struct IORequest *) AHIios[0]);
121 break;
124 link = AHIios[0];
126 // Swap buffer and request pointers, and restart
127 tmp = p1;
128 p1 = p2;
129 p2 = tmp;
131 tmp = AHIios[0];
132 AHIios[0] = AHIios[1];
133 AHIios[1] = tmp;
137 // Abort any pending iorequests
138 AbortIO((struct IORequest *) AHIios[0]);
139 WaitIO((struct IORequest *) AHIios[0]);
141 if(link) { // Only if the second request was started
142 AbortIO((struct IORequest *) AHIios[1]);
143 WaitIO((struct IORequest *) AHIios[1]);
146 if(IoErr()) {
147 PrintFault(IoErr(), argv[0] );
148 cleanup(RETURN_ERROR);
151 cleanup(RETURN_OK);
152 return RETURN_OK; // Make compiler happy