读取声音文件,添加回声,并写入新的声音文件

时间:2014-03-07 14:33:02

标签: ios objective-c audio signal-processing

我正在努力完成以下任务:

  1. 将声音文件读入内存
  2. 将其划分为块并在预定义的偏移处添加回波(2个可能的偏移值)。
  3. 另存为新文件
  4. 我有以下代码将声音文件读入内存:

    NSURL   *urlToCAF = [NSURL URLWithString:@"simple-drum-beat.caf"];
    
    ExtAudioFileRef caf;
    OSStatus    status;
    
    status = ExtAudioFileOpenURL((__bridge CFURLRef)urlToCAF, &caf);
    if(noErr == status) {
        // request float format
        const UInt32 NumFrames = 1024;
        const int ChannelsPerFrame = 1;  // Mono, 2 for Stereo
    
        // request float format
        AudioStreamBasicDescription clientFormat;
        clientFormat.mChannelsPerFrame = ChannelsPerFrame;
        clientFormat.mSampleRate = 44100;
    
        clientFormat.mFormatID = kAudioFormatLinearPCM;
        clientFormat.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsNonInterleaved; // float
        int cmpSize = sizeof(float);
        int frameSize = cmpSize*ChannelsPerFrame;
        clientFormat.mBitsPerChannel = cmpSize*8;
        clientFormat.mBytesPerPacket = frameSize;
        clientFormat.mFramesPerPacket = 1;
        clientFormat.mBytesPerFrame = frameSize;
    
        status = ExtAudioFileSetProperty(caf, kExtAudioFileProperty_ClientDataFormat, sizeof(clientFormat), &clientFormat);
        if(noErr != status) { /* handle it */ }
    
        while(1) {
            float   buf[ChannelsPerFrame*NumFrames];
            AudioBuffer ab = { ChannelsPerFrame, sizeof(buf), buf };
            AudioBufferList abl;
            abl.mNumberBuffers = 1;
            abl.mBuffers[0] = ab;
    
            UInt32  ioNumFrames = NumFrames;
            status = ExtAudioFileRead(caf, &ioNumFrames, &abl);
    
            if(noErr == status) {
                // do something here
            }
        }
    
        // later
        status = ExtAudioFileDispose(caf);
        if(noErr != status) { /* hmm */ }
    }
    

    您能否请我提供必要的代码,将回声添加到每个块中,然后将声音保存为新文件?

    我很感激。

1 个答案:

答案 0 :(得分:0)

接近“请为我编写代码”

如果你在缓冲区中有声波,那么放入回声是微不足道的:

out[k] = in[k] + in[k-30]

保存到文件我很确定你可以找到谷歌。试着让你的问题询问一件具体的事情。

我建议调查http://theamazingaudioengine.com/ - 这会让生活变得更轻松!