Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using only custom audio settings if provided #549

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 91 additions & 67 deletions ios/RNAudioRecorderPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,44 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
subscriptionDuration = duration
}

func getAvFormatFromEncoding(encoding: String?) -> Int? {
var avFormat: Int?

if (encoding == "lpcm") {
avFormat = Int(kAudioFormatAppleIMA4)
} else if (encoding == "ima4") {
avFormat = Int(kAudioFormatAppleIMA4)
} else if (encoding == "aac") {
avFormat = Int(kAudioFormatMPEG4AAC)
} else if (encoding == "MAC3") {
avFormat = Int(kAudioFormatMACE3)
} else if (encoding == "MAC6") {
avFormat = Int(kAudioFormatMACE6)
} else if (encoding == "ulaw") {
avFormat = Int(kAudioFormatULaw)
} else if (encoding == "alaw") {
avFormat = Int(kAudioFormatALaw)
} else if (encoding == "mp1") {
avFormat = Int(kAudioFormatMPEGLayer1)
} else if (encoding == "mp2") {
avFormat = Int(kAudioFormatMPEGLayer2)
} else if (encoding == "mp4") {
avFormat = Int(kAudioFormatMPEG4AAC)
} else if (encoding == "alac") {
avFormat = Int(kAudioFormatAppleLossless)
} else if (encoding == "amr") {
avFormat = Int(kAudioFormatAMR)
} else if (encoding == "flac") {
if #available(iOS 11.0, *) {
avFormat = Int(kAudioFormatFLAC)
}
} else if (encoding == "opus") {
avFormat = Int(kAudioFormatOpus)
}

return avFormat
}

/********** Player **********/

@objc(startRecorder:audioSets:meteringEnabled:resolve:reject:)
Expand All @@ -146,60 +184,69 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {

_meteringEnabled = meteringEnabled;

let encoding = audioSets["AVFormatIDKeyIOS"] as? String
let mode = audioSets["AVModeIOS"] as? String
let avLPCMBitDepth = audioSets["AVLinearPCMBitDepthKeyIOS"] as? Int
let avLPCMIsBigEndian = audioSets["AVLinearPCMIsBigEndianKeyIOS"] as? Bool
let avLPCMIsFloatKey = audioSets["AVLinearPCMIsFloatKeyIOS"] as? Bool
let avLPCMIsNonInterleaved = audioSets["AVLinearPCMIsNonInterleavedIOS"] as? Bool
let sampleRate = audioSets["AVSampleRateKeyIOS"] as? Int
let numberOfChannel = audioSets["AVNumberOfChannelsKeyIOS"] as? Int
let audioQuality = audioSets["AVEncoderAudioQualityKeyIOS"] as? Int
let bitRate = audioSets["AVEncoderBitRateKeyIOS"] as? Int
let avFormat: Int? = getAvFormatFromEncoding(encoding: audioSets["AVFormatIDKeyIOS"] as? String)

var avFormat: Int? = nil
let mode = audioSets["AVModeIOS"] as? String
var avMode: AVAudioSession.Mode = AVAudioSession.Mode.default
var sampleRate = audioSets["AVSampleRateKeyIOS"] as? Int
var numberOfChannel = audioSets["AVNumberOfChannelsKeyIOS"] as? Int
var audioQuality = audioSets["AVEncoderAudioQualityKeyIOS"] as? Int
var bitRate = audioSets["AVEncoderBitRateKeyIOS"] as? Int

let defaultSettings: [String: Any] = [
AVSampleRateKey: 44100,
AVFormatIDKey: Int(kAudioFormatAppleLossless),
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.medium.rawValue,
AVLinearPCMBitDepthKey: AVLinearPCMBitDepthKey.count,
AVLinearPCMIsBigEndianKey: true,
AVLinearPCMIsFloatKey: false,
AVLinearPCMIsNonInterleaved: false,
AVEncoderBitRateKey: 128000
]

var customSettings: [String : Any] = [:]

setAudioFileURL(path: path)

if (sampleRate == nil) {
sampleRate = 44100;
if (sampleRate != nil) {
customSettings[AVSampleRateKey] = sampleRate
}

if (encoding == nil) {
avFormat = Int(kAudioFormatAppleLossless)
} else {
if (encoding == "lpcm") {
avFormat = Int(kAudioFormatAppleIMA4)
} else if (encoding == "ima4") {
avFormat = Int(kAudioFormatAppleIMA4)
} else if (encoding == "aac") {
avFormat = Int(kAudioFormatMPEG4AAC)
} else if (encoding == "MAC3") {
avFormat = Int(kAudioFormatMACE3)
} else if (encoding == "MAC6") {
avFormat = Int(kAudioFormatMACE6)
} else if (encoding == "ulaw") {
avFormat = Int(kAudioFormatULaw)
} else if (encoding == "alaw") {
avFormat = Int(kAudioFormatALaw)
} else if (encoding == "mp1") {
avFormat = Int(kAudioFormatMPEGLayer1)
} else if (encoding == "mp2") {
avFormat = Int(kAudioFormatMPEGLayer2)
} else if (encoding == "mp4") {
avFormat = Int(kAudioFormatMPEG4AAC)
} else if (encoding == "alac") {
avFormat = Int(kAudioFormatAppleLossless)
} else if (encoding == "amr") {
avFormat = Int(kAudioFormatAMR)
} else if (encoding == "flac") {
if #available(iOS 11.0, *) {
avFormat = Int(kAudioFormatFLAC)
}
} else if (encoding == "opus") {
avFormat = Int(kAudioFormatOpus)
}
if (avFormat != nil) {
customSettings[AVFormatIDKey] = avFormat
}

if (numberOfChannel != nil) {
customSettings[AVNumberOfChannelsKey] = numberOfChannel
}

if (audioQuality != nil) {
customSettings[AVEncoderAudioQualityKey] = audioQuality
}

if (bitRate != nil) {
customSettings[AVEncoderBitRateKey] = bitRate
}

if (avLPCMBitDepth != nil) {
customSettings[AVLinearPCMBitDepthKey] = avLPCMBitDepth
}

if (avLPCMIsBigEndian != nil) {
customSettings[AVLinearPCMIsBigEndianKey] = avLPCMIsBigEndian
}

if (avLPCMIsFloatKey != nil) {
customSettings[AVLinearPCMIsFloatKey] = avLPCMIsFloatKey
}

if(avLPCMIsNonInterleaved != nil) {
customSettings[AVLinearPCMIsNonInterleaved] = avLPCMIsNonInterleaved
}

if (mode == "measurement") {
Expand All @@ -224,31 +271,8 @@ class RNAudioRecorderPlayer: RCTEventEmitter, AVAudioRecorderDelegate {
}
}


if (numberOfChannel == nil) {
numberOfChannel = 2
}

if (audioQuality == nil) {
audioQuality = AVAudioQuality.medium.rawValue
}

if (bitRate == nil) {
bitRate = 128000
}

func startRecording() {
let settings = [
AVSampleRateKey: sampleRate!,
AVFormatIDKey: avFormat!,
AVNumberOfChannelsKey: numberOfChannel!,
AVEncoderAudioQualityKey: audioQuality!,
AVLinearPCMBitDepthKey: avLPCMBitDepth ?? AVLinearPCMBitDepthKey.count,
AVLinearPCMIsBigEndianKey: avLPCMIsBigEndian ?? true,
AVLinearPCMIsFloatKey: avLPCMIsFloatKey ?? false,
AVLinearPCMIsNonInterleaved: avLPCMIsNonInterleaved ?? false,
AVEncoderBitRateKey: bitRate!
] as [String : Any]
let settings = customSettings.count > 0 ? customSettings : defaultSettings
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this ignore the default values when one of customSettings provided? For example, when AVSampleRateKey is provided, what are other default values be?


do {
audioRecorder = try AVAudioRecorder(url: audioFileURL!, settings: settings)
Expand Down