Audaspace: Load CoreAudio device on demand #107607

Closed
Richard Antalik wants to merge 7 commits from iss/blender:core-audio-on-demand into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.

Refactor CoreAudioDevice code to support on demand device opening and
closing when sound is not playing.

Currently device is opened when AUD_init() is called and then closed
immediately. This is because it seems, that this is causing issues with
synchronizer and causes AV sync to fail.

Anothe issue seems to be usage of audio scrubbing. This seems to confuse the device and it fails to play audio at all after some time. For WASAPI this was also problematic and a solution could be to create a timer in new thread, that would delay closing of the device.

Refactor CoreAudioDevice code to support on demand device opening and closing when sound is not playing. Currently device is opened when AUD_init() is called and then closed immediately. This is because it seems, that this is causing issues with synchronizer and causes AV sync to fail. Anothe issue seems to be usage of audio scrubbing. This seems to confuse the device and it fails to play audio at all after some time. For WASAPI this was also problematic and a solution could be to create a timer in new thread, that would delay closing of the device.
Richard Antalik added 1 commit 2023-05-04 10:09:47 +02:00
buildbot/vexp-code-patch-coordinator Build done. Details
0b834f3573
Audaspace: Load CoreAudio device on demand
Refactor CoreAudioDevice code to support on demand device opening and
closing when sound is not playing.

Currently device is opened when AUD_init() is called and then closed
immediately. This is because it seems, that this is causing issues with
synchronizer and causes AV sync to fail.
Author
Member

@blender-bot package

@blender-bot package
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR107607) when ready.
Joerg Mueller requested changes 2023-05-04 16:47:35 +02:00
Joerg Mueller left a comment
Member

Please follow the code style of audaspace and do not apply Blender's clang format in this file. This leads to way too many changes in this file. Audaspace has it's own clang format changes, but you should only format actually changed code.

I'd also name the methods simply open and close, the Device is superfluous and the open function is not on demand, it just opens.

Apart from that, the changes look good as far as I can tell. Cannot try it, since I lack the hardware though.

Please follow the code style of audaspace and do not apply Blender's clang format in this file. This leads to way too many changes in this file. Audaspace has it's own clang format changes, but you should only format actually changed code. I'd also name the methods simply `open` and `close`, the Device is superfluous and the open function is not on demand, it just opens. Apart from that, the changes look good as far as I can tell. Cannot try it, since I lack the hardware though.
Richard Antalik added 1 commit 2023-06-20 20:42:29 +02:00
Author
Member

@neXyon Hi, this should be ready to review,.Can you have a look?

@neXyon Hi, this should be ready to review,.Can you have a look?
Member

Please fix the formatting first as I mentioned before, otherwise it's hard to read the diff.

Please fix the formatting first as I mentioned before, otherwise it's hard to read the diff.
Author
Member

@neXyon Sorry I was pretty sure I did that, but I was wrong. Would you in future accept plain cleanup patch to format the file? Otherwise I have to undo unintended changes manually which is bit tedious.

@neXyon Sorry I was pretty sure I did that, but I was wrong. Would you in future accept plain cleanup patch to format the file? Otherwise I have to undo unintended changes manually which is bit tedious.
Member

Not sure what you mean? Do you mean running clang-format as auto-formatter? Clang format can also be run on just the changes in a diff which would be ideal here. So, can you apply the cleanup patch here already?

Not sure what you mean? Do you mean running clang-format as auto-formatter? Clang format can also be run on just the changes in a diff which would be ideal here. So, can you apply the cleanup patch here already?
Author
Member

Not sure what you mean?

I meant to make the files conform to format rules before I make any changes to files. I don't have IDE setup to work on Audaspace code, and MSVC is ignoring clang format currently anyway. Not sure if I broke the addon somehow. It's just in Blender we like to keep our code formatted at all times, so I thought about doing same thing in this case.

So, can you apply the cleanup patch here already?

OMG I forgot to commit changes before pushing :D

> Not sure what you mean? I meant to make the files conform to format rules before I make any changes to files. I don't have IDE setup to work on Audaspace code, and MSVC is ignoring clang format currently anyway. Not sure if I broke the addon somehow. It's just in Blender we like to keep our code formatted at all times, so I thought about doing same thing in this case. > So, can you apply the cleanup patch here already? OMG I forgot to commit changes before pushing :D
Joerg Mueller reviewed 2023-09-23 16:02:46 +02:00
Joerg Mueller left a comment
Member

