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).
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>
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
;
30 BYTE buffer1
[BUFFERSIZE
];
31 BYTE buffer2
[BUFFERSIZE
];
36 CloseDevice((struct IORequest
*)AHIio
);
37 DeleteIORequest((struct IORequest
*)AHIio
);
38 FreeMem(AHIiocopy
,sizeof(struct AHIRequest
));
43 int main(int argc
, char *argv
[])
45 BYTE
*p1
=buffer1
,*p2
=buffer2
;
48 struct AHIRequest
*link
= NULL
;
54 StrToLong(argv
[1], &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);
67 Printf("Unable to open %s/0 version 4\n",AHINAME
);
71 // Make a copy of the request (for double buffering)
72 AHIiocopy
= AllocMem(sizeof(struct AHIRequest
), MEMF_ANY
);
76 CopyMem(AHIio
, AHIiocopy
, sizeof(struct AHIRequest
));
85 length
= Read(Input(),p1
,BUFFERSIZE
);
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]);
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
);
111 // Remove the reply and abort on error
112 if(WaitIO((struct IORequest
*) link
)) {
113 SetIoErr(ERROR_WRITE_PROTECTED
);
118 // Check for end-of-sound, and wait until it is finished before aborting
119 if(length
!= BUFFERSIZE
) {
120 WaitIO((struct IORequest
*) AHIios
[0]);
126 // Swap buffer and request pointers, and restart
132 AHIios
[0] = AHIios
[1];
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]);
147 PrintFault(IoErr(), argv
[0] );
148 cleanup(RETURN_ERROR
);
152 return RETURN_OK
; // Make compiler happy