Created
November 2, 2011 11:22
-
-
Save dlivingstone/1333413 to your computer and use it in GitHub Desktop.
BASS audio test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BASS demo test | |
// A very basic OpenGL/GLUT application | |
// Create a window, load some sounds and play them. | |
// A test program for BASS - available from http://www.un4seen.com/ | |
// This program uses two sound effects - available from http://www.freesound.org/ | |
// To test this program you will need two wav sound effect files! | |
#include "bass.h" | |
#include <iostream> | |
#include <GL/glut.h> | |
using std::cout; | |
using std::endl; | |
HSAMPLE *samples=NULL; | |
void printString(void *font, char* str) | |
{ | |
while (*str != NULL) | |
glutBitmapCharacter(font, *str++); | |
} | |
void display(void) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT); // Just clear the screen | |
glColor3f (1.0, 1.0, 1.0); // set colour ( R, G, B ) | |
glRasterPos2f(-0.95,0.8); | |
printString(GLUT_BITMAP_HELVETICA_18, "Press '1' and/or '2' to play sounds"); | |
glRasterPos2f(-0.95,0.5); | |
printString(GLUT_BITMAP_HELVETICA_18, "'p' to pause and 'r' to resume"); | |
glFlush(); // Redraw (single buffered) | |
} | |
HSAMPLE loadSample(char * filename) | |
{ | |
HSAMPLE sam; | |
if (sam=BASS_SampleLoad(FALSE,filename,0,0,3,BASS_SAMPLE_OVER_POS)) | |
cout << "sample " << filename << " loaded!" << endl; | |
else | |
{ | |
cout << "Can't load sample"; | |
exit (0); | |
} | |
return sam; | |
} | |
void init(void) | |
{ | |
glClearColor(0.0, 0.0, 0.0, 0.0); // set background colour | |
/* Initialize default output device */ | |
if (!BASS_Init(-1,44100,0,0,NULL)) | |
cout << "Can't initialize device"; | |
samples = new HSAMPLE[2]; | |
// Following comment is from source basstest file! | |
/* Load a sample from "file" and give it a max of 3 simultaneous | |
playings using playback position as override decider */ | |
samples[0] = loadSample( "bulletsound1.wav"); | |
samples[1] = loadSample( "car_idle.wav"); | |
} | |
void keyboard(unsigned char key, int x, int y) | |
{ | |
switch (key) | |
{ | |
case 27: | |
// Free up the BASS resources | |
BASS_Free(); | |
exit(0); | |
case '1': | |
{ // These curly braces are strictly not required but | |
// Visual Studio .NET 2003 complains if they are not present | |
HCHANNEL ch=BASS_SampleGetChannel(samples[0],FALSE); | |
BASS_ChannelSetAttributes(ch,-1,50,(rand()%201)-100); | |
if (!BASS_ChannelPlay(ch,FALSE)) | |
cout << "Can't play sample" << endl; | |
} | |
break; | |
case '2': | |
{ // See comment above! | |
HCHANNEL ch=BASS_SampleGetChannel(samples[1],FALSE); | |
BASS_ChannelSetAttributes(ch,-1,50,(rand()%201)-100); | |
if (!BASS_ChannelPlay(ch,FALSE)) | |
cout << "Can't play sample" << endl; | |
} | |
break; | |
case 'p': | |
// Pause output | |
BASS_Pause(); | |
break; | |
case 'r': | |
// Resume playing | |
BASS_Start(); | |
break; | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
glutInitWindowSize(320, 160); | |
glutInitWindowPosition(150, 150); | |
glutCreateWindow("Basic GL/GLUT App"); | |
init(); | |
if (BASS_GetVersion()!=MAKELONG(2,2)) { | |
MessageBox(0,"BASS version 2.2 was not loaded","Incorrect BASS.DLL",0); | |
return 0; | |
} | |
glutKeyboardFunc(keyboard); | |
glutDisplayFunc(display); | |
glutMainLoop(); // loop while waiting for messages | |
return 0; // return 0 indicates program exited OK | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment