我正在使用 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];

有什么建议么?


使用开放 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