Audiokit录音文件太大

时间:2019-11-08 19:10:36

标签: ios swift audiokit

类似于this question(到目前为止,一年中没有答案)。

这是我设置AKRecorder和Player的方式:

AKAudioFile.cleanTempDirectory()

        // Session settings
        AKSettings.bufferLength = .medium
        AKSettings.sampleRate = AudioKit.engine.inputNode.inputFormat(forBus: 0).sampleRate
        mic = AKMicrophone()!
        do {
            try AKSettings.setSession(category: .playAndRecord, with: .defaultToSpeaker)
        } catch {
            AKLog("Could not set session category.")
        }

        AKSettings.defaultToSpeaker = true

        // Patching
        let monoToStereo = AKStereoFieldLimiter(mic, amount: 1)
        micMixer = AKMixer(monoToStereo)
        micBooster = AKBooster(micMixer)

        // Will set the level of microphone monitoring
        micBooster.gain = 0
        recorder = try? AKNodeRecorder(node: micMixer)
        if let audio = audioFile {
            player = AKPlayer(audioFile: audio)
        }
        else if let file = recorder.audioFile {
            player = AKPlayer(audioFile: file)
        }
        player.isLooping = false
        player.completionHandler = playingEnded

        moogLadder = AKMoogLadder(player)

        mainMixer = AKMixer(moogLadder, micBooster)

        AudioKit.output = mainMixer
        do {
            try AudioKit.start()
        } catch {
            AKLog("AudioKit did not start!")
        }
        self.plot?.node = self.mic

这就是我导出音频的方式:

tape = recorder.audioFile!
player.load(audioFile: tape)
let fileName = self.primaryRecording.replacingOccurrences(of: ".wav", with: ".caf")
tape.exportAsynchronously(name: fileName,
                      baseDir: .documents,
                      exportFormat: .caf) {file, exportError in
if let error = exportError {
    AKLog("Export Failed \(error)")
    exported(false)
}
else {}

问题在于这里的大小很大。长度为2:15的音频文件大约需要51 MB。我知道我必须降低采样率和缓冲区大小等以减小大小,例如:

    AudioStreamBasicDescription myPCMFormat;
    myPCMFormat.mSampleRate = 8000.0;
    myPCMFormat.mChannelsPerFrame = 1;
    myPCMFormat.mFramesPerPacket = 1;
    myPCMFormat.mBitsPerChannel = 16;
    myPCMFormat.mBytesPerPacket = 2;
    myPCMFormat.mBytesPerFrame = 2;
    myPCMFormat.mReserved = 0;

但是如果我设置:

AKSettings.sampleRate = 8000

它崩溃了。我必须编写以下内容才能运行Audiokit:

   AKSettings.sampleRate = AudioKit.engine.inputNode.inputFormat(forBus: 0).sampleRate
    mic = AKMicrophone()

如果我这样做,则采样率为48000,由此得出的文件很大。如何将文件大小减小到1分钟的音频占用1 MB的空间?一个示例项目是here

1 个答案:

答案 0 :(得分:0)

这可能不是正确的方法,但是我设法将1分钟的记录减少到975kb。我只是使用AKConverter将生成的caf文件转换为wav文件,如下所示:

func getAKConverterOptions()-> AKConverter.Options{
        var options = AKConverter.Options()
        // any options left nil will assume the value of the input file
        options.format = "wav"
        options.sampleRate = 8000
        options.bitRate = 16
        options.channels = 1
        options.bitDepth = 16
        options.eraseFile = true
        options.isInterleaved = true
        return options
    }

导出后,我将其转换为:

let converter = AKConverter(inputURL: self.getFilePath(name: self.baseRecording.replacingOccurrences(of: ".wav", with: ""), extension: "caf"), outputURL: self.getFilePath(name: self.baseRecording.replacingOccurrences(of: ".wav", with: ""), extension: "wav"), options: self.getAKConverterOptions())
converter.start { (error) in
         if error == nil {
                   exported(true)
         }
         else{
                  exported(false)
         }
}