4 #include <devices/ahi.h>
5 #include <exec/memory.h>
7 #include <utility/hooks.h>
9 #include <clib/alib_protos.h>
10 #include <proto/exec.h>
11 #include <proto/ahi.h>
19 static const char version
[] = "$VER: PlaySineEverywhere " VERS
"\n\r";
21 long __oslibversion
= 37;
23 struct Library
* AHIBase
= NULL
;
26 PlaySineEverywhere( void );
29 SoundFunc( struct Hook
* hook
,
30 struct AHIAudioCtrl
* actrl
,
31 struct AHISoundMessage
* sm
);
35 main( int argc
, char* argv
[] ) {
39 fprintf( stderr
, "Usage: %s\n", argv
[ 0 ] );
43 struct MsgPort
* mp
= CreateMsgPort();
46 struct AHIRequest
* io
= (struct AHIRequest
*)
47 CreateIORequest( mp
, sizeof( struct AHIRequest
) );
50 // We use 32 bit samples, so we need version 6.
53 if( OpenDevice( AHINAME
, AHI_NO_UNIT
, (struct IORequest
*) io
, 0 )
55 AHIBase
= (struct Library
*) io
->ahir_Std
.io_Device
;
57 rc
= PlaySineEverywhere();
59 CloseDevice( (struct IORequest
*) io
);
62 fprintf( stderr
, "Unable to open '" AHINAME
"' version 6.\n" );
66 DeleteIORequest( (struct IORequest
*) io
);
69 fprintf( stderr
, "Unable to create IO request.\n" );
76 fprintf( stderr
, "Unable to create message port.\n" );
85 PlaySineEverywhere( void ) {
87 int sine_length
= 44100 / 100;
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
&&
107 sine_s32s
!= NULL
) {
108 ULONG mode
= AHI_INVALID_ID
;
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
;
130 struct Hook sound_hook
= {
133 (HOOKFUNC
) SoundFunc
,
137 AHI_GetAudioAttrs( mode
, NULL
,
138 AHIDB_Name
, (ULONG
) &name
,
142 printf( "Mode 0x%08lx: %s\n", mode
, name
);
144 actrl
= AHI_AllocAudio( AHIA_AudioID
, mode
,
148 AHIA_SoundFunc
, (ULONG
) &sound_hook
,
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
) {
168 AHIP_BeginChannel
, 0,
176 // Now, when everything is "armed", lets start processing.
178 SetSignal( 0, SIGF_SINGLE
);
180 if( AHI_ControlAudio( actrl
,
182 TAG_DONE
) == AHIE_OK
) {
185 AHI_ControlAudio( actrl
,
190 fprintf( stderr
, "Unable start playback.\n" );
194 // AHI_FreeAudio() will unload the sounds
197 fprintf( stderr
, "Unable load sound.\n" );
201 AHI_FreeAudio( actrl
);
204 fprintf( stderr
, "Unable to allocate audio.\n" );
210 fprintf( stderr
, "Unable to allocate memory for sine\n" );
215 FreeVec( sine_s16s
);
216 FreeVec( sine_m16s
);
217 FreeVec( sine_s32s
);
218 FreeVec( sine_m32s
);
224 SoundFunc( struct Hook
* hook
,
225 struct AHIAudioCtrl
* actrl
,
226 struct AHISoundMessage
* sm
) {
227 struct Task
* task
= hook
->h_Data
;
230 ++( (ULONG
) actrl
->ahiac_UserData
);
231 cnt
= (ULONG
) actrl
->ahiac_UserData
;
234 AHI_SetSound( 0, AHI_NOSOUND
, 0, 0, actrl
, AHISF_NONE
);
236 else if( cnt
== 101 ) {
237 Signal( task
, SIGF_SINGLE
);
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
);