RE: No sound but buffer is playing????
RE: No sound but buffer is playing????
- Subject: RE: No sound but buffer is playing????
- From: Waverly Edwards <email@hidden>
- Date: Thu, 6 Jul 2017 12:32:13 +0000
- Thread-topic: No sound but buffer is playing????
If you wouldn't mind, would someone offer to confirm whether the behavior of
not hearing a tone exists outside of my own machine.
After I execute _player.play(), I've used print( _player.isPlaying) and the
result is true, so AVAudioEngine, says it is playing but nothing is heard.
Only when I execute the workaround code, do I hear anything.
At this point, just knowing whether a tone is heard is beneficial, after only
instantiating the class. I am just stumped at what I could be overlooking. If
you hear a tone, maybe I have an issue in my environment.
I'm on macOS Sierra 10.12.5, Xcode 8.3.1
let playSine = SinePlayer() // should hear tone after only
instantiating the class
// playSine.executeTimerCode(index: 1)
// playSine.executeTimerCode(index: 2)
// playSine.executeTimerCode(index: 3)
Thank you,
W.
From: Coreaudio-api
[mailto:coreaudio-api-bounces+wedwards=email@hidden] On Behalf Of
Waverly Edwards
Sent: Tuesday, July 04, 2017 2:11 PM
To: email@hidden
Subject: No sound but buffer is playing????
I've encountered a very odd thing. I created a class in which I play content
of an audio buffer but that buffer can *only* be heard if I execute some timer
code. The code is supposed to play a continuous tone but that tone is only
heard if I execute with an objc method and once that method has been executed
sound is no longer heard but it should be continuous.
Note, I dont want the timer code at all. I only created the timer code after
pondering a technote about blocking the main queue would cause avaudioengine to
not allocate resources, however I have not done that.
I cleaned the project, deleted xcodes derived code, restarted xcode, restarted
my machine but the behavior continues which leads me to think I've done
something incorrectly but I am not seeing what that is.
This is test code. My actual code is suffering the same way and this example
was the simplest code I could come up with to reproduce the problem and
potential workaround.
Any ideas on what I could be doing incorrectly and how to resolve this issue?
W.
import Foundation
import AVFoundation
class SinePlayer{
var _engine:AVAudioEngine
var _player:AVAudioPlayerNode
var _buffer: AVAudioPCMBuffer
@objc func myPerformeCode()
{
print("We executed myPerformeCode()")
}
// we dont want to use executeTimerCode at all, it just demonstrates a
workaround
func executeTimerCode(index: Int)
{
let kTimeInSeconds = 5.0
switch index {
case 1 :
_ = Timer.scheduledTimer(timeInterval: kTimeInSeconds, target:
self, selector: #selector(myPerformeCode), userInfo: nil, repeats: false)
print( "Using Timer.scheduledTimer -- produces sound.")
case 2 :
DispatchQueue.main.asyncAfter(deadline: .now() + kTimeInSeconds) {
self.myPerformeCode()
}
print( "Using DispatchQueue.main.asyncAfter, calling objc method --
produces sound.")
case 3 :
DispatchQueue.main.asyncAfter(deadline: .now() + kTimeInSeconds) {
print( "Only a print statment in dispatch queue.")
}
print( "Using DispatchQueue.main.asyncAfter objc method not called
-- no sound output")
default :
print( "Invalid value: Index value 1,2 or 3 are the only valid
values.")
}
}
init(){
_engine = AVAudioEngine()
_player = AVAudioPlayerNode()
_buffer = AVAudioPCMBuffer(pcmFormat: _player.outputFormat(forBus: 0),
frameCapacity: 100)
_buffer.frameLength = 100
// generate sine wave
let sampleRate = Float(_engine.mainMixerNode.outputFormat(forBus:
0).sampleRate)
let numChannels = _engine.mainMixerNode.outputFormat(forBus:
0).channelCount
for i in stride(from: 0, to: Int(_buffer.frameLength), by:
Int(numChannels)) {
let val = sinf(441.0*Float(i)*2*Float(Double.pi)/sampleRate)
_buffer.floatChannelData?.pointee[i] = val * 0.5
}
_engine.attach(_player)
_engine.connect(_player, to: _engine.mainMixerNode, format: nil)
_engine.connect(_engine.mainMixerNode, to: _engine.outputNode, format:
nil)
do{
try _engine.start()
print("started")
} catch let error as NSError {
print("Error start:\(error)")
}
// _player.scheduleBuffer(_buffer, at: nil, options: [],
completionHandler: nil)
_player.scheduleBuffer(_buffer, at: nil, options: .loops,
completionHandler: nil)
_player.play()
}
}
let playSine = SinePlayer() // should hear tone after only
instantiating the class
playSine.executeTimerCode(index: 1)
// playSine.executeTimerCode(index: 2)
// playSine.executeTimerCode(index: 3)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Coreaudio-api mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden