Package: pya
pyaBranchmasterdevelopCI-Linux/MacOSCI-WindowsChangesBinderWhat is pya?pya is a package to support creation and manipulation of audio signals with Python.
It uses numpy arrays to store and compute audio signals.Documentation: see examples/pya-examples.ipynb for a quick tutorial andDocumentationSource code:https://github.com/interactive-sonification/pyaIt provides:Asig - a versatile audio signal classUgen - a subclass of Asig, which offers unit generators
such as sine, square, sawtooth, noiseAserver - an audio server class for queuing and playing AsigsArecorder - an audio recorder classAspec - an audio spectrum class, using rfft as real-valued signals are always impliedAstft - an audio STFT (short-term Fourier transform) classA number of helper functions, e.g. device_info()pya can be used formulti-channel audio processingauditory display and sonificationsound synthesis experimentaudio applications in general such as games or GUI-enhancementssignal analysis and plottingAt this time pya is more suitable for offline rendering than realtime.Authors and ContributorsThomas(author, maintainer)Jiajun(co-author, maintainer)Alexander(maintainer)Contributors will be acknowledged here, contributions are welcome.InstallationInstall usingpip install pyaHowever to play and record audio you need a backend.pip install pya[remote]for a web based Jupyter backendpip install pya[pyaudio]forportaudioand its Python wrapperPyAudioUsing CondaPyaudio can be installed viaconda:conda install pyaudioDisclaimer: Python 3.10+ requires PyAudio 0.2.12 which is not available on Conda as of December 2022.Conda-forgeprovides a version only for Linux at the moment. Users of Python 3.10 should for now use other installation options.Using Homebrew and PIP (MacOS only)brew install portaudioThenpip install pyaFor Apple ARM Chip, if you failed to install the PyAudio dependency, you can follow this guide:Installation on ARM chipOption 1: Create .pydistutils.cfg in your home directory,~/.pydistutils.cfg, add:echo "[build_ext]
include_dirs=$(brew --prefix portaudio)/include/
library_dirs=$(brew --prefix portaudio)/lib/" > ~/.pydistutils.cfgUse pip:pip install pyaYou can remove the.pydistutils.cfgfile after installation.Option 2: UseCFLAGS:CFLAGS="-I/opt/homebrew/include -L/opt/homebrew/lib" pip install pyaUsing PIP (Linux)Trysudo apt-get install portaudio19-devor equivalent to your distro, thenpip install pyaUsing PIP (Windows)PyPIprovidesPyAudio wheelsfor Windows including portaudio:pip install pyaudioshould be sufficient.A simple exampleStartup:importpyas=pya.Aserver(bs=1024)pya.Aserver.default=s# to set as default servers.boot()Create an Asig signal:A 1s / 440 Hz sine tone at sampling rate 44100 as channel name 'left':importnumpyasnpsignal_array=np.sin(2*np.pi*440*np.linspace(0,1,44100))atone=pya.Asig(signal_array,sr=44100,label='1s sine tone',cn=['left'])Other ways of creating an Asig object:asig_int=pya.Asig(44100,sr=44100)# zero array with 44100 samplesasig_float=pya.Asig(2.,sr=44100)# float argument, 2 seconds of zero arrayasig_str=pya.Asig('./song.wav')# load audio fileasig_ugen=pya.Ugen().square(freq=440,sr=44100,dur=2.,amp=0.5)# using Ugen class to create common waveformsAudio files are also possible using the file path.WAVshould work without issues.MP3is supported but may raise error ifFFmpeg.If you use Anaconda, installation is quite easy:conda install -c conda-forge ffmpegOtherwise:Mac or Linux with brewbrew install ffmpegOn LinuxInstall FFmpeg via apt-get:sudo apt install ffmpegOn WindowsDownload the latest distribution fromhttps://ffmpeg.zeranoe.com/builds/Unzip the folder, preferably toC:\Append the FFmpeg binary folder (e.g.C:\ffmpeg\bin) to the PATH system variable (How do I set or change the PATH system variable?)Key attributesatone.sig--> The numpy array containing the signal isatone.sr--> the sampling rateatone.cn--> the list of custom defined channel namesatone.label--> a custom set identifier stringPlay signalsatone.play(server=s)play() uses Aserver.default if server is not specifiedInstead of specifying a long standing server. You can also useAserveras a context:withpya.Aserver(sr=48000,bs=256,channels=2)asaserver:atone.play(server=aserver)# Or do: aserver.play(atone)The benefit of this is that it will handle server bootup and shutdown for you. But notice that server up/down introduces extra latency.Play signal on a specific devicefrompyaimportfind_devicefrompyaimportAserverdevices=find_device()# This will return a dictionary of all devices, with their index, name, channels.s=Aserver(sr=48000,bs=256,device=devices['name_of_your_device']['index'])Plotting signalsto plot the first 1000 samples:atone[:1000].plot()to plot the magnitude and phase spectrum:atone.plot_spectrum()to plot the spectrum via the Aspec classatone.to_spec().plot()to plot the spectrogram via the Astft classatone.to_stft().plot(ampdb)Selection of subsetsAsigs support multi-channel audio (as columns of the signal array)a1[:100, :3]would select the first 100 samples and the first 3 channels,a1[{1.2:2}, ['left']]would select the channel named 'left' using a time slice from 1Recording from DeviceArecorderallows recording from input deviceimporttimefrompyaimportfind_devicefrompyaimportArecorderdevices=find_device()# Find the index of the input devicearecorder=Arecorder(device=some_index,sr=48000,bs=512)# Or not set device to let pya find the default devicearecorder.boot()arecorder.record()time.sleep(2)# Recording is non-blockingarecorder.stop()last_recording=arecorder.recordings[-1]# Each time a recorder stop, a new recording is appended to recordingsMethod chainingAsig methods usually return an Asig, so methods can be chained, e.gatone[{0:1.5}].fade_in(0.1).fade_out(0.8).gain(db=-6).plot(lw=0.1).play(rate=0.4, onset=1)Learning morePlease check the examples/pya-examples.ipynb for more examples and details.ContributingPlease get in touch with us if you wish to contribute. We are happy to be involved in the discussion of new features and to receive pull requests.