Simple sample to show you how to enable/disable sound and sample a sinus wave.
Using library
GP2X minimal library by rlyeh, 2005
License
- Free for non-commercial projects (it would be nice receiving a mail from you). Other cases, ask me first.
- GamePark Holdings is not allowed to use this library and/or use parts from it.
Source code
#include "minimal.h"
int main(int argc, char *argv[])
{
unsigned long volumeL=100, volumeR=100;
gp2x_init(1000, 16, 44100,16,1,60, 1);
//resume playing (sound is paused by default)
gp2x_sound_pause(0);
while(1)
{
unsigned long pad=gp2x_joystick_read();
if(pad & GP2X_VOL_DOWN) if(pad & GP2X_START) exit(0);
if(pad & GP2X_RIGHT) { if(volumeL>0) volumeL--; volumeR=100-volumeL; gp2x_sound_volume(volumeL,volumeR); }
if(pad & GP2X_LEFT) { if(volumeL<100) volumeL++; volumeR=100-volumeL; gp2x_sound_volume(volumeL,volumeR); }
if(pad & GP2X_B) gp2x_sound_pause(1);
if(pad & GP2X_X) gp2x_sound_pause(0);
}
}
#define PI2 (3.14159f*2.f)
void gp2x_sound_frame(void *blah, void *buffer, int samples)
{
signed short *buffer16=buffer;
static float sinpos=0.0;
while(samples--)
{
int L, R;
L=R=(signed short)16384.f * sin(sinpos);
*buffer16++=L;
*buffer16++=R;
sinpos += (PI2 * 400.f) / 44100.f;
}
if(sinpos >= PI2) sinpos -= PI2;
}
Explanation
- Our gp2x_init line inits the whole library. We set 1000 ticks per second, 16 bits video mode, 44100/16bits/stereo/60Hz sound mode, and solid font.
- Our gp2x_joystick_read line reads the joystick. It is used to act properly to different button combinations.
- Our gp2x_sound_frame line fills our sound buffer with a 400 Hz sinus wave.
- Note that we just fill the given buffer with the amount of specified samples, and we fill data with the expected sample format type.
C Compilation
arm-linux-gcc minimal.c sound.c -static -lpthread -lm
C++ Compilation
arm-linux-g++ minimal.c sound.c -static -lpthread -lm
Output
- this program plays a sinus wave.
- press left or right to pan sound (stereo volume).
- press B to pause sound, and X to resume playing.
- press volume down and start buttons to exit.