miércoles, 17 de septiembre de 2008

PHP y OpenAL

Me sorprendió mucho ver que se puede programar OpenAl desde PHP.

En el manual de PHP capitulo “Audio Formats Manipulation” explica de forma muy clara como llamar los métodos necesarios para inicializar un dispositivo, crear buffers, ejecutarlos, etc...

Queda claro que PHP y OpenAL debe estar instalado en el cliente previamente, y que la ejecución es de forma local, no es que el código PHP se ejecute en el servidor y el sonido se reproduzca en el PC cliente.

Es muy interesante, que desde un lenguaje tan practico y sencillo como PHP, que además tiene tanta difusión, podamos desarrollar audiojuegos.

El siguiente es un ejemplo de cómo acceder OpenAL desde PHP, el mismo fue obtenido de un comentario en la documentación del sitio del lenguaje.

Playing a wav file:

function printok($b) {
echo $b ? " ok\n" : " error\n";
}

echo "Opening device";
$device = openal_device_open();
printok($device);

echo "Creating context";
$context = openal_context_create($device);
printok($context);
echo "Making context the current";
printok(openal_context_current($context));

//where we are
//echo "Setting listener position";
//printok(openal_listener_set(AL_POSITION, array(0, 0, 0)));
//echo "Setting listener orientation";
//printok(openal_listener_set(AL_ORIENTATION, array(0=>0,1 => 1, 2=>0, 3=> 0, 4=>4, 5=>5)));

echo "Creating buffer";
$buffer = openal_buffer_create();
echo "Loading wav";
printok(openal_buffer_loadwav($buffer, '/data/shared/vmware/newmessage.wav'));
echo "buffer stats\n";
echo " Frequency: " . openal_buffer_get($buffer, AL_FREQUENCY) . "\n";
echo " Bits : " . openal_buffer_get($buffer, AL_BITS) . "\n";
echo " Channels : " . openal_buffer_get($buffer, AL_CHANNELS) . "\n";
echo " Size : " . openal_buffer_get($buffer, AL_SIZE) . "\n";

echo "Creating source";
$source = openal_source_create();

echo "Setting source buffer";
printok(openal_source_set($source, AL_BUFFER, $buffer));
//echo "Setting source position";
//printok(openal_source_set($source, AL_POSITION, array(1, 0, 0)));

echo "Playing source";
printok(openal_source_play($source));

echo "sleeping\n";
//sleep(1);
//we wait 300msecs beause the sound
// has no time to be played otherwise
//since playing sound is done concurrently
// to the script
usleep(300000);

echo "Destroying source";
printok(openal_source_destroy($source));
echo "Destroying buffer";
printok(openal_buffer_destroy($buffer));
echo "Destroying context";
printok(openal_context_destroy($context));
echo "Closing device";
printok(openal_device_close($device));
?>

No hay comentarios: