Skip to content

Instantly share code, notes, and snippets.

@hovermind
Last active June 18, 2020 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hovermind/d114548c073573f08e98541f7f9b6b5b to your computer and use it in GitHub Desktop.
Save hovermind/d114548c073573f08e98541f7f9b6b5b to your computer and use it in GitHub Desktop.
LinearPCMRecording class in AudioUtil
/**
Leaner PCM recording setting model
This class represents Linear PCM recording related settings
*/
public class LinearPCMRecording {
//
// these constants are only to avoid magic numbers
//
private static let SAMPLE_RATE_16_KHZ = 16_000
private static let BIT_DEPTH_16 = 16
private static let CHANNEL_MONO = 1
//
// bit rate will be automatically calculated from sampleRate, bitDepth and channelCount
//
public private(set) var bitRate = SAMPLE_RATE_16_KHZ * BIT_DEPTH_16 * CHANNEL_MONO
private func updateBitRate(){
Logger.logIt(#function)
bitRate = sampleRate * bitDepth * channelCount
}
//
// Linear PCM sample rate for AVAudioRecorder
//
public var sampleRate:Int = SAMPLE_RATE_16_KHZ {
willSet {
updateBitRate()
updateLinearPCMRecordingSettings()
}
}
//
// Linear PCM channel count for AVAudioRecorder
//
public var channelCount:Int = CHANNEL_MONO {
willSet {
updateBitRate()
updateLinearPCMRecordingSettings()
}
}
//
// Linear PCM bit depth for AVAudioRecorder
//
public var bitDepth:Int = BIT_DEPTH_16 {
willSet {
updateBitRate()
updateLinearPCMRecordingSettings()
}
}
//
// Linear PCM format settings for AVAudioRecorder
//
public var isFormatFloat = false {
didSet {
updateLinearPCMRecordingSettings()
}
}
public var isFormatBigEndian = false {
didSet {
updateLinearPCMRecordingSettings()
}
}
//
// Designed initilizer
//
public init(sampleRate: Int, channelCount: Int, bitDepth: Int){
self.sampleRate = sampleRate
self.channelCount = channelCount
self.bitDepth = bitDepth
updateBitRate()
}
//
// Convinience initilizer
//
public convenience init(sampleRate: Int, channelCount: Int, bitDepth: Int, isFormatFloat: Bool, isFormatBigEndian: Bool){
self.init(sampleRate: sampleRate, channelCount: channelCount, bitDepth: bitDepth)
self.isFormatFloat = isFormatFloat
self.isFormatBigEndian = isFormatBigEndian
updateLinearPCMRecordingSettings()
}
//
// Recorder setting for Linear PCM
//
public static let LINEAR_PCM_RECODING_SETTINGS_DEFAULT = [
AVFormatIDKey: kAudioFormatLinearPCM,
AVSampleRateKey: SAMPLE_RATE_16_KHZ,
AVNumberOfChannelsKey: CHANNEL_MONO,
AVLinearPCMBitDepthKey: BIT_DEPTH_16,
AVLinearPCMIsFloatKey: false
] as [String : Any]
public var recordingSettings = LINEAR_PCM_RECODING_SETTINGS_DEFAULT
private func updateLinearPCMRecordingSettings(){
Logger.logIt(#function)
recordingSettings = [
AVFormatIDKey: kAudioFormatLinearPCM,
AVSampleRateKey: sampleRate,
AVNumberOfChannelsKey: channelCount,
AVLinearPCMBitDepthKey: bitDepth,
AVLinearPCMIsFloatKey: isFormatFloat,
AVLinearPCMIsBigEndianKey: isFormatBigEndian
] as [String : Any]
}
//
// Default Linear PCM recording settings
//
public static let `default` = LinearPCMRecording(sampleRate: SAMPLE_RATE_16_KHZ, channelCount: CHANNEL_MONO, bitDepth: BIT_DEPTH_16)
}
// rest is here: https://stackoverflow.com/questions/59622288/how-to-encode-self-delimited-opus-in-ios
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment