devmater上openal的例子5在vc6确实可以跑的版本

本文介绍如何使用OpenAL加载和播放音频文件。通过创建缓冲区和源来实现音效的播放,并设置了监听器参数以模拟真实听觉体验。此外,还提供了加载不同类型的音效并控制其播放的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  www.openal.org
http://www.devmaster.net/articles/openal-tutorials/lesson5.php

 /**********************************************
*Change by TheCell(TheCell61@hotmail.com)
*I did not change much, other than headers and
*library included to the project
*Also, once you've downloaded the sdk, you must
*copy the files you've got from the sdk
*according to this template
*$MSDEV/
* |
* include/
* |
* al/
* |
* |  al.h
* |  alc.h
* |  alut.h
* |
* lib/
* |
* !  alut.lib
* |  openal32,lib
*
*If you don't copy all the .h files into the
*include dir, you'll get compile error
*and if you don't copy the .lib files
*to the correct directory AND and add them to
*the project, you'll get link error
**********************************************
*/


/**********************************************
*Change: Added stdio.h
* changed al/alut.c to al/alut.h
*
**********************************************
*/


/**********************************************
*Explanation 1:Since we use printf, we MUST
* include stdio.h to use this
* function
*
* 2:I have been searching for
* the alut.c files, but I have
* not found it.  So I assume
* that alut.h was intended
* instead
***********************************************
*/



#include 
<conio.h>
#include 
<stdlib.h>
#include 
<stdio.h> //For some reason, stdio.h were not included, so I added it(TheCell)
//See Explanation 1

#include 
<al/al.h>
#include 
<al/alc.h>
//#include <al/alut.c> Since I have not found alut.c anywhere, I suppose we use alut.h
#include <al/alut.h> //Added by TheCell
//See explanation 2

#include 
<vector>

using namespace std;

// These index the buffers.

#define THUNDER     
0
#define WATERDROP   
1
#define STREAM      
2
#define RAIN        
3
#define CHIMES      
4
#define OCEAN       
5
#define NUM_BUFFERS 
6

// Buffers hold sound data.
ALuint Buffers[NUM_BUFFERS];

// A vector list of sources for multiple emissions.
vector<ALuint> Sources;

// Position of the source sounds.
ALfloat SourcePos[] = 0.00.00.0 };

// Velocity of the source sounds.
ALfloat SourceVel[] = 0.00.00.0 };

// Position of the listener.
ALfloat ListenerPos[] = 0.00.00.0 };

// Velocity of the listener.
ALfloat ListenerVel[] = 0.00.00.0 };

// Orientation of the listener. (first 3 elements are "at", second 3 are "up")
ALfloat ListenerOri[] = 0.00.0-1.00.01.00.0 };


/*
 * ALboolean InitOpenAL()
 *
 *  Gets a handle to the sound device, and obtains a context to stream
 *  audio samples through.
 
*/

ALboolean InitOpenAL()
{
    ALCdevice
* pDevice;
    ALCcontext
* pContext;
    
//ALCubyte* defaultDevice; It has never been used in the code(TheCell)

    
// Get handle to device.

    pDevice 
= alcOpenDevice((ALCubyte*)"DirectSound3D");

    
// Get the device specifier.

    
//defaultDevice = alcGetString(pDevice, ALC_DEVICE_SPECIFIER);

    
//printf("Using device '%s'. ", defaultDevice);

    
// Create audio context.

    pContext 
=(struct ALCcontext_struct *) alcCreateContext(pDevice, NULL);

    
// Set active context.

    alcMakeContextCurrent(pContext);

    
// Do an error check.

    
if(alcGetError() != ALC_NO_ERROR)
        
return AL_FALSE;
        
    alGetError(); 
// Clear the bit.

    
return AL_TRUE;
}


/*
 * void ExitOpenAL()
 *
 *  This will release all contexts and devices in use.
 
*/

void ExitOpenAL()
{
    ALCcontext
* pCurContext;
    ALCdevice
* pCurDevice;

    
// Get the current context.

    pCurContext 
= (struct ALCcontext_struct *)alcGetCurrentContext();

    
// Get the device used by that context.

    pCurDevice 
= alcGetContextsDevice(pCurContext);

    
// Reset the current context to NULL.

    alcMakeContextCurrent(NULL);

    
// Release the context and the device.

    alcDestroyContext(pCurContext);
    alcCloseDevice(pCurDevice);
}


