├── testal.pro └── main.cpp /testal.pro: -------------------------------------------------------------------------------- 1 | TEMPLATE = app 2 | CONFIG += console c++11 3 | CONFIG -= app_bundle 4 | CONFIG -= qt 5 | 6 | SOURCES += main.cpp 7 | 8 | LIBS += -lopenal 9 | LIBS += -lalut 10 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | int main(int argc, char *argv[]) { 6 | ALuint source; 7 | ALCdevice *device; 8 | ALCcontext *context; 9 | 10 | device = alcOpenDevice(NULL); 11 | if (device == NULL) 12 | { 13 | //ALenum error = alcGetError(); 14 | std::cout<< "error is errory"; 15 | /* do something with the error */ 16 | return -1; 17 | } 18 | /* Omit error checking */ 19 | context = alcCreateContext(device, NULL); 20 | alcMakeContextCurrent(context); 21 | 22 | /* Do more things */ 23 | alGenSources(1, &source); 24 | alSourcef(source, AL_PITCH, 1); 25 | alSourcef(source, AL_GAIN, 1); 26 | alSource3f(source, AL_POSITION, 10, 0, 0); 27 | alSource3f(source, AL_VELOCITY, 0, 0, 0); 28 | alSourcei(source, AL_LOOPING, 1); 29 | 30 | 31 | //new 32 | char* alBuffer; //data for the buffer 33 | ALenum alFormatBuffer; //buffer format 34 | ALsizei alFreqBuffer; //frequency 35 | ALsizei alBufferLen; //bit depth 36 | ALboolean alLoop; //loop 37 | unsigned int alSource; //source 38 | unsigned int alSampleSet; 39 | 40 | 41 | 42 | //load the wave file //new 43 | //http://www.gamedev.net/topic/394781-alutloadwavfile/ 44 | alutLoadWAVFile((ALbyte *)"my_music.wav",&alFormatBuffer, 45 | (void **) &alBuffer, 46 | &alBufferLen, &alFreqBuffer, &alLoop); 47 | 48 | //create a source 49 | alGenSources(1, &alSource); 50 | 51 | //create buffer 52 | alGenBuffers(1, &alSampleSet); 53 | 54 | //put the data into our sampleset buffer 55 | alBufferData(alSampleSet, alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer); 56 | 57 | //assign the buffer to this source 58 | alSourcei(alSource, AL_BUFFER, alSampleSet); 59 | 60 | //release the data 61 | alutUnloadWAV(alFormatBuffer, alBuffer, alBufferLen, alFreqBuffer); 62 | 63 | //old 64 | alDeleteSources(1, &source); 65 | alcDestroyContext(context); 66 | alcCloseDevice(device); 67 | return 0; 68 | } 69 | --------------------------------------------------------------------------------