Audio Howto

From OESF

Jump to: navigation, search

Zaurus audio development howto: by L.J. Potter

To get brief audio device info: run this command from the console:

 # cat /dev/sndstat

The VERY easiest way to play sound is to use use cat command such as this:

 # cat myNewSound >/dev/dsp
or in a program, use system()
system( "cat myNewSound >/dev/dsp");

The VERY easiest way to record sound is to use use cat command such as this:

 # cat /dev/dsp1 >myNewSound
or in a program, use system()
system( "cat /dev/dsp1 >myNewSound");

This is not the most elegant way to do this.


The next easiest way to play audio is to use QSound: The benefits are that you should be able to play more than one sound at a time. (*) NOTE: there is no QSound::record()

 #include <qsound.h>
 #include <qpe/resource.h>
  
QSound
:play( Resource::findSound("ohdear") );

(*) NOTE: There is a bug on the Zaurus with QSound not being able to play sounds shorter than about 8k.


or if you can use Qtopia's Sound class to loop sounds: (there's no docs, so grab the qtopia cvs at sourceforge and look in the source)

 #include <qpe/sound.h>
 #include <qpe/resource.h>
Sound
:soundAlarm();

or something like:

Sound mySound( Resource
:findSound("ohdear") );
mySound.playLoop();
mySound.stop();

(*) NOTE: There is a bug on the Zaurus with QSound not being able to play sounds shorter than about 8k.


Using oss with the Zaurus: Playing sounds:

 #include <sys/ioctl.h>
 #include <linux/soundcard.h>
int speed = 44100;
int channels = 2;
int format = AFMT_S16_LE; // or AFMT_U8, but has buggy output on the Z
int dsp=0;
signed short soundBuffer[1024];
dsp = 
:open("/dev/dsp", O_WRONLY);
if( dsp == -1) {
perror("open(\"/dev/dsp\")");
return;
}
 // this is assuming you know what the format of the wav file is.
 // otherwise you'd have to read the header file first.
if( ioctl( dsp, SNDCTL_DSP_SETFMT , &format)==-1) {
perror("ioctl(\"SNDCTL_DSP_SETFMT\")");
return -1;
}
if( ioctl( dsp, SNDCTL_DSP_CHANNELS , &channels)==-1) {
perror("ioctl(\"SNDCTL_DSP_CHANNELS\")");
return -1;
}
if( ioctl( dsp, SNDCTL_DSP_SPEED , &speed)==-1) {
perror("ioctl(\"SNDCTL_DSP_SPEED\")");
return -1;
}
QFile track( "/opt/QtPalmtop/sounds/somesound.wav" );
if( track.open( IO_ReadOnly )) {
QMessageBox:message("Sounds","There was an error opening the wav file");
return -1;
}
int oldStyleFileHandle = track.handle();
int length;
while( track.atEnd()) {
length = ::read( oldStyleFileHandle, soundBuffer, 1024 ); //read from the file
 ::write( dsp, soundBuffer, length); //write to the device
}
 
:close (dsp);
track.close();

To record:

 #include <sys/ioctl.h>
 #include <linux/soundcard.h>
int speed = 22050;
int channels = 1;
// *NOTE* the Zaurus input is ONLY mono !!
int format = AFMT_S16_LE;
int dsp=0;
signed short soundBuffer[1024];
dsp = 
:open("/dev/dsp1", O_RDONLY);
// *NOTE* the Zaurus has a nonstandard input
// so /dev/dsp1 must be opened when
// recording
if( dsp == -1) {
perror("open(\"/dev/dsp\")");
return;
}
// this is assuming you know what the format of the wav file is.
// otherwise you'd have to read the header file first.
if( ioctl( dsp, SNDCTL_DSP_SETFMT , &format)==-1) {
perror("ioctl(\"SNDCTL_DSP_SETFMT\")");
return -1;
}
if( ioctl( dsp, SNDCTL_DSP_CHANNELS , &channels)==-1) {
perror("ioctl(\"SNDCTL_DSP_CHANNELS\")");
return -1;
}
if( ioctl( dsp, SNDCTL_DSP_SPEED , &speed)==-1) {
perror("ioctl(\"SNDCTL_DSP_SPEED\")");
return -1;
}
QFile track( "/opt/QtPalmtop/sounds/myNewSound.wav" );
if( track.open( IO_WriteOnly | IO_Truncate )) {
QMessageBox:message("Sounds","There was an error opening the wav file");
return -1;
}
int oldStyleFileHandle = track.handle();
int length;
while( 1) { // or some other way to stop this crazy thing
length = 
:read( dsp, soundBuffer, 1024 ); //read from the file
 ::write( oldStyleFileHandle, soundBuffer, length); //write to the device
}
 
:close (dsp);
track.close();
// to make a standard wav file, you would need to write
// a header before you start writing from the device, and then
// write to the header after you finish

For more information about programming OSS, visit: http://www.opensound.com


This page was last updated: May 21 2003 06:25:12.

Personal tools