-
Notifications
You must be signed in to change notification settings - Fork 7
audio logging and diagnostics with structlog #47
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
Open
jquast
wants to merge
8
commits into
vxgmichel:main
Choose a base branch
from
jquast:jq/audio-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
68a347d
audio: diagnostics and prevent some underruns
jquast aa702d6
Defensive max(0.0, ...) guard in fill_fraction
jquast 8e675ae
use standard logging interface, only
jquast 93b811a
mypy ..
jquast c6a92c4
upgrade mypy for its false problems
jquast c93b595
don't double log, per-frame stats go to CSV, only
jquast 98f1e72
remove batching and use structlog
jquast a1bf4ce
local CLI defaults to 'critical' log level
jquast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,10 @@ | |
| from __future__ import annotations | ||
|
|
||
| import time | ||
| import logging | ||
| import argparse | ||
| from pathlib import Path | ||
| from typing import ContextManager, TYPE_CHECKING | ||
| from typing import Any, ContextManager, Optional, TYPE_CHECKING | ||
| import dataclasses | ||
| from dataclasses import dataclass, field | ||
|
|
||
|
|
@@ -23,6 +24,45 @@ | |
| if TYPE_CHECKING: | ||
| from typing import Self | ||
|
|
||
| _DEFAULT_LOGFMT = " ".join(("%(levelname)s", "%(filename)s:%(lineno)d", "%(message)s")) | ||
|
|
||
|
|
||
| def make_logger( | ||
| name: str, | ||
| loglevel: str = "info", | ||
| logfile: Optional[str] = None, | ||
| logfmt: str = _DEFAULT_LOGFMT, | ||
| filemode: str = "a", | ||
| ) -> logging.Logger: | ||
| """Create and return a configured logger (following telnetlib3 pattern).""" | ||
| lvl = getattr(logging, loglevel.upper(), None) | ||
| if lvl is None: | ||
| lvl = logging.getLevelName(loglevel.upper()) | ||
| _cfg: dict[str, Any] = {"format": logfmt} | ||
| if logfile: | ||
| _cfg["filename"] = logfile | ||
| _cfg["filemode"] = filemode | ||
| logging.basicConfig(**_cfg) | ||
| logging.getLogger().setLevel(lvl) | ||
| logging.getLogger(name).setLevel(lvl) | ||
| return logging.getLogger(name) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh right, I will wire it there, I will have to look at |
||
|
|
||
|
|
||
| def add_logging_arguments(parser: argparse.ArgumentParser) -> None: | ||
| parser.add_argument( | ||
| "--logfile", default=None, help="File path for log output (default: stderr)" | ||
| ) | ||
| parser.add_argument( | ||
| "--loglevel", | ||
| default="warn", | ||
| help="Logging level: debug, info, warn, error (default: warn)", | ||
| ) | ||
| parser.add_argument( | ||
| "--logfmt", | ||
| default=_DEFAULT_LOGFMT, | ||
| help="Log format string (default: LEVEL file:lineno message)", | ||
| ) | ||
|
|
||
|
|
||
| @dataclass | ||
| class AppConfig: | ||
|
|
@@ -151,11 +191,18 @@ def main( | |
| add_base_arguments(parser) | ||
| add_input_file_arguments(parser) | ||
| add_tuning_arguments(parser) | ||
| add_logging_arguments(parser) | ||
| add_local_only_arguments(parser) | ||
| console_cls.add_console_arguments(parser) | ||
|
|
||
| # Parse arguments | ||
| namespace = parser.parse_args(parser_args) | ||
| make_logger( | ||
| __name__, | ||
| loglevel=getattr(namespace, "loglevel", "warn"), | ||
| logfile=getattr(namespace, "logfile", None), | ||
| logfmt=getattr(namespace, "logfmt", _DEFAULT_LOGFMT), | ||
| ) | ||
| disable_audio = getattr(namespace, "disable_audio", False) | ||
| args = LocalAppConfig.from_namespace(namespace) | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I introduced the circular buffer, I had in mind that variable-length audio batches would not be a problem since I made sure that the core loop itself is synchronized with the number of produced audio samples:
gambatte-terminal/gambaterm/run.py
Lines 201 to 209 in f6fbf74
So in theory, the producer (i.e gambatte) and the consumer (miniaudio) should move at the exact same rate. The reason why they drift appart is because the producer uses the cpu clock, and the consumer the audio clock. So the job of the PI controller is to detect this slow drift and tune the resample rate to compensate for it. Since the drift is slow, monitoring a rolling average of the fill ratio is a good way to detect and adapt to the drift.
So as far as I can tell, tiny audio batches should not starve the ring buffer since they should be produced faster to compensate (unless the producing of the video frames at a faster rates slows down the core loop, although this should already be accounted for)
Do you think my analysis is correct? Did I miss something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can only induce a starved audio buffer by spending too much CPU in rendering, the text mode blitter doesn't consume enough CPU to show the effect. In any case, I will change the render logic there instead, from "always render at least one partial graphics frame", to "skip rendering this frame all together when fill_fraction is low, to allow subsequent frames to fill it back up again before I take too much CPU time rendering it"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I already implemented some logic to detect when the main loop can't keep up with the clock, and skip rendering+outputting the video frame if that's the case:
gambatte-terminal/gambaterm/run.py
Lines 207 to 209 in f6fbf74
gambatte-terminal/gambaterm/run.py
Lines 142 to 150 in f6fbf74
It's too bad that it's not enough to keep the audio circular buffer half-full. There are other solutions we can try in order to fix this:
shifting[-1] > 1 / fps / 2(or more)audio_delay_in_framesto 4 or 5, this should give the buffer more time to adapt to sporadic heavy video frames