Python doing music

Python doing music

Share it!

Tania ask me this Sunday morning if I was able to make a “better” sound out of a .wav file for a project in a course… So, I started googling and found different softwares to do this kind of stuff, but you need to pay for them and they are more like the “professional” kind of software. Not really what we wanted here.

So, I carry on googling but adding the magic word “python” 😉 until I found a great music synthesizer made in python by a guy called Martin C. Doege, the sauce is here.

Its important to notice that this was file comes for a 3D model… so, .stl file to .wav (via importing raw data in Audacity).

This is the original sound file (beware: it sounds horrible!!)

 

When I tested the examples, I was able to hear the classic synthesizer sound, which I really love. I remembered my padrisinimo Juan who introduced me into the music of Wendy Carlos, which is awesome both in sound and in complexity. For example, the album Switch-on-Bach was Carlos’ break-through album, one of the first to draw attention to the synthesizer as a genuine musical instrument, and it took 4 years to complete!!!! Listen to this mix.

Soooo… the basic idea was to read the wav file, get the data, which is an array of numbers with lots of precision. That particular wav file (the one Tania gave me), it ranges from -1 to 1, and it was 1519616 on length. So, the idea was to map those values, and the changed them to notes, and then create a synthesizer sound file from it.

Of course there is more music science behind it, but me, being not a music scientist I fixed the octaves, and with that the time changed… the final version sounds like this:

 

And thats how a 3D part sounds!!!!

Making sounds with objects.

The code is here:

[python title=”Code to ransform a wav file to synthesizer sound”]
#!/usr/bin/env python

"""wav-to-synth.py: Transform a wav file to synthesizer sound"""
"""
– Using the PySynth library from Martin C. Doege
– http://mdoege.github.io/PySynth/
– The sound is similar to a: DX7 e-piano
* Using Audiolab to read the wav file (maybe there us an easier way of doing this…)
* https://pypi.python.org/pypi/scikits.audiolab/
"""
__author__ = "Aldo Vargas"
__copyright__ = "Copyright 2015 Aldux.net"

__license__ = "GPL"
__version__ = "1"
__maintainer__ = "Aldo Vargas"
__email__ = "alduxvm@gmail.com"
__status__ = "Development"

import pysynth
import numpy as np
from scikits.audiolab import wavread

# 88 notes or piano key frequencies (for equal temperament)
notes = [‘a0’, ‘a#0’, ‘b0’, ‘c1’, ‘c#1’, ‘d1’, ‘d#1’, ‘e1’, ‘f1’, ‘f#1’, ‘g1’, ‘g#1’, ‘a1’, ‘a#1’, ‘b1’, ‘c2’, ‘c#2’, ‘d2’, ‘d#2’, ‘e2’, ‘f2’, ‘f#2’, ‘g2’, ‘g#2’, ‘a2’, ‘a#2’, ‘b2’, ‘c3’, ‘c#3’, ‘d3’, ‘d#3’, ‘e3’, ‘f3’, ‘f#3’, ‘g3’, ‘g#3’, ‘a3’, ‘a#3’, ‘b3’, ‘c4’, ‘c#4’, ‘d4’, ‘d#4’, ‘e4’, ‘f4’, ‘f#4’, ‘g4’, ‘g#4’, ‘a4’, ‘a#4’, ‘b4’, ‘c5’, ‘c#5’, ‘d5’, ‘d#5’, ‘e5’, ‘f5’, ‘f#5’, ‘g5’, ‘g#5’, ‘a5’, ‘a#5’, ‘b5’, ‘c6’, ‘c#6’, ‘d6’, ‘d#6’, ‘e6’, ‘f6’, ‘f#6’, ‘g6’, ‘g#6’, ‘a6’, ‘a#6’, ‘b6’, ‘c7’, ‘c#7’, ‘d7’, ‘d#7’, ‘e7’, ‘f7’, ‘f#7’, ‘g7’, ‘g#7’, ‘a7’, ‘a#7’, ‘b7’, ‘c8’]

# Re-maps a number from one range to another.
def map(x, in_min, in_max, out_min, out_max):
return (x – in_min) * (out_max – out_min) // (in_max – in_min) + out_min

# Round and convert to integer a array.
def roundandint(a, decimals=1):
b=np.around(a-10**(-(decimals+5)), decimals=decimals)
return b.astype(int)

# Change the numbers to piano key notes of 2 channels
def toNotes(data):
c1 = ();
c2 = ();
for k in range(100):
c1=((notes[data[k][0]],2),)+c1
c2=((notes[data[k][1]],4),)+c2
return (c1,c2)

if __name__=="__main__":
# Read the wav file
filename = "test2.wav"
data, sample_frequency,encoding = wavread(filename)

# Transform it to notes
c1,c2 = toNotes(roundandint(map(data,-1,1,1,88),0))

# Save channel 1 and 2 and the mix them
pysynth.make_wav(c1, fn = "c1.wav")
pysynth.make_wav(c2, fn = "c2.wav")
pysynth.mix_files("c1.wav", "c2.wav", "final.wav")
[/python]

Share it!

One thought on “Python doing music

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.