in Education by
Currently, I'm getting an EXC_BAD_ACCESS error on the audio thread, and I'm trying to deduce what is going wrong. When converting .wav file data from Data to an AVAudioPCMBuffer, do I need to strip the RIFF header first? import AVFoundation public class Player : NSObject { let engine = AVAudioEngine() public override init() { super.init() do { let _ = engine.mainMixerNode try engine.start() } catch { print("Player error: \(error)") } } @objc public func play(_ data: Data) { let format = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 48000, channels: 2, interleaved: true)! let buffer = data.toPCMBuffer(format: format)! let player = AVAudioPlayerNode() engine.attach(player) engine.connect(player, to: engine.mainMixerNode, format: nil) player.scheduleBuffer(buffer, at: nil, completionCallbackType: .dataPlayedBack) { callbackType in // Nothing in here. } player.play() } } Here's the toPCMBuffer extension: // Taken from: https://stackoverflow.com/a/52731480/2228559 extension Data { func toPCMBuffer(format: AVAudioFormat) -> AVAudioPCMBuffer? { let streamDesc = format.streamDescription.pointee let frameCapacity = UInt32(count) / streamDesc.mBytesPerFrame guard let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frameCapacity) else { return nil } buffer.frameLength = buffer.frameCapacity let audioBuffer = buffer.audioBufferList.pointee.mBuffers withUnsafeBytes { addr in audioBuffer.mData?.copyMemory(from: addr, byteCount: Int(audioBuffer.mDataByteSize)) } return buffer } } Note: I cannot use AVAudioFile because the .wav file data is loaded over-the-wire. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
IDK, but my mac crashes if I play interleaved AVAudioPCMBuffers, and garbled audio if they're not float data, so you could convert to non-interleaved float data: @objc public func play(_ data: Data) { let sampleRate: Double = 48000 let interleavedFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: sampleRate, channels: 2, interleaved: true)! let interleavedBuffer = data.toPCMBuffer(format: interleavedFormat)! let nonInterleavedFormat = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: sampleRate, channels: 2, interleaved: false)! let nonInterleavedBuffer = AVAudioPCMBuffer(pcmFormat: nonInterleavedFormat, frameCapacity: interleavedBuffer.frameCapacity)! nonInterleavedBuffer.frameLength = interleavedBuffer.frameLength let converter = AVAudioConverter(from: interleavedFormat, to: nonInterleavedFormat)! try! converter.convert(to: nonInterleavedBuffer, from: interleavedBuffer) let player = AVAudioPlayerNode() engine.attach(player) engine.connect(player, to: engine.mainMixerNode, format: nil) player.scheduleBuffer(nonInterleavedBuffer, at: nil, completionCallbackType: .dataPlayedBack) { callbackType in // Nothing in here. } player.play() } extension Data { func toPCMBuffer(format: AVAudioFormat) -> AVAudioPCMBuffer? { assert(format.isInterleaved) let streamDesc = format.streamDescription.pointee let frameCapacity = UInt32(count) / streamDesc.mBytesPerFrame guard let buffer = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: frameCapacity) else { return nil } buffer.frameLength = buffer.frameCapacity let b = UnsafeMutableBufferPointer(start: buffer.int16ChannelData![0], count: buffer.stride * Int(frameCapacity)) let bytesCopied = self.copyBytes(to: b) assert(bytesCopied == count) return buffer } }

Related questions

0 votes
    Currently, I'm getting an EXC_BAD_ACCESS error on the audio thread, and I'm trying to deduce what is ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    1. What is the commonest type of compressed digital audio file ? (c) .mp3 (d) .wav (b) .flac (a) ... to download and poorer quality Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
0 votes
    I have a mobile website that plays a background song. When I pressed the home button on my phone ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I'm currently working on a project which is a small educational game for children. Right now I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I'm currently working on a project which is a small educational game for children. Right now I am ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    I want to play a external SWF file (which is in server) in client side. It is working when I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    Two card are drawn from a pack of playing cards, one after the other, find the probability of getting a ... (ii) Without replacement Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    Two cards are drawn at random and without replacement from a pack of 52 playing cards. Find the probability that ... cards are black. Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    A card is drawn from a pack of 52 playing cards What is the probability that is queen. Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    Two cards are drawn from a pack of playing cards, one after the other. Find the probability of getting a heart ... (b) with replacement Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    From a pack of 52 playing cards Jacks, queens, kings and aces of red colour are removed. From the remaining, ... are picture cards) Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
...