Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / devs / AHI / Examples / Low-level / DoubleBuffer / DoubleBuffer.c
blobb52a569bb5a67f3720e7e4e271f9d5d1f45755f7
1 /* Simple sample player for AHI using the low-level API
2 with double buffered AHIST_DYNAMICSAMPLE sounds.
4 Usage: DoubleBuffer < [raw sample file]
6 the file must be in mono 16 bit signed big endian format sampled
7 in 17640 Hz.
9 This software is Public Domain. */
11 #include <devices/ahi.h>
12 #include <exec/exec.h>
13 #include <proto/ahi.h>
14 #include <proto/dos.h>
15 #include <proto/exec.h>
17 #define EQ ==
18 #define MINBUFFLEN 10000
20 struct Library *AHIBase;
21 struct MsgPort *AHImp=NULL;
22 struct AHIRequest *AHIio=NULL;
23 BYTE AHIDevice=-1;
25 BYTE signal=-1;
27 struct AHIAudioModeRequester *req=NULL;
28 struct AHIAudioCtrl *actrl=NULL;
30 BOOL DBflag=FALSE; // double buffer flag
31 ULONG BufferLen=NULL;
33 struct AHISampleInfo Sample0 =
35 AHIST_M16S,
36 NULL,
37 NULL,
40 struct AHISampleInfo Sample1 =
42 AHIST_M16S,
43 NULL,
44 NULL,
47 __asm __saveds ULONG SoundFunc(register __a0 struct Hook *hook,
48 register __a2 struct AHIAudioCtrl *actrl,
49 register __a1 struct AHISoundMessage *smsg)
51 if(DBflag = !DBflag) // Flip and test
52 AHI_SetSound(0,1,0,0,actrl,NULL);
53 else
54 AHI_SetSound(0,0,0,0,actrl,NULL);
55 Signal(actrl->ahiac_UserData,(1L<<signal));
56 return NULL;
59 struct Hook SoundHook =
61 0,0,
62 SoundFunc,
63 NULL,
64 NULL,
68 int main()
70 ULONG mixfreq,playsamples,readsamples,signals,i;
72 if(AHImp=CreateMsgPort())
74 if(AHIio=(struct AHIRequest *)CreateIORequest(AHImp,sizeof(struct AHIRequest)))
76 AHIio->ahir_Version = 4; // Open at least version 4 of 'ahi.device'.
77 if(!(AHIDevice=OpenDevice(AHINAME,AHI_NO_UNIT,(struct IORequest *)AHIio,NULL)))
79 AHIBase=(struct Library *)AHIio->ahir_Std.io_Device;
81 if(req=AHI_AllocAudioRequest(
82 AHIR_PubScreenName,"",
83 AHIR_TitleText,"Select a mode and rate",
84 AHIR_InitialMixFreq,17640,
85 AHIR_DoMixFreq,TRUE,
86 TAG_DONE))
88 if(AHI_AudioRequest(req,TAG_DONE))
90 if(actrl=AHI_AllocAudio(
91 AHIA_AudioID,req->ahiam_AudioID,
92 AHIA_MixFreq,req->ahiam_MixFreq,
93 AHIA_Channels,1,
94 AHIA_Sounds,2,
95 AHIA_SoundFunc,&SoundHook,
96 AHIA_UserData,FindTask(NULL),
97 TAG_DONE))
99 AHI_GetAudioAttrs(AHI_INVALID_ID,actrl,
100 AHIDB_MaxPlaySamples,&playsamples,
101 TAG_DONE);
102 AHI_ControlAudio(actrl,
103 AHIC_MixFreq_Query,&mixfreq,
104 TAG_DONE);
105 BufferLen=playsamples*17640/mixfreq;
106 if (BufferLen<MINBUFFLEN)
107 BufferLen=MINBUFFLEN;
109 Sample0.ahisi_Length=BufferLen;
110 Sample1.ahisi_Length=BufferLen;
111 Sample0.ahisi_Address=AllocVec(BufferLen*2,MEMF_PUBLIC|MEMF_CLEAR);
112 Sample1.ahisi_Address=AllocVec(BufferLen*2,MEMF_PUBLIC|MEMF_CLEAR);
114 if(Sample0.ahisi_Address && Sample1.ahisi_Address)
116 if((!(AHI_LoadSound(0,AHIST_DYNAMICSAMPLE,&Sample0,actrl))) &&
117 (!(AHI_LoadSound(1,AHIST_DYNAMICSAMPLE,&Sample1,actrl))))
119 if((signal=AllocSignal(-1)) != -1)
121 if(!(AHI_ControlAudio(actrl,
122 AHIC_Play,TRUE,
123 TAG_DONE)))
125 // Everything is set up now. Let's load the buffers and start rockin'...
126 Read(Input(),Sample0.ahisi_Address,BufferLen*2);
128 // The new AHI_PlayA() function is demonstrated...
129 AHI_Play(actrl,
130 AHIP_BeginChannel,0,
131 AHIP_Freq,17640,
132 AHIP_Vol,0x10000L,
133 AHIP_Pan,0x8000L,
134 AHIP_Sound,0,
135 AHIP_Offset,0,
136 AHIP_Length,0,
137 AHIP_EndChannel,NULL,
138 TAG_DONE);
139 /* These functions were available in V2, too.
140 AHI_SetFreq(0,17640,actrl,AHISF_IMM);
141 AHI_SetVol(0,0x10000L,0x8000L,actrl,AHISF_IMM);
142 AHI_SetSound(0,0,0,0,actrl,AHISF_IMM);
144 for(;;)
146 signals=Wait((1L<<signal) | SIGBREAKF_CTRL_C);
147 if(signals & SIGBREAKF_CTRL_C)
148 break;
150 if(DBflag)
152 readsamples=Read(Input(),Sample1.ahisi_Address,BufferLen*2)/2;
153 if(readsamples<BufferLen)
155 // Clear rest of buffer
156 for(i=readsamples;i<BufferLen;i++)
157 ((WORD *)Sample1.ahisi_Address)[i]=0;
158 Wait(1L<<signal);
159 // Clear other buffer
160 for(i=0;i<BufferLen;i++)
161 ((WORD *)Sample0.ahisi_Address)[i]=0;
162 break;
165 else
167 readsamples=Read(Input(),Sample0.ahisi_Address,BufferLen*2)/2;
168 if(readsamples<BufferLen)
170 // Clear rest of buffer
171 for(i=readsamples;i<BufferLen;i++)
172 ((WORD *)Sample0.ahisi_Address)[i]=0;
173 Wait(1L<<signal);
174 // Clear other buffer
175 for(i=0;i<BufferLen;i++)
176 ((WORD *)Sample1.ahisi_Address)[i]=0;
177 break;
183 if(signals & SIGBREAKF_CTRL_C)
184 Printf("***Break\n");
185 else
186 Wait(1L<<signal); // Wait for half-loaded buffer to finish.
188 AHI_ControlAudio(actrl,
189 AHIC_Play,FALSE,
190 TAG_DONE);
192 FreeSignal(signal);
193 signal=-1;
197 else
198 Printf("Cannot intialize sample buffers\n");
200 else
201 Printf("Out of memory.\n");
203 FreeVec(Sample0.ahisi_Address);
204 FreeVec(Sample1.ahisi_Address);
205 Sample0.ahisi_Address=NULL;
206 Sample1.ahisi_Address=NULL;
207 AHI_FreeAudio(actrl);
208 actrl=NULL;
210 else
211 Printf("Unable to allocate sound hardware\n");
213 else
215 if(IoErr() EQ NULL)
216 Printf("Aborted.\n");
217 else if(IoErr() EQ ERROR_NO_MORE_ENTRIES)
218 Printf("No available audio modes.\n");
219 else if(IoErr() EQ ERROR_NO_FREE_STORE)
220 Printf("Out of memory.\n");
223 AHI_FreeAudioRequest(req);
224 req=NULL;
226 else
227 Printf("Out of memory.\n");
229 CloseDevice((struct IORequest *)AHIio);
230 AHIDevice=-1; // Good habit, IMHO.
232 DeleteIORequest((struct IORequest *)AHIio);
233 AHIio=NULL;
235 DeleteMsgPort(AHImp);
236 AHImp=NULL;