Skip to content

Commit

Permalink
More work on numpy audio integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kunitoki committed Feb 7, 2024
1 parent a812dff commit b677c0a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
80 changes: 80 additions & 0 deletions examples/numpy_audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import numpy as np

from juce_init import START_JUCE_COMPONENT
import popsicle as juce


class AudioCallback(juce.AudioIODeviceCallback):
gain = 1.0
time = 0.0
device = None

def audioDeviceAboutToStart(self, device: juce.AudioIODevice):
print("starting", device, "at", device.getCurrentSampleRate())
self.device = device
self.buffer = np.zeros(self.device.getCurrentBufferSizeSamples())

def audioDeviceIOCallbackWithContext(self, inputs, numInputChannels, outputs, numOutputChannels, numSamples, context):
start = self.time
end = start + 2.0 * numSamples

self.buffer[:] = (
(np.random.random(numSamples) * 2.0 - 1 * 0.025) + np.sin(np.deg2rad(np.linspace(start, end, numSamples)))
) * self.gain * 0.005

self.time = end % 360.0

for output in outputs:
nout = np.array(output, copy=False)
nout[:] = self.buffer

def audioDeviceError(self, errorMessage: str):
print("error", errorMessage)

def audioDeviceStopped(self):
print("stopping")


class MainContentComponent(juce.Component):
manager = juce.AudioDeviceManager()
audio_callback = AudioCallback()

def __init__(self):
juce.Component.__init__(self)

width = 600
height = 400

self.manager.addAudioCallback(self.audio_callback)
result = self.manager.initialiseWithDefaultDevices(0, 2)
if result:
print(result)

self.button = juce.TextButton("Silence!")
self.addAndMakeVisible(self.button)
self.button.onStateChange = lambda: self.onButtonStateChange()

self.setSize(int(width), int(height))
self.setOpaque(True)

def visibilityChanged(self):
if not self.isVisible() and self.manager:
self.manager.removeAudioCallback(self.audio_callback)
self.manager.closeAudioDevice()

def onButtonStateChange(self):
if self.button.getState() == juce.Button.ButtonState.buttonDown:
self.audio_callback.gain = 0.25
else:
self.audio_callback.gain = 1.0

def paint(self, g: juce.Graphics):
g.fillAll(juce.Colours.black)

def resized(self):
bounds = self.getLocalBounds()
self.button.setBounds(bounds.reduced(100))


if __name__ == "__main__":
START_JUCE_COMPONENT(MainContentComponent, name="Audio Device Example")
18 changes: 2 additions & 16 deletions modules/juce_python/bindings/ScriptJuceAudioDevicesBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,7 @@ void registerJuceAudioDevicesBindings (pybind11::module_& m)
})
.def_buffer ([](PyChannelData<const float>& v) -> py::buffer_info
{
return py::buffer_info(
const_cast<void*> (static_cast<const void*> (v.data())),
sizeof (float),
py::format_descriptor<float>::format(),
1,
{ static_cast<ssize_t> (1), static_cast<ssize_t> (v.size()) },
{ static_cast<ssize_t> (sizeof(float) * v.size()), static_cast<ssize_t> (sizeof(float)) }
);
return py::buffer_info (v.data(), static_cast<ssize_t> (v.size()), true);
})
;

Expand All @@ -81,14 +74,7 @@ void registerJuceAudioDevicesBindings (pybind11::module_& m)
})
.def_buffer ([](PyChannelData<float>& v) -> py::buffer_info
{
return py::buffer_info(
static_cast<void*> (v.data()),
sizeof (float),
py::format_descriptor<float>::format(),
1,
{ static_cast<ssize_t> (1), static_cast<ssize_t> (v.size()) },
{ static_cast<ssize_t> (sizeof(float) * v.size()), static_cast<ssize_t> (sizeof(float)) }
);
return py::buffer_info (v.data(), static_cast<ssize_t> (v.size()), false);
})
;

Expand Down

0 comments on commit b677c0a

Please sign in to comment.