Skip to content

dboyliao/pyaudio_wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Simple Audio Wrapper of PyAudio

PyAudio is a python binding for PortAudio. However, the api, to my humble opinion, is not so friendly to the programmer who has no deep or comprehensive knowledge about audio data. This package is yet another wrapper build upon PyAudio in order to provide a friendly api.

Basic Usage

Working with WAV Audio Data

from pyaudio_wrapper.audio_data import WavFileAudioData

wav_audio = WavFileAudioData("tests/data/my_voice.wav")

# Get the raw wav data in bytes string(hex format).
wav_audio.raw_wav_data

# duration of the audio file in milliseconds.
wav_audio.duration     
# >>> 3008

# A numpy array of the wav audio data.
wav_audio.data         

# Play the audio.
wav_audio.play()       

# Play the audio from 500 millisecond to 1500 millisecond.
wav_audio.play(500, 1500)

# Segmentation of audio data
# segment from 500 millisecond to 1500 millisecond.
wav_segment = wav_audio[500:1500] 
wav_segment.play() # just work!

Analyse Audio Data

from pyaudio_wrapper.analyse import AudioAnalysor

analysor = AudioAnalysor(wav_audio)

# plot the audio data.
analysor.plot() 
analysor.show()

analysor_plot

You can also use your mouse to select a segment of the data and then you will see the zoomed-in image of that segment in the subplot below. Moreover, you will hear the sound of that segment right away. plot_segment

If the audio is stereo, you will see something like this: plot_stereo

coef = analysor.fft() # fast fourier transform.

fft

# Or you can plot the spectrum.
analysor.plot_spectrum()
analysor.show()

fft_plot

If the audio is stereo, you will see something like this: fft_plot_stereo

Fun Time

from pyaudio_wrapper.notes import *

(C + D).play()     # play Do and Re for 0.5 sec, repsectively
happy.play()    # One well-known song. : )

# My goal is to use this package to compose music in a simple way. XDD

Recording Audio Data From Microphone

from pyaudio_wrapper import Recorder
from pyaudio_wrapper.source import Microphone

recorder = Recorder() # Recording the sound from default microphone.
with Microphone() as source:
    wav_audio = recorder.record(source)

wav_audio.play()             # play out the audio.
wav_audio.data               # an numpy array of this audio data
wav_audio.save('record.wav') # save the audio as a wav file.

Installation

git clone https://github.com/dboyliao/pyaudio_wrapper.git
cd pyaudio_wrapper
make install

# Run `make uninstall` if you want to uninstall this package.
# Before remove related files from your computer, there will 
# be a prompt shows up asking you whether to remove the files 
# from the directory shows up in the prompt message. Be sure 
# that the directory is correct one or the uninstallation will
# just be skipped. 

Troble Shooting

  • The installation script works on my mac OS X 10.10.
  • If you can not successfully install this package on OS X 10.10, you can try to re-install python with brew. That is, run brew install python.
  • I use docker to test installation scripts with ubuntu. It can successfully install portaudio and pyaudio but once you instantiate pyaudio.PyAudio, ALSA breaks. I don't know why by now....
  • I don't know how to install portaudio and pyaudio on Window. A little help here?

Reference

About

A simple wrapper of pyaudio in order to utilize audio resources.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published