Skip to content

Instantly share code, notes, and snippets.

@hecanjog
Last active September 20, 2018 00:25
Show Gist options
  • Select an option

  • Save hecanjog/b806cbb0190af0da516e0af2c8e6f5a3 to your computer and use it in GitHub Desktop.

Select an option

Save hecanjog/b806cbb0190af0da516e0af2c8e6f5a3 to your computer and use it in GitHub Desktop.
import io
import numpy
from pippi import dsp, fx
SAMPLERATE = 44100
CHANNELS = 2
# Reading bytes from an image into a soundbuffer
with open('test.jpg', 'rb') as f:
b = io.BytesIO(f.read())
v = b.getbuffer()
a = numpy.array(v).astype('d')
snd = dsp.buffer(a, channels=CHANNELS, samplerate=SAMPLERATE)
snd = fx.norm(snd, 0.5)
# Could write the sound back to a file at this point
snd.write('test.wav')
# Or -- pippi soundbuffers have a `frames` property which
# is a memoryview to the internal numpy array.
# To copy bytes from a pippi soundbuffer:
b = io.BytesIO(numpy.array(snd.frames).tobytes())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment