Post

Replies

Boosts

Views

Activity

Get audio volume from microphone
Hello. We are trying to get audio volume from microphone. We have 2 questions. 1. Can anyone tell me about AVAudioEngine.InputNode.volume? AVAudioEngine.InputNode.volume Return 0 in the silence, Return float type value within 1.0 depending on the volume are expected work, but it looks 1.0 (default value) is returned at any time. Which case does it return 0.5 or 0? Sample code is below. Microphone works correctly. // instance member private var engine: AVAudioEngine! private var node: AVAudioInputNode! // start method self.engine = .init() self.node = engine.inputNode engine.prepare() try! engine.start() // volume getter print(\(self.node.volume)) 2. What is the best practice to get audio volume from microphone? Requirements are: Without AVAudioRecorder. We use it for streaming audio. it should withstand high frequency access. Testing info device: iPhone XR OS version: iOS 18 Best Regards.
2
0
304
Oct ’24
deadlock thread
Hi. We have weird trouble related to the multi-threading. Does anyone have ideas how to resolve or avoid? Sample Code // sample.hpp class Sample { public: Sample(); ~Sample(); void Init(); void Dealloc(); void OnEvent(); private: std::mutex sample_mutex_; int counter = 1; } // sample.cpp #include "sample.hpp" Sample::Sample() {} Sample::~Sample() {} void Sample::Init() { std::async([]() { std::this_thread::sleep_for(std::chrono::seconds(2)); this->OnEvent(); }); } void Sample::Dealloc() { std::lock_guard<std::mutex> lock(sample_mutex_); counter = 0; } void Sample::OnEvent { // called from another thread std::lock_guard<std::mutex> lock(sample_mutex_); counter += 1; } // obj_sample.h @interface ObjSample : NSObject - (id _Nonnull)init; @property(nonatomic, readonly) std::shared_ptr<Sample> sample; @end // obj_sample.mm @implementation ObjSample - (id _Nonnull)init{ if (self = [super init]) { _sample = std::make_shared<Sample>(); _sample->Init(); } return self; } - (void)dealloc { sample->Dealloc(); } @end What happens the deadlock happens. according to the debug navigator with the Xcode, we figured out 2 facts below. OnEvent does not end although it looks completed. void Sample::OnEvent { // called from another thread std::lock_guard<std::mutex> lock(sample_mutex_); counter += 1; } // <= Thread 35: stop here Sample::Dealloc is run on the same thread of OnEvent. // debug navigator Thread 35 2 Sample::Dealloc <- this is weird. 3 Sample::OnEvent We guess they causes the deadlock. probability less than 10% Environment MacOS: 14.3.1(Apple M1) Xcode: 15.3 iOS Simulator: 17.0.1
3
0
649
Apr ’24