Overall changes look good now! I've added some comments inline of what I'd like to see changed still and additionally:

The threaded functionality is very similar to what ThreadedDevice is doing so it is a bit of code duplication if I did not miss anything and that code can be reused (m_device_opened and m_delayed_close_finished correspond to m_playing, m_stop in some sense and m_delayed_close_thread corresponds to m_thread ofc). Additionally, the automatic opening and closing could be used by other backends as well. Therefore, please create a class OpenCloseDevice or so, derived from ThreadedDevice that adds abstract/purely virtual methods for open/close and does all the functionality that can be shared, i.e., implementing ThreadedDevice's runMixingThread with what closeAfterDelay does.

Overall changes look good now! I've added some comments inline of what I'd like to see changed still and additionally: The threaded functionality is very similar to what `ThreadedDevice` is doing so it is a bit of code duplication if I did not miss anything and that code can be reused (`m_device_opened` and `m_delayed_close_finished` correspond to `m_playing`, `m_stop` in some sense and `m_delayed_close_thread` corresponds to `m_thread` ofc). Additionally, the automatic opening and closing could be used by other backends as well. Therefore, please create a class `OpenCloseDevice` or so, derived from `ThreadedDevice` that adds abstract/purely virtual methods for `open`/`close` and does all the functionality that can be shared, i.e., implementing `ThreadedDevice`'s `runMixingThread` with what `closeAfterDelay` does.
@ -170,0 +196,4 @@
{
for(;;)
{
std::this_thread::sleep_for(std::chrono::microseconds(5));
Member

I dislike the hardcoding of this value, especially since it reappears just a few lines below again. Maybe you could turn this into a proper variable and even base it on some other value that makes sense (see e.g. sleep_duration in WASAPIDevice.cpp:95).

I dislike the hardcoding of this value, especially since it reappears just a few lines below again. Maybe you could turn this into a proper variable and even base it on some other value that makes sense (see e.g. `sleep_duration` in `WASAPIDevice.cpp:95`).
Author
Member

Sure, makes sense to use variables here, but not sure if I can base this on something else. My goal was to have multiple seconds until device is closed. If these delays would not be constant, some users may have issues and some not, or they would have trouble providing reproducible case.

Sure, makes sense to use variables here, but not sure if I can base this on something else. My goal was to have multiple seconds until device is closed. If these delays would not be constant, some users may have issues and some not, or they would have trouble providing reproducible case.
@ -170,0 +198,4 @@
{
std::this_thread::sleep_for(std::chrono::microseconds(5));
if(m_playback || m_playback_stopped_time == 0)
time(&m_playback_stopped_time);
Member

Please don't mix std::chrono code with ctime code, just use std::chrono everywhere!

Please don't mix `std::chrono` code with `ctime` code, just use `std::chrono` everywhere!
@ -29,7 +29,9 @@
#include "CoreAudioSynchronizer.h"
#include "devices/SoftwareDevice.h"
#include <ctime>
Member

Please use chrono instead of ctime.

Please use `chrono` instead of `ctime`.

@iss what is the status of this patch? Can this be wrapped up?

@iss what is the status of this patch? Can this be wrapped up?
Author
Member

@fsiddi Will prioritize this. I focused most of my efforts on retiming and bugfixing, so did revisit this when I got time to spare.

@fsiddi Will prioritize this. I focused most of my efforts on retiming and bugfixing, so did revisit this when I got time to spare.
Author
Member

Overall changes look good now! I've added some comments inline of what I'd like to see changed still and additionally:

The threaded functionality is very similar to what ThreadedDevice is doing so it is a bit of code duplication if I did not miss anything and that code can be reused (m_device_opened and m_delayed_close_finished correspond to m_playing, m_stop in some sense and m_delayed_close_thread corresponds to m_thread ofc). Additionally, the automatic opening and closing could be used by other backends as well. Therefore, please create a class OpenCloseDevice or so, derived from ThreadedDevice that adds abstract/purely virtual methods for open/close and does all the functionality that can be shared, i.e., implementing ThreadedDevice's runMixingThread with what closeAfterDelay does.

Sorry for delay, I have allocated some time to finish this now. Also sorry if I am plain wrong here, I have still limited understanding of cpp abstraction in this case.

OpenCloseDevice would have to have also start and stop virtual functions, since it needs to use playing function to control when the device should be opened and closed.
Then looking at ThreadedDevice this also implements playing function, but it would have to be overridden, which I can do. In that case, I would be able to reuse ThreadedDevice::start() function, but to me it seems I would have to do quite a bit of gymnastics to make it work. To me this solution looks highly confusing, and I would propose to derive from SoftwareDevice class. The threading that is used is super simple anyway.

If I am wrong, I would still probably do that as I think it is better starting point for code optimization.

> Overall changes look good now! I've added some comments inline of what I'd like to see changed still and additionally: > > The threaded functionality is very similar to what `ThreadedDevice` is doing so it is a bit of code duplication if I did not miss anything and that code can be reused (`m_device_opened` and `m_delayed_close_finished` correspond to `m_playing`, `m_stop` in some sense and `m_delayed_close_thread` corresponds to `m_thread` ofc). Additionally, the automatic opening and closing could be used by other backends as well. Therefore, please create a class `OpenCloseDevice` or so, derived from `ThreadedDevice` that adds abstract/purely virtual methods for `open`/`close` and does all the functionality that can be shared, i.e., implementing `ThreadedDevice`'s `runMixingThread` with what `closeAfterDelay` does. Sorry for delay, I have allocated some time to finish this now. Also sorry if I am plain wrong here, I have still limited understanding of cpp abstraction in this case. `OpenCloseDevice` would have to have also `start` and `stop` virtual functions, since it needs to use `playing` function to control when the device should be opened and closed. Then looking at `ThreadedDevice` this also implements playing function, but it would have to be overridden, which I can do. In that case, I would be able to reuse `ThreadedDevice::start()` function, but to me it seems I would have to do quite a bit of gymnastics to make it work. To me this solution looks highly confusing, and I would propose to derive from `SoftwareDevice` class. The threading that is used is super simple anyway. If I am wrong, I would still probably do that as I think it is better starting point for code optimization.
Richard Antalik force-pushed core-audio-on-demand from b5b5b95fda to 8f65025a11 2024-01-18 21:53:46 +01:00 Compare
Richard Antalik added 1 commit 2024-01-19 18:00:23 +01:00
Richard Antalik added 2 commits 2024-01-19 18:13:07 +01:00
Richard Antalik requested review from Joerg Mueller 2024-01-19 18:14:11 +01:00
Author
Member

@neXyon Do you have policy on how copyright blocks should be updated?

I have thought a bit more about implications of previous message, and currently I think there would be some issues if you needed a class derived from both OpenCloseDevice and ThreadedDevice

@neXyon Do you have policy on how copyright blocks should be updated? I have thought a bit more about implications of previous message, and currently I think there would be some issues if you needed a class derived from both `OpenCloseDevice` and `ThreadedDevice`
Member

Sorry for taking some time to respond, I'm as busy as usual. The patch looks good and you're right, the code probably doesn't get much better if you derive from ThreadedDevice. I've made minor fixes (attached), please add those and then I think this is ready to merge. It would be great if you could also open a PR for this at upstream audaspace (https://github.com/audaspace/audaspace/) as mentioned before. And no I don't have a policy for the copyright blocks. You can put your name and current dates if you wish :)

Sorry for taking some time to respond, I'm as busy as usual. The patch looks good and you're right, the code probably doesn't get much better if you derive from `ThreadedDevice`. I've made minor fixes (attached), please add those and then I think this is ready to merge. It would be great if you could also open a PR for this at upstream audaspace (https://github.com/audaspace/audaspace/) as mentioned before. And no I don't have a policy for the copyright blocks. You can put your name and current dates if you wish :)
Author
Member

Thanks for checking, will apply your changes and post PR in audaspace repo. As far as copyright goes, I will then just update year. I don't believe in intellectual property anyway :D

Thanks for checking, will apply your changes and post PR in audaspace repo. As far as copyright goes, I will then just update year. I don't believe in intellectual property anyway :D
Author
Member

Created PR for upstream, closign this one.

Created PR for upstream, closign this one.
Richard Antalik closed this pull request 2024-03-03 21:46:55 +01:00

Pull request closed

Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#107607
No description provided.