Skip to main content

Python audio coding classes - for dsp and sonification

Project description

PyPI License

pya

Branch master develop
CI-Linux/MacOS Build Status Travis Build Status Travis
CI-Windows Build status AppVeyor Build status AppVeyor
Changes GitHub commits GitHub commits
Binder Master Binder Develop Binder

What 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.

It provides:

  • Asig - a versatile audio signal class
    • Ugen - a subclass of Asig, which offers unit generators such as sine, square, swatooth, noise
  • Aserver - an audio server class for queuing and playing Asigs
  • Arecorder - an audio recorder class
  • Aspec - an audio spectrum class, using rfft as real-valued signals are always implied
  • Astft - an audio STFT (short-term Fourier transform) class
  • A number of helper functions, e.g. device_info()

pya can be used for

  • multi-channel audio processing
  • auditory display and sonification
  • sound synthesis experiment
  • audio applications in general such as games or GUI-enhancements
  • signal analysis and plotting

At this time pya is more suitable for offline rendering than realtime.

Authors and Contributors

  • Thomas Hermann, Ambient Intelligence Group, Faculty of Technology, Bielefeld University (author and maintainer)
  • Jiajun Yang, Ambient Intelligence Group, Faculty of Technology, Bielefeld University (co-author)
  • Alexander Neumann, Neurocognitions and Action - Biomechanics, Bielefeld University
  • Contributors will be acknowledged here, contributions are welcome.

Installation

Note: pya can be installed using pip. But pya uses PyAudio for audio playback and record, and PyAudio 0.2.11 has yet to fully support Python 3.7. So using pip install with Python 3.7 may encounter issues such as portaudio. Solutions are:

  1. Anaconda can install non-python packages, so that the easiest way (if applicable) would be to

    conda install pyaudio

  2. For Mac users, you can brew install portaudio beforehand.

  3. For Linux users, try sudo apt-get install portaudio19-dev or equivalent to your distro.

  4. For Windows users, you can install PyAudio wheel at: https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio

Then pya can be installed using pip:

pip install pya

See pyaudio installation http://people.csail.mit.edu/hubert/pyaudio/#downloads

A simple example

Startup:

from pya import *
s = Aserver(bs=1024)
Aserver.default = s  # to set as default server
s.boot()   

Create an Asig signal:

A 1s / 440 Hz sine tone at sampling rate 44100 as channel name 'left':

import numpy as np
signal_array = np.sin(2 * np.pi * 440 * np.linspace(0, 1, 44100))
atone = Asig(signal_array, sr=44100, label='1s sine tone', cn=['left'])

Other ways of creating an Asig object:

asig_int = Asig(44100, sr=44100)  # zero array with 44100 samples
asig_float = Asig(2., sr=44100)  # float argument, 2 seconds of zero array
asig_str = Asig('./song.wav')  # load audio file
asig_ugen = Ugen().square(freq=440, sr=44100, dur=2., amp=0.5)  # using Ugen class to create common waveforms

Audio files are also possible using the file path. WAV should work without issues. MP3 is supported but may raise error if FFmpeg.

Key attributes

  • atone.sig --> The numpy array containing the signal is
  • atone.sr --> the sampling rate
  • atone.cn --> the list of custom defined channel names
  • atone.label --> a custom set identifier string

Play signals

atone.play(server=s)  

play() uses Aserver.default if server is not specified

Plotting signals

to 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 class

atone.to_spec().plot()

to plot the spectrogram via the Astft class

atone.to_stft().plot(ampdb)

Selection of subsets

  • Asigs 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 1

Method chaining

Asig methods usually return an Asig, so methods can be chained, e.g

atone[{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 more

  • Please check the examples/pya-examples.ipynb for more examples and details.

Contributing

  • Please 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pya-0.3.3.tar.gz (2.2 MB view details)

Uploaded Source

Built Distribution

pya-0.3.3-py3-none-any.whl (46.5 kB view details)

Uploaded Python 3

File details

Details for the file pya-0.3.3.tar.gz.

File metadata

  • Download URL: pya-0.3.3.tar.gz
  • Upload date:
  • Size: 2.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for pya-0.3.3.tar.gz
Algorithm Hash digest
SHA256 2343765ec6cd7589cfa755d2b2a873e021ddb62ba6bfbf07f94270816bfec6de
MD5 ca6d44164af7af89d950bc3a5d69b784
BLAKE2b-256 46735fce5cc01bdc9ee0c7d9fd29d954e25df28c63e4ae6375761d179e0f640c

See more details on using hashes here.

File details

Details for the file pya-0.3.3-py3-none-any.whl.

File metadata

  • Download URL: pya-0.3.3-py3-none-any.whl
  • Upload date:
  • Size: 46.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for pya-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b08f643e2b4cd7010b46467a875377994de9096acceb9daa1ec77789bcebcae1
MD5 d1d0aaec761f3a3db0b918a0a5fcf3bf
BLAKE2b-256 604523e1e5bed00dcddf44731fd30db427b265be7762149bb19e34954fde74e9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page