-
Notifications
You must be signed in to change notification settings - Fork 4
Added support for HART audio buffers #21
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ | |
| from .std_2D import * | ||
| from .juce import * | ||
| from .choc import * | ||
| from .hart import * | ||
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 |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from typing import Tuple | ||
|
|
||
| from ...container import SampleType, Container2D | ||
| from ...debuggers.value import AbstractValue | ||
| from dave.server.languages import c_cpp | ||
|
|
||
| class HartAudioBuffer(Container2D): | ||
| __REGEX = rf"^(?:const\s+)?hart::AudioBuffer<{SampleType.regex()}>\s*$" | ||
|
|
||
| def __init__(self, dbg_value: AbstractValue, name: str, _): | ||
| typename = dbg_value.typename() | ||
| sample_type, *_ = self._parse_typename(typename) | ||
| super().__init__(dbg_value, name, sample_type) | ||
|
|
||
| @property | ||
| def __inner(self) -> c_cpp.StdVector1D: | ||
| return c_cpp.StdVector1D(self._value.attr("m_frames"), self.name + ".m_frames") | ||
|
|
||
| @classmethod | ||
| def typename_matcher(cls) -> re.Pattern: | ||
| return re.compile(cls.__REGEX) | ||
|
|
||
| @classmethod | ||
| def _parse_typename(cls, typename: str, **_) -> Tuple[SampleType, None, None]: | ||
| re_match = cls.typename_matcher().match(typename) | ||
| if re_match is None: | ||
| raise TypeError( | ||
| f"HartAudioBuffer could not parse {typename} as a valid type" | ||
| ) | ||
|
|
||
| return (SampleType.parse(re_match.group(1)), None, None) | ||
|
|
||
| def shape(self) -> Tuple[int, int]: | ||
| assert isinstance(self._value, AbstractValue) | ||
| try: | ||
| return ( | ||
| int(self._value.attr("m_numChannels")), | ||
| int(self._value.attr("m_numFrames")) | ||
| ) | ||
| except: | ||
| raise RuntimeError(f"Failed to retrieve shape of {self._value.typename()}") | ||
|
|
||
| def read_from_debugger(self) -> bytearray: | ||
| return self.__inner.read_from_debugger() | ||
|
|
||
| HartAudioBuffer.register() | ||
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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #define HART_IMPLEMENTATION | ||
| #include "hart.hpp" | ||
|
|
||
| HART_DECLARE_ALIASES_FOR_FLOAT; | ||
| using AudioBuffer = hart::AudioBuffer<float>; | ||
| using hart::roundToSizeT; | ||
|
|
||
| int main() | ||
| { | ||
| constexpr double sampleRateHz = 44100.0; | ||
|
|
||
| // Sine sweep | ||
| const size_t sineSweepDurationFrames = roundToSizeT (sampleRateHz * 1.0_s); | ||
| AudioBuffer bufferA (1, sineSweepDurationFrames); | ||
| auto sineSweepSignalA = SineSweep(); | ||
| sineSweepSignalA.prepare ( | ||
| sampleRateHz, | ||
| 1, // numOutputChannels | ||
| sineSweepDurationFrames // maxBlockSizeFrames | ||
| ); | ||
| sineSweepSignalA.renderNextBlock (bufferA); | ||
|
|
||
| // A different sweep, overwrites the same buffer | ||
| auto sineSweepSignalB = SineSweep() | ||
| .withType (SineSweep::SweepType::linear) | ||
| .withStartFrequency (100_Hz) | ||
| .withEndFrequency (1_Hz) | ||
| .withDuration (500_ms) | ||
| .withLoop (SineSweep::Loop::yes); | ||
| sineSweepSignalB.prepare ( | ||
| sampleRateHz, | ||
| 1, // numOutputChannels | ||
| sineSweepDurationFrames // maxBlockSizeFrames | ||
| ); | ||
| sineSweepSignalB.renderNextBlock (bufferA); | ||
|
|
||
| // Multi-channel noise | ||
| constexpr size_t multiChannelNoiseNumChannels = 5; | ||
| const size_t multiChannelNoiseDurationFrames = hart::roundToSizeT (sampleRateHz * 10_ms); | ||
| AudioBuffer bufferB (multiChannelNoiseNumChannels, multiChannelNoiseDurationFrames); | ||
| auto multiChannelNoiseSignal = WhiteNoise(); | ||
| multiChannelNoiseSignal.prepare ( | ||
| sampleRateHz, | ||
| multiChannelNoiseNumChannels, // numOutputChannels | ||
| multiChannelNoiseDurationFrames // maxBlockSizeFrames | ||
| ); | ||
| multiChannelNoiseSignal.renderNextBlock (bufferB); | ||
| multiChannelNoiseSignal.renderNextBlock (bufferB); // Some more noise... | ||
| multiChannelNoiseSignal.renderNextBlock (bufferB); // ...and more noise | ||
|
|
||
| return 0; | ||
| } |
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.