2011-07-06 28 views

回答

3

我曾在一個示例應用程序,該應用程序記錄用戶所說的並向後播放它們。我已經使用CoreAudio來實現這一點。鏈接到應用程序代碼。

由於每個樣本的大小爲16位(2字節)(單聲道)。您可以一次加載每個樣本,方法是從錄製結束開始並向後讀取,將其複製到不同的緩衝區中。當你到達數據的開始時,你已經轉換了數據並且播放將被顛倒過來。

// set up output file 
AudioFileID outputAudioFile; 

AudioStreamBasicDescription myPCMFormat; 
myPCMFormat.mSampleRate = 16000.00; 
myPCMFormat.mFormatID = kAudioFormatLinearPCM ; 
myPCMFormat.mFormatFlags = kAudioFormatFlagsCanonical; 
myPCMFormat.mChannelsPerFrame = 1; 
myPCMFormat.mFramesPerPacket = 1; 
myPCMFormat.mBitsPerChannel = 16; 
myPCMFormat.mBytesPerPacket = 2; 
myPCMFormat.mBytesPerFrame = 2; 


AudioFileCreateWithURL((__bridge CFURLRef)self.flippedAudioUrl, 
         kAudioFileCAFType, 
         &myPCMFormat, 
         kAudioFileFlags_EraseFile, 
         &outputAudioFile); 
// set up input file 
AudioFileID inputAudioFile; 
OSStatus theErr = noErr; 
UInt64 fileDataSize = 0; 

AudioStreamBasicDescription theFileFormat; 
UInt32 thePropertySize = sizeof(theFileFormat); 

theErr = AudioFileOpenURL((__bridge CFURLRef)self.recordedAudioUrl, kAudioFileReadPermission, 0, &inputAudioFile); 

thePropertySize = sizeof(fileDataSize); 
theErr = AudioFileGetProperty(inputAudioFile, kAudioFilePropertyAudioDataByteCount, &thePropertySize, &fileDataSize); 

UInt32 dataSize = fileDataSize; 
void* theData = malloc(dataSize); 

//Read data into buffer 
UInt32 readPoint = dataSize; 
UInt32 writePoint = 0; 
while(readPoint > 0) 
{ 
    UInt32 bytesToRead = 2; 

    AudioFileReadBytes(inputAudioFile, false, readPoint, &bytesToRead, theData); 
    AudioFileWriteBytes(outputAudioFile, false, writePoint, &bytesToRead, theData); 

    writePoint += 2; 
    readPoint -= 2; 
} 

free(theData); 
AudioFileClose(inputAudioFile); 
AudioFileClose(outputAudioFile); 
+0

有沒有辦法用.m4a文件而不是.caf文件做到這一點? –

2

一個WAV文件只包含原始PCM樣本。如果您按相反順序播放它們,您將聽到相反的聲音。 See this question for details.(也this search

+0

我把它一個PCM採樣僅僅是一個不同的聲音? –

+1

PCM樣本是模擬波形的一個數字樣本(並且聲音最終只是一個波形...如果向後播放波形,則會得到反向聲音)請參閱http://en.wikipedia.org/上的圖形維基/脈衝code_modulation。每個離散級別是一個樣本。 WAV文件只是這些樣本的數組(加上一些元數據)。反轉陣列,將其饋送到您的WAV播放器,然後就完成了。 – AShelly

1

使用libsndfile,我能夠錄製的音頻樣本中閱讀。下面的代碼片斷展示瞭如何打開輸入文件,並以相反的順序寫入輸出。儘管所有數據都是在內存中讀取的,但您應該注意!

SF_INFO info_read; 
info_read.format = 0; 
SNDFILE* sndfilein = sf_open("my_input_file.caf", SFM_READ, &info_read); 

SF_INFO info_write; 
info_write.format = info.format; 
info_write.channels = info.channels; 
info_write.frames = info.frames; 
info_write.samplerate = info.samplerate; 
info_write.sections = info.sections; 
info_write.seekable = info.seekable; 

SNDFILE* sndfileout = sf_open("my_output_file.caf", SFM_RDWR, &info_write); 

int* buf = new int[8000 * 30]; 
int framesRead = sf_readf_int(sndfilein, buf, info.frames); 
for (int i = 0; i < info.frames; i++) 
    sf_writef_int(sndfileout, &buf[info.frames - i], 1); 
delete[] buf; 

sf_close(sndfilein); 
sf_close(sndfileout); 
1

Kiran的斯威夫特2.2答案:

let forwardAudioURL: NSURL = ... // wherever your original audio is 
    let reversedAudioURL: NSURL = ... // wherever you want the reversed file to go 

    // Load forward audio into originalAudioFile 
    var originalAudioFile: AudioFileID = nil 
    let possibleError1 = AudioFileOpenURL(forwardAudioURL, 
              AudioFilePermissions.ReadPermission, 
              0, 
              &originalAudioFile) 

    // Load the size in bytes of the original audio into originalAudioSize variable 
    var originalAudioSize: Int64 = 0 
    var propertySize: UInt32 = 8 
    let possibleError2 = AudioFileGetProperty(originalAudioFile, 
               kAudioFilePropertyAudioDataByteCount, 
               &propertySize, 
               &originalAudioSize) 

    if possibleError1 != 0 || possibleError2 != 0 { 
     // Handle errors if you want 
    } 

    // Set up file that the reversed audio will be loaded into 
    var reversedAudioFile: AudioFileID = nil 
    var format = AudioStreamBasicDescription() 
    format.mSampleRate = 16000 
    format.mFormatID = kAudioFormatLinearPCM 
    format.mChannelsPerFrame = 1 
    format.mFramesPerPacket = 1 
    format.mBitsPerChannel = 16 
    format.mBytesPerPacket = 2 
    format.mBytesPerFrame = 2 
    AudioFileCreateWithURL(reversedAudioURL, 
          kAudioFileCAFType, 
          &format, 
          AudioFileFlags.EraseFile, 
          &reversedAudioFile) 

    // Read data into the reversedAudioFile 
    var readPoint: Int64 = originalAudioSize 
    var writePoint: Int64 = 0 
    var buffer: Int16 = 0 
    while readPoint > 0 { 
     var bytesToRead: UInt32 = 2; 
     AudioFileReadBytes(originalAudioFile, 
          false, 
          readPoint, 
          &bytesToRead, 
          &buffer) 
     AudioFileWriteBytes(reversedAudioFile, 
          false, 
          writePoint, 
          &bytesToRead, 
          &buffer) 
     writePoint += 2 
     readPoint -= 2 
    } 
    AudioFileClose(originalAudioFile) 
    AudioFileClose(reversedAudioFile) 
+0

我試過這段代碼,但播放速度變慢,1:45分鐘的音頻文件變爲4:15分鐘。任何解決方案? @ mckennac4 –