Audio noise player example
Small demonstration how to create audible noise and play that along with some audio callback functions.
Compilation
Easiest way to get this example running is to go to examples directory in package root, and run:
$ ./build.sh audio_play
build.sh script compiles Ano script to C, copies source files in place and pops up instructions what to do next. Follow them. Check also examples/README for more info.
Preview
; ------------------------------------------------------------------------------
; audio_play.ano
;
; To compile:
;
; $ ./build/ano ./examples/audio_play/audio_play.ano > \
; engine/dsl_ano.h
; ------------------------------------------------------------------------------
;
; @ANO_SCRIPT_NAME audio_play
; @ANO_SCRIPT_VERSION 0.0.1
; @ANO_SCRIPT_DESCRIPTION Simple audio noise player example
;
; @ANO_FLAGS_USE_PROTOS [ ]
; @ANO_FLAGS_VAR_NAME_SUBS [ ]
; @ANO_FLAGS_VAR_WARN_UNUSED [ ]
;
; Copyright © 2016-2026, Jani Salonen <salojan@goto10.co>
; All rights reserved.
;
; Small demonstration how to create audible noise and play that along with
; some audio callback functions.
;
main [exit: 0] {
; Initialize audio system, this example does nothing if
; --disable-audio was passed to configure script.
;
audio_enabled {
mov buf_printed (0)
mov buf_played (0)
audio_init (\
cb_track_play: "cb_play", \
cb_track_mode: NULL, \
cb_track_finish: "cb_finish", \
cb_track_restart: NULL, \
cb_track_cancel: NULL, \
cb_track_pan: NULL, \
cb_track_vol: NULL, \
cb_master_vol: NULL, \
cb_buffer_start: "cb_buf_start", \
cb_buffer_done: "cb_buf_done")
; Create one second white noise…
;
_ahnd = audio_create_noise (\
seconds: 1.0, pink: 0.0, brown: 0.0)
; …start playing it…
;
_ahnd.play(volume: 0.1, pan: 0.0)
; …wait until it finishes…
;
_ahnd.wait()
; …and close audio track.
;
;_ahnd.eject()
}
}
_AUDIOCB_PLAY_ callback cb_play (_hnd, _vol, _pan, _data_size, \
_ch1_data, _ch1_vol, _ch2_data, _ch2_vol) {
; This callback is called when track starts playing.
;
print "Track " . _hnd . " playing " . _data_size . " samples using" \
" volume of " . _vol . " and panning of " . _pan . ".\n\n"
}
_AUDIOCB_FINISH_ callback cb_finish (_hnd, _data_size, \
_ch1_data, _ch2_data) {
; This callback is called when track finishes playing.
;
print "Track " . _hnd . " finished playing " . _data_size . \
" samples, " . buf_played . " buffers in total.\n\n"
}
_AUDIOCB_START_ callback cb_buf_start (_buf_data, _buf_channels, _buf_speed, \
_buf_size, _buf_samplesize, _buf_be, _buf_signed) {
; This callback is called when internal data buffer is about to be
; processed.
;
; Print buffer info only once.
;
mov rc (buf_printed)
end_unless_zero
print "Buffer start, data address: " . _buf_data . "\n"
print "Buffer size: " . _buf_size . " bytes\n"
print "Channels: " . _buf_channels . "\n"
print "Sample rate: " . _buf_speed . " Hz\n"
print "Sample size: " . _buf_samplesize . " bytes\n"
print "Big endian: " . _buf_be . "\n"
print "Signed: " . _buf_signed . "\n\n"
}
_AUDIOCB_DONE_ callback cb_buf_done (_buf_data, _buf_channels, _buf_speed, \
_buf_size, _buf_samplesize, _buf_be, _buf_signed, _usecs) {
; This callback is called when internal data buffer was processed.
;
inc buf_played
; Print buffer info only once.
;
mov rc (buf_printed)
end_unless_zero
mov buf_printed (1)
print "One buffer done, processing time in μsecs: " . _usecs . "\n\n"
}
; ------------------------------------------------------------------------------ ; audio_play.w2c ; ; To compile: ; ; $ ./build/widget ./examples/audio_play/audio_play.w2c > \ ; engine/widget_defs.h ; ------------------------------------------------------------------------------
; ------------------------------------------------------------------------------ ; audio_play.m2c ; ; To compile: ; ; $ ./build/menu ./examples/audio_play/audio_play.m2c > \ ; engine/menu_defs.h ; ------------------------------------------------------------------------------
Copyright © 2026, Jani Salonen <salojan at goto10 piste co>. Piste is finnish word and means dot. All rights reserved.