Reproduction of sound from a waveform stored in an array

I'm currently experimenting with making sounds in Python, and I'm curious how I can take an n array representing a waveform (with a sampling frequency of 44100 Hz) and reproduce it. I'm looking for pure Python here, and not relying on a library that supports more than just the .wav format.

+6
source share
4 answers

You must use the library. Writing all this in pure python can be many thousands of lines of code to interact with audio equipment!

With a library, for example. audiere, it will be so simple:

import audiere ds = audiere.open_device() os = ds.open_array(input_array, 44100) os.play() 

There is also a piglet, pygame and many others.

+4
source

I think you can see this list http://wiki.python.org/moin/PythonInMusic It lists many useful tools for working with sound.

+3
source

Play sound with a given array of input_array of 16 bits. This is a modified example from the pyadio documentation page

 import pyaudio # instantiate PyAudio (1) p = pyaudio.PyAudio() # open stream (2), 2 is size in bytes of int16 stream = p.open(format=p.get_format_from_width(2), channels=1, rate=44100, output=True) # play stream (3), blocking call stream.write(input_array) # stop stream (4) stream.stop_stream() stream.close() # close PyAudio (5) p.terminate() 
+3
source

or use the sounddevice module. Install using pip install sounddevice , but you need it first: sudo apt-get install libportaudio2

absolute basis:

 import numpy as np import sounddevice as sd sd.play(myarray) #may need to be normalised like in below example #myarray must be a numpy array. If not, convert with np.array(myarray) 

A few more options:

 import numpy as np import sounddevice as sd #variables samplfreq = 100 #the sampling frequency of your data (mine=100Hz, yours=44100) factor = 10 #incr./decr frequency (speed up / slow down by a factor) (normal speed = 1) #data print('..interpolating data') arr = myarray #normalise the data to between -1 and 1. If your data wasn't/isn't normalised it will be very noisy when played here sd.play( arr / np.max(np.abs(arr)), samplfreq*factor) 
+3
source

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


All Articles