我正在使用Objective-C中的OpenAL API创建一个倾斜的声音(哔哔声)。基本上,我想表明电梯与用户的距离。当电梯上升或下降时声音音高是否会增加?
另外,我试图在电梯启动时降低声音,但如果音高很低,它可能不起作用(可能是因为回声)。
NewVolume= (MaxFloors - intFloors)/((double)MaxFloors)/1.4;
myPitch= pow(2.0, intFloors/12.0);
[[AudioSamplePlayer sharedInstance] playAudioSample:@"myBeep" gain:NewVolume pitch:myPitch];
有什么建议吗?
答案 0 :(得分:0)
使用Open AL示例
// SoundMan.h
@interface SoundMan : NSObject
- (void) loadSound: (NSString*) name ;
- (void) genTestSound ;
- (void) playSound ;
@end
// SoundMan.mm
@implementation SoundMan
ALCcontext *alC ;
ALCdevice *alD ;
NSUInteger sourceId ;
NSUInteger soundBufferId ;
- (id) init
{
if( self = [super init] )
{
// initializes openal
alD = alcOpenDevice(NULL);
if( alD )
{
alC = alcCreateContext( alD, NULL ) ;
alcMakeContextCurrent( alC ) ;
// Generate a test sound
[self genTestSound] ;
}
}
return self ;
}
- (void) genTestSound
{
alGenSources( 1, &sourceId );
// Load explode1.caf
NSString *path = [[NSBundle mainBundle] pathForResource:@"explode1" ofType:@"caf" ];
AudioFileID fileID ;
NSURL *afUrl = [NSURL fileURLWithPath:path];
// NON zero means err
if( AudioFileOpenURL((__bridge CFURLRef)afUrl, kAudioFileReadPermission, 0, &fileID) )
printf( "cannot open file: %s", [path UTF8String] ) ;
UInt32 fileSize = [self audioDataSize:fileID];
unsigned char *outData = (unsigned char*)malloc(fileSize);
if( AudioFileReadBytes(fileID, FALSE, 0, &fileSize, outData) )
{
printf( "Cannot load sound %s", [path UTF8String] );
return;
}
AudioFileClose(fileID);
alGenBuffers(1, &soundBufferId);
alBufferData( soundBufferId, AL_FORMAT_STEREO16, outData, fileSize, 44100 );
free( outData ) ;
}
- (void) playSound
{
// set source
alSourcei( sourceId, AL_BUFFER, soundBufferId ) ;
alSourcef( sourceId, AL_PITCH, 1 ) ;
alSourcef( sourceId, AL_GAIN, 1 ) ;
alSourcei( sourceId, AL_LOOPING, AL_FALSE ) ;
ALenum err = alGetError();
if( err ) // 0 means no error
printf( "ERROR SoundManager: %d", err ) ;
else
alSourcePlay( sourceId );
}
- (UInt32) audioDataSize:(AudioFileID)fileId {
UInt64 dataSize = 0; // dataSize
UInt32 ps = sizeof(UInt64); // property size
if( AudioFileGetProperty(fileId,
kAudioFilePropertyAudioDataByteCount, &ps, &dataSize) )
puts( "error retriving data chunk size" );
return dataSize ;
}
@end