Implementing libPD (Pure Data wrapper) in Python

I created a simple text output from the game into a room in Python with the intention of introducing a Pure Data patch (via libPd) in order to play another sound file (this will later be replaced by an algorithm for generative music) for each of my rooms.

The python code I'm currently working with was taken from one of the examples in libput github. It looks like this:

import pyaudio import wave import sys from pylibpd import * p = pyaudio.PyAudio() ch = 2 sr = 48000 tpb = 16 bs = 64 stream = p.open(format = pyaudio.paInt16, channels = ch, rate = sr, input = True, output = True, frames_per_buffer = bs * tpb) m = PdManager(ch, ch, sr, 1) libpd_open_patch('wavfile.pd') while 1: data = stream.read(bs) outp = m.process(data) stream.write(outp) stream.close() p.terminate() libpd_release() 

A clean data patch simply reproduces a pre-rendered wav file, however the result sounds almost as if it had been corrupted. I assume the problem is with the block size, but I'm not sure.

If anyone has experience implementing lidPD in Python, I would be very grateful because I am sure that what I am trying to achieve is confusing simply.

Thanks in advance, Cap

+6
source share
3 answers

In the end, I used a workaround and imported pygame (unlike pyaudio) to process the sound and initialize the patch. It works without a glitch.

Thank you for your help.

* For those who encounter a similar problem, check out "pygame_test.py" in gythub libPd for python.

+3
source

I had similar problems. Using callback fixed it for me.

Here is a python to play a sine wave.

     import pyaudio
     from pylibpd import *
     import time

     def callback (in_data, frame_count, time_info, status):
         outp = m.process (data)
         return (outp, pyaudio.paContinue)

     p = pyaudio.PyAudio ()
     bs = libpd_blocksize ()

     stream = p.open (format = pyaudio.paInt16,
                     channels = 1,
                     rate = 44100,
                     input = False,
                     output = True,
                     frames_per_buffer = bs,
                     stream_callback = callback)

     m = PdManager (1, 1, 44100, 1)

     libpd_open_patch ('sine.pd')

     data = array.array ('B', [0] * bs)

     while stream.is_active ():
         time.sleep (.1)

     stream.close ()
     p.terminate ()
     libpd_release ()

and the patch "sine.pd"

     #N canvas 647 301 450 300 10;
     #X obj 67 211 dac ~;
     #X obj 24 126 osc ~ 1000;
     #X obj 16 181 * ~ 0.2;
     #X connect 1 0 2 0;
     #X connect 2 0 0 0;

+2
source

There are several parts to this.

  • The block size of the audio file is incorrect because you set tpb = 16 instead of 1. By setting it to 16, you make the block size 16 * 64 instead of 64.

  • There may be a problem with sample rates. Are you sure your sound file is 48000 Hz and not 44100 Hz?

0
source

Source: https://habr.com/ru/post/948643/


All Articles