Added a test for MUIA_Listview_SelectChange.
[AROS.git] / workbench / devs / AHI / Examples / Low-level / PlaySineEverywhere / PlaySineEverywhere.c
blobdc6d13a592d38001d2432e7fcab5ca871e000783
2 #include <config.h>
4 #include <devices/ahi.h>
5 #include <exec/memory.h>
6 #include <dos/dos.h>
7 #include <utility/hooks.h>
9 #include <clib/alib_protos.h>
10 #include <proto/exec.h>
11 #include <proto/ahi.h>
13 #include <stdio.h>
14 #include <math.h>
15 #include <limits.h>
17 #include "version.h"
19 static const char version[] = "$VER: PlaySineEverywhere " VERS "\n\r";
21 long __oslibversion = 37;
23 struct Library* AHIBase = NULL;
25 int
26 PlaySineEverywhere( void );
28 void
29 SoundFunc( struct Hook* hook,
30 struct AHIAudioCtrl* actrl,
31 struct AHISoundMessage* sm );
34 int
35 main( int argc, char* argv[] ) {
36 int rc = RETURN_OK;
38 if( argc != 1 ) {
39 fprintf( stderr, "Usage: %s\n", argv[ 0 ] );
40 rc = RETURN_ERROR;
42 else {
43 struct MsgPort* mp = CreateMsgPort();
45 if( mp != NULL ) {
46 struct AHIRequest* io = (struct AHIRequest *)
47 CreateIORequest( mp, sizeof( struct AHIRequest ) );
49 if( io != NULL ) {
50 // We use 32 bit samples, so we need version 6.
51 io->ahir_Version = 6;
53 if( OpenDevice( AHINAME, AHI_NO_UNIT, (struct IORequest *) io, 0 )
54 == 0 ) {
55 AHIBase = (struct Library *) io->ahir_Std.io_Device;
57 rc = PlaySineEverywhere();
59 CloseDevice( (struct IORequest *) io );
61 else {
62 fprintf( stderr, "Unable to open '" AHINAME "' version 6.\n" );
63 rc = RETURN_FAIL;
66 DeleteIORequest( (struct IORequest *) io );
68 else {
69 fprintf( stderr, "Unable to create IO request.\n" );
70 rc = RETURN_FAIL;
73 DeleteMsgPort( mp );
75 else {
76 fprintf( stderr, "Unable to create message port.\n" );
77 rc = RETURN_FAIL;
81 return rc;
84 int
85 PlaySineEverywhere( void ) {
86 int rc = RETURN_OK;
87 int sine_length = 44100 / 100;
88 BYTE* sine_m8s;
89 BYTE* sine_s8s;
90 WORD* sine_s16s;
91 WORD* sine_m16s;
92 LONG* sine_s32s;
93 LONG* sine_m32s;
95 sine_m8s = AllocVec( 1 * sizeof( BYTE ) * sine_length, MEMF_ANY | MEMF_PUBLIC );
96 sine_s8s = AllocVec( 2 * sizeof( BYTE ) * sine_length, MEMF_ANY | MEMF_PUBLIC );
97 sine_m16s = AllocVec( 1 * sizeof( WORD ) * sine_length, MEMF_ANY | MEMF_PUBLIC );
98 sine_s16s = AllocVec( 2 * sizeof( WORD ) * sine_length, MEMF_ANY | MEMF_PUBLIC );
99 sine_m32s = AllocVec( 1 * sizeof( LONG ) * sine_length, MEMF_ANY | MEMF_PUBLIC );
100 sine_s32s = AllocVec( 2 * sizeof( LONG ) * sine_length, MEMF_ANY | MEMF_PUBLIC );
102 if( sine_m8s != NULL &&
103 sine_s8s != NULL &&
104 sine_m16s != NULL &&
105 sine_s16s != NULL &&
106 sine_m32s != NULL &&
107 sine_s32s != NULL ) {
108 ULONG mode = AHI_INVALID_ID;
109 int i;
111 for( i = 0; i < sine_length; ++i ) {
112 double value = sin( i * 2 * M_PI / sine_length );
114 sine_m8s[ i ] = (BYTE) ( SCHAR_MAX * value );
115 sine_m16s[ i ] = (WORD) ( SHRT_MAX * value );
116 sine_m32s[ i ] = (LONG) ( LONG_MAX * value );
118 sine_s8s[ i * 2 + 0 ] = (BYTE) ( SCHAR_MAX * value );
119 sine_s8s[ i * 2 + 1 ] = (BYTE) ( SCHAR_MAX * value );
120 sine_s16s[ i * 2 + 0 ] = (WORD) ( SHRT_MAX * value );
121 sine_s16s[ i * 2 + 1 ] = (WORD) ( SHRT_MAX * value );
122 sine_s32s[ i * 2 + 0 ] = (LONG) ( LONG_MAX * value );
123 sine_s32s[ i * 2 + 1 ] = (LONG) ( LONG_MAX * value );
126 while( rc == RETURN_OK &&
127 ( mode = AHI_NextAudioID( mode ) ) != AHI_INVALID_ID ) {
128 struct AHIAudioCtrl* actrl;
129 char name[ 64 ];
130 struct Hook sound_hook = {
131 { NULL, NULL },
132 HookEntry,
133 (HOOKFUNC) SoundFunc,
134 FindTask( NULL )
137 AHI_GetAudioAttrs( mode, NULL,
138 AHIDB_Name, (ULONG) &name,
139 AHIDB_BufferLen, 64,
140 TAG_DONE );
142 printf( "Mode 0x%08lx: %s\n", mode, name );
144 actrl = AHI_AllocAudio( AHIA_AudioID, mode,
145 AHIA_MixFreq, 44100,
146 AHIA_Channels, 1,
147 AHIA_Sounds, 6,
148 AHIA_SoundFunc, (ULONG) &sound_hook,
149 AHIA_UserData, 0,
150 TAG_DONE );
152 if( actrl != NULL ) {
153 struct AHISampleInfo sample_m8s = { AHIST_M8S, sine_m8s, sine_length };
154 struct AHISampleInfo sample_s8s = { AHIST_S8S, sine_s8s, sine_length };
155 struct AHISampleInfo sample_m16s = { AHIST_M16S, sine_m16s, sine_length };
156 struct AHISampleInfo sample_s16s = { AHIST_S16S, sine_s16s, sine_length };
157 struct AHISampleInfo sample_m32s = { AHIST_M32S, sine_m32s, sine_length };
158 struct AHISampleInfo sample_s32s = { AHIST_S32S, sine_s32s, sine_length };
160 if( AHI_LoadSound( 0, AHIST_SAMPLE, &sample_m8s, actrl) == AHIE_OK &&
161 AHI_LoadSound( 1, AHIST_SAMPLE, &sample_s8s, actrl) == AHIE_OK &&
162 AHI_LoadSound( 2, AHIST_SAMPLE, &sample_m16s, actrl) == AHIE_OK &&
163 AHI_LoadSound( 3, AHIST_SAMPLE, &sample_s16s, actrl) == AHIE_OK &&
164 AHI_LoadSound( 4, AHIST_SAMPLE, &sample_m32s, actrl) == AHIE_OK &&
165 AHI_LoadSound( 5, AHIST_SAMPLE, &sample_s32s, actrl) == AHIE_OK ) {
167 AHI_Play( actrl,
168 AHIP_BeginChannel, 0,
169 AHIP_Sound, 0,
170 AHIP_Freq, 44100,
171 AHIP_Vol, 0x10000,
172 AHIP_Pan, 0x00000,
173 AHIP_EndChannel, 0,
174 TAG_DONE );
176 // Now, when everything is "armed", lets start processing.
178 SetSignal( 0, SIGF_SINGLE );
180 if( AHI_ControlAudio( actrl,
181 AHIC_Play, TRUE,
182 TAG_DONE ) == AHIE_OK ) {
183 Wait( SIGF_SINGLE );
185 AHI_ControlAudio( actrl,
186 AHIC_Play, FALSE,
187 TAG_DONE );
189 else {
190 fprintf( stderr, "Unable start playback.\n" );
191 rc = RETURN_ERROR;
194 // AHI_FreeAudio() will unload the sounds
196 else {
197 fprintf( stderr, "Unable load sound.\n" );
198 rc = RETURN_ERROR;
201 AHI_FreeAudio( actrl );
203 else {
204 fprintf( stderr, "Unable to allocate audio.\n" );
205 rc = RETURN_ERROR;
209 else {
210 fprintf( stderr, "Unable to allocate memory for sine\n" );
213 FreeVec( sine_m8s );
214 FreeVec( sine_s8s );
215 FreeVec( sine_s16s );
216 FreeVec( sine_m16s );
217 FreeVec( sine_s32s );
218 FreeVec( sine_m32s );
220 return rc;
223 void
224 SoundFunc( struct Hook* hook,
225 struct AHIAudioCtrl* actrl,
226 struct AHISoundMessage* sm ) {
227 struct Task* task = hook->h_Data;
228 ULONG cnt;
230 ++( (ULONG) actrl->ahiac_UserData );
231 cnt = (ULONG) actrl->ahiac_UserData;
233 if( cnt == 100 ) {
234 AHI_SetSound( 0, AHI_NOSOUND, 0, 0, actrl, AHISF_NONE );
236 else if( cnt == 101 ) {
237 Signal( task, SIGF_SINGLE );
239 else {
240 UWORD sound = cnt % 6;
242 AHI_SetSound( 0, sound, 0, 0, actrl, AHISF_NONE );
244 if( ( sound == 0 ) ) {
245 AHI_SetVol( 0, 0x10000, 0x10000 * cnt / 6 / ( 100 / 6 ), actrl, AHISF_NONE );