enzyme69 / blendersushi

Blender Sushi related scripts. Mostly about Sverchok, Geometry Nodes, Animation Nodes, and related Python scripts.
243 stars 33 forks source link

LIVENODING 985 / Realtime Audio Processing in Blender ANSV #361

Open enzyme69 opened 6 years ago

enzyme69 commented 6 years ago

pyaudio_realtime_014_2018_03_18_00_37.zip

For this to work, you need a couple of dependency taken care of:

enzyme69 commented 6 years ago
screen shot 2018-03-18 at 11 45 41 am
enzyme69 commented 6 years ago
### THIS SPIT OUT DATA FOR SVERCHOK
import sys

#mcubes_path = r"/usr/local/lib/python3.5/dist-packages" #it depend on your OS but just paste the path where is mcubes
pyaudio_path = r"/Users/jimmygunawan/anaconda3/lib/python3.5/site-packages"

if not pyaudio_path in sys.path:
    sys.path.append(pyaudio_path)    

import pyaudio
import numpy

print("#" * 50)

RATE=16000
RECORD_SECONDS = 5
CHUNKSIZE = 1024

# initialize portaudio
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNKSIZE)

frames = [] # A python-list of chunks(numpy.ndarray)
for _ in range(0, int(RATE / CHUNKSIZE * RECORD_SECONDS)):
    data = stream.read(CHUNKSIZE)
    frames.append(numpy.fromstring(data, dtype=numpy.int16))

#Convert the list of numpy-arrays into a 1D array (column-wise)
numpydata = numpy.hstack(frames)

# close stream
stream.stop_stream()
stream.close()
p.terminate()

for num,i in enumerate(numpydata):
    print(num,i)

#import scipy.io.wavefile as wav
#wav.write('out.wav',RATE,numpydata)
enzyme69 commented 6 years ago
### THIS IS FOR AN SCRIPT NODE TO OUTPUT VALUE TO DRIVE ANIMATION

import sys

#mcubes_path = r"/usr/local/lib/python3.5/dist-packages" #it depend on your OS but just paste the path where is mcubes
pyaudio_path = r"/Users/jimmygunawan/anaconda3/lib/python3.5/site-packages"

if not pyaudio_path in sys.path:
    sys.path.append(pyaudio_path)    

import pyaudio
import numpy as np

CHUNK = 2**11
RATE = 44100

p=pyaudio.PyAudio()
stream=p.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,
              frames_per_buffer=CHUNK)

data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
peak=np.average(np.abs(data))*2
bars="#"*int(50*peak/2**16)
print("%05d %s"%(peak,bars))

OUTVALUE = peak * 0.01

bpy.ops.anim.keyframe_insert()

#stream.stop_stream()
#stream.close()
#p.terminate()
enzyme69 commented 6 years ago

You need PyAudio module: http://people.csail.mit.edu/hubert/pyaudio/

enzyme69 commented 6 years ago

This is Python script that I borrowed: https://www.swharden.com/wp/2016-07-19-realtime-audio-visualization-in-python/

enzyme69 commented 6 years ago

Perhaps speech recognition is a fun thing to try also: https://pypi.python.org/pypi/SpeechRecognition/3.4.5

LuZa1976 commented 4 years ago

Cannot use pyaudio... Console message from Blender: Could not import the PyAudio C module '_portaudio'.

I've some problem using the system python in Blender, cause in Spider i can run the script and see the values, but inside Blender no.

I installed Portaudio via conda, even if i didn't needed in system cause everything works. I understand that in Blender i can use external installation of Python but seems not to work

Blender 2.90, blender python 3.7.4 system Blender python 3.7.6 Anaconda

Any suggestion ? Thanks

enzyme69 commented 4 years ago

Try brew install portaudio before installing pyaudio. https://people.csail.mit.edu/hubert/pyaudio/

I tested it just now with Blender 2.83 and copy paste PyAudio into Blender site-package and it seems to work for me.

msurguy commented 3 years ago

@enzyme69 how could I read data from .wav or .mp3 file and pass that data to Sverchok easily these days? For example I want to visualize amplitude of the whole recording as opposed to dynamic changes on each keyframe... Any pointers would be appreciated!