Where does audio_callback get its information from?

Pixilang programming language
Post Reply
philipbergwerf
Posts: 174
Joined: Sat Mar 17, 2018 4:23 pm

Where does audio_callback get its information from?

Post by philipbergwerf »

Question about input_visualizer.pixi:

Code: Select all

//Input (Mic/Line) Visualizer

set_pixel_size( WINDOW_XSIZE / 256 )
resize( get_screen(), WINDOW_XSIZE, WINDOW_YSIZE )

fn audio_callback(
    $stream, 
    $userdata, 
    $channels, 
    $frames, 
    $output_time_in_system_ticks, 
    $in_channels, 
    $latency_in_frames )
{
    if $in_channels >= 0
    {
	copy( visual_buf, $in_channels[ 0 ] )
    }
    ret( 0 ) //output is empty
}

visual_buf = new( 256, 1, INT16 )
clean( visual_buf )

set_audio_callback( audio_callback, 0, 44100, INT16, 2 )
enable_audio_input( 1 )

xsize = get_xsize( get_screen() )
ysize = get_ysize( get_screen() )
hxsize = xsize / 2
hysize = ysize / 2

while 1
{
    transp( 64 );
    clear()
    transp( 256 );

    x = 0 while( x < 256 )
    {
        line( x - 128, 0, x - 128, visual_buf[ x ] / 512, GREEN )
        x + 1
    }

    while( get_event() ) { if EVT[ EVT_TYPE ] == EVT_QUIT { halt } }

    frame()
}
I am trying to understand why this file works as intended. I am assuming that 'set_audio_callback()' is setting the destination for any audio(in or out) to 'fn audio_callback()'. But how can 'audio_callback()' be called as argument for 'set_audio_callback()'? Where is 'audio_callback' on line 6 getting it's information/arguments from? I see 'audio_callback()' is called without any arguments inside 'set_audio_callback()' while 'audio_callback()' is always returning 0.
User avatar
NightRadio
Site Admin
Posts: 3941
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

Re: Where does audio_callback get its information from?

Post by NightRadio »

set_audio_callback tells the virtual machine that the main audio in/out stream handler should call audio_callback() to get the next piece of sound (from the $channels[] arrays) and to send the next piece of mic/linein sound to the user (through the $in_channels[] arrays).
So the $channels points to the buffers to be filled by the code inside audio_callback().
Left channel buffer = $channels[0]
Right channel buffer = $channels[1]
Function return value tells the VM if the output buffers were filled or not.
Post Reply