问题
Similar to this question (Which has no answers as of now for a year).
This is how I am setting up my AKRecorder and 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
This is how I am exporting my audio:
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 {}
The problem is that size here is huge. An audiofile 2:15 in length takes about 51 MB. I know I have to reduce the sample rate and buffer size etc to reduce the size i.e. something like this:
AudioStreamBasicDescription myPCMFormat;
myPCMFormat.mSampleRate = 8000.0;
myPCMFormat.mChannelsPerFrame = 1;
myPCMFormat.mFramesPerPacket = 1;
myPCMFormat.mBitsPerChannel = 16;
myPCMFormat.mBytesPerPacket = 2;
myPCMFormat.mBytesPerFrame = 2;
myPCMFormat.mReserved = 0;
But if I set:
AKSettings.sampleRate = 8000
It crashes. I have to write the following in order to even run the Audiokit:
AKSettings.sampleRate = AudioKit.engine.inputNode.inputFormat(forBus: 0).sampleRate
mic = AKMicrophone()
And if I do that, sample rate is 48000 and the file resulting from it is huge. How do I reduce my filesize to something where 1 minute of audio takes 1 MB? A sample project is here.
回答1:
This might not be the proper way of doing it but I have managed to reduce 1 minute recording down to 975kb size. I just converted the resulting caf file to a wav file using AKConverter like this:
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
}
And after export, I converted it like this:
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)
}
}
来源:https://stackoverflow.com/questions/58772495/audiokit-recording-file-size-too-big