/*
 * ALboolean LoadALData()
 *
 * This function will load our sample data from the disk using the alut
 * utility and send the data into OpenAL as a buffer. A source is then
 * also created to play that buffer.
 
*/

ALboolean LoadALData()
{
// Variables to load into.

ALenum format;
ALsizei size;
ALvoid
* data;
ALsizei freq;
ALboolean loop;

// Load wav data into buffers.

alGenBuffers(NUM_BUFFERS, Buffers);

if(alGetError() != AL_NO_ERROR)
return AL_FALSE;

alutLoadWAVFile(
"wavdata/1.wav"&format, &data, &size, &freq, &loop);
alBufferData(Buffers[THUNDER], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);

alutLoadWAVFile(
"wavdata/2.wav"&format, &data, &size, &freq, &loop);
alBufferData(Buffers[WATERDROP], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);

alutLoadWAVFile(
"wavdata/3.wav"&format, &data, &size, &freq, &loop);
alBufferData(Buffers[STREAM], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);

alutLoadWAVFile(
"wavdata/4.wav"&format, &data, &size, &freq, &loop);
alBufferData(Buffers[RAIN], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);

alutLoadWAVFile(
"wavdata/5.wav"&format, &data, &size, &freq, &loop);
alBufferData(Buffers[OCEAN], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);

alutLoadWAVFile(
"wavdata/6.wav"&format, &data, &size, &freq, &loop);
alBufferData(Buffers[CHIMES], format, data, size, freq);
alutUnloadWAV(format, data, size, freq);

// Do another error check and return.

if(alGetError() != AL_NO_ERROR)
return AL_FALSE;

return AL_TRUE;
}


/*
 * void AddSource(ALint type)
 *
 *  Will add a new water drop source to the audio scene.
 
*/

void AddSource(ALint type)
{
ALuint Source;

alGenSources(
1&Source);

if(alGetError() != AL_NO_ERROR)
{
printf(
"Error generating audio source.");
exit(
-1);
}


alSourcei (Source, AL_BUFFER,   Buffers[type]);
alSourcef (Source, AL_PITCH,    
1.0f         );
alSourcef (Source, AL_GAIN,     
1.0f         );
alSourcefv(Source, AL_POSITION, SourcePos    );
alSourcefv(Source, AL_VELOCITY, SourceVel    );
alSourcei (Source, AL_LOOPING,  AL_TRUE      );

alSourcePlay(Source);

Sources.push_back(Source);
}


/*
 * void SetListenervalues()
 *
 * We already defined certain values for the listener, but we need
 * to tell OpenAL to use that data. This function does just that.
 
*/

void SetListenervalues()
{
alListenerfv(AL_POSITION,    ListenerPos);
alListenerfv(AL_VELOCITY,    ListenerVel);
alListenerfv(AL_ORIENTATION, ListenerOri);
}


/*
 * void KillALData()
 *
 * We have allocated memory for our buffers and sources which needs
 * to be returned to the system. This function frees that memory.
 
*/

void KillALData()
{
for(vector<ALuint>::iterator iter = Sources.begin(); iter != Sources.end(); iter++)
alDeleteSources(
1&(*iter));
Sources.clear();
alDeleteBuffers(NUM_BUFFERS, Buffers);
ExitOpenAL();
}


int main(int argc, char *argv[])
{
// Initialize OpenAL.

if(InitOpenAL() == AL_FALSE)
{
printf(
"Could not initialize.");
getche();
return 0;
}

        
printf(
"MindCode's OpenAL Lesson 5: Sources Sharing Buffers ");
printf(
"Controls: ");
printf(
"w) Water drops. ");
printf(
"t) Rolling thunder. ");
printf(
"s) Stream of trickling water. ");
printf(
"r) Rain. ");
printf(
"o) Lapping ocean waves. ");
printf(
"c) Wind chimes. ");
printf(
"q) Quit program. ");

// Load the wav data.

if(LoadALData() == AL_FALSE)

return 0;

SetListenervalues();

// Setup an exit procedure.

atexit(KillALData);

// Loop.

ALubyte c 
= ' ';

while(c != 'q')
{
= getche();

switch(c)
{
case 'w': AddSource(WATERDROP); break;
case 't': AddSource(THUNDER);   break;
case 's': AddSource(STREAM);    break;
case 'r': AddSource(RAIN);      break;
case 'o': AddSource(OCEAN);     break;
case 'c': AddSource(CHIMES);    break;
}
;
}


return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值