#MAT 201A Winter 2016
#HW 2
#Zhenyu Yang
%pylab inline
rcParams['figure.figsize'] = (10, 4) #wide graphs by default
from __future__ import print_function
from __future__ import division
from scipy.io import wavfile
from PIL import Image
playRate = 44100 #define rate of player
Populating the interactive namespace from numpy and matplotlib
## The following code is converting an image to Audio
## Note that this image was encoded from an audio file
#read bmp
imgRead = Image.open("img.bmp")
imshow(imgRead)
widthReceived,lengthReceived = imgRead.size
arrayReceived = zeros(widthReceived*lengthReceived)
for i in range(widthReceived): # for every colunm
for j in range(lengthReceived): # for every element
r, g, b = imgRead.getpixel((i, j)) # read each pixal
arrayReceived[i*lengthReceived+j] = b # use values of blue pixals to recover audio data
src_mono_recovered = ((arrayReceived)).astype(int) # recover audio data to its o
plot(src_mono_recovered) #plot audio wave
[<matplotlib.lines.Line2D at 0xad5d0f0>]
from IPython.display import Audio
Audio(src_mono_recovered, rate=playRate) #Play results
## The following code is converting an image to Audio
## The RGB values are treated as spectrum value and are processed by irfft
#read bmp
imgRead = Image.open("test.jpg")
sampleCycle = 100
widthReceived,lengthReceived = imgRead.size
arrayReceivedReal = zeros(widthReceived*lengthReceived)
arrayReceivedImag = zeros(widthReceived*lengthReceived)
arrayReceivedComplex = zeros(widthReceived*lengthReceived)
arrayReceivedRed = zeros(widthReceived*lengthReceived)
arrayReceivedGreen = zeros(widthReceived*lengthReceived)
arrayReceivedBlue = zeros(widthReceived*lengthReceived)
for i in range(widthReceived): # for every colunm
for j in range(lengthReceived): # for every element
r, g, b = imgRead.getpixel((i, j)) # read each pixal
#arrayReceived[i*lengthReceived+j] = b # use values of blue pixals to recover audio data
arrayReceivedReal[i*lengthReceived+j] = r+b # use values of blue pixals to recover audio data
arrayReceivedImag[i*lengthReceived+j] = b+g
if j == i:
arrayReceivedRed[i*lengthReceived+j] = r
arrayReceivedGreen[i*lengthReceived+j] = g
arrayReceivedBlue[i*lengthReceived+j] = b
continue
arrayReceivedComplex = arrayReceivedReal + 1j*arrayReceivedImag
arrayReceivedImag
array([ 510., 510., 510., ..., 506., 510., 501.])
#read bmp
imgRead2 = imread("test.jpg")
imshow(imgRead2)
<matplotlib.image.AxesImage at 0xad5d860>
mag_spec = imgRead2[:,0,0]
phs_spec = imgRead2[:,0,1]
X = [np.complex(cos(phs)* mag, sin(phs)* mag) for mag, phs in zip(mag_spec, phs_spec)]
signal = list(fft.irfft(X)*6000)
for i in range(1,imgRead2.shape[1]):
mag_spec = imgRead2[:,i,0]
phs_spec = imgRead2[:,i,1]
X = [np.complex(cos(phs)* mag, sin(phs)* mag) for mag, phs in zip(mag_spec, phs_spec)]
x = fft.irfft(X)
signal = signal+list(x*6000)
plot(signal) #plot audio wave
from IPython.display import Audio
Audio(signal, rate=playRate) #Play results
scaledToKeynot = ((signal/max(max(signal),-min(signal)))*88).astype(int) # scale resulted signal to keynote range
for i in range(scaledToKeynot.size): # reverse negative values in the list
if(scaledToKeynot[i]<0):
scaledToKeynot[i] = -scaledToKeynot[i]
continue
# The following part is based on personal insterst, which generats a piece of music from 88 piano keynotes
# generation uses data from image
# This part generates some basic chords C D Em F G Am A Bm by using 88 seperated piano sound files
src2Add_mono = [0]
src2BEAdd_mono = zeros(22000)
src2Add_mono = zeros(25000)
chordList =['c3','e3','g3']
for i in range(len(chordList)): # for every colunm
filename = "sounds/" + str(chordList[i])+".wav"
sr2BeAdd,src2BeAdd = wavfile.read(filename) #read audio data
src2BEAdd_mono = sum(src2BeAdd.astype(float)/src2BeAdd.shape[1], axis=1).astype(src2BeAdd.dtype)#pick one channel, convert to mono
for j in range(len(src2BEAdd_mono)):
#src2Add_mono = np.concatenate((src2Add_mono,src2BEAdd_mono), axis=0) #add
src2Add_mono[j] = src2Add_mono[j]+src2BEAdd_mono[j] #mix
chordC = src2Add_mono
src2Add_mono = zeros(25000)
chordList =['d3','f3m','a3']
for i in range(len(chordList)): # for every colunm
filename = "sounds/" + str(chordList[i])+".wav"
sr2BeAdd,src2BeAdd = wavfile.read(filename) #read audio data
src2BEAdd_mono = sum(src2BeAdd.astype(float)/src2BeAdd.shape[1], axis=1).astype(src2BeAdd.dtype)#pick one channel, convert to mono
for j in range(len(src2BEAdd_mono)):
#src2Add_mono = np.concatenate((src2Add_mono,src2BEAdd_mono), axis=0) #add
src2Add_mono[j] = src2Add_mono[j]+src2BEAdd_mono[j] #mix
chordD = src2Add_mono
src2Add_mono = zeros(25000)
chordList =['e3','g3','b3']
for i in range(len(chordList)): # for every colunm
filename = "sounds/" + str(chordList[i])+".wav"
sr2BeAdd,src2BeAdd = wavfile.read(filename) #read audio data
src2BEAdd_mono = sum(src2BeAdd.astype(float)/src2BeAdd.shape[1], axis=1).astype(src2BeAdd.dtype)#pick one channel, convert to mono
for j in range(len(src2BEAdd_mono)):
#src2Add_mono = np.concatenate((src2Add_mono,src2BEAdd_mono), axis=0) #add
src2Add_mono[j] = src2Add_mono[j]+src2BEAdd_mono[j] #mix
chordEm = src2Add_mono
src2Add_mono = zeros(25000)
chordList =['f3','a3','c4']
for i in range(len(chordList)): # for every colunm
filename = "sounds/" + str(chordList[i])+".wav"
sr2BeAdd,src2BeAdd = wavfile.read(filename) #read audio data
src2BEAdd_mono = sum(src2BeAdd.astype(float)/src2BeAdd.shape[1], axis=1).astype(src2BeAdd.dtype)#pick one channel, convert to mono
for j in range(len(src2BEAdd_mono)):
#src2Add_mono = np.concatenate((src2Add_mono,src2BEAdd_mono), axis=0) #add
src2Add_mono[j] = src2Add_mono[j]+src2BEAdd_mono[j] #mix
chordF = src2Add_mono
src2Add_mono = zeros(25000)
chordList =['g3','b3','d4']
for i in range(len(chordList)): # for every colunm
filename = "sounds/" + str(chordList[i])+".wav"
sr2BeAdd,src2BeAdd = wavfile.read(filename) #read audio data
src2BEAdd_mono = sum(src2BeAdd.astype(float)/src2BeAdd.shape[1], axis=1).astype(src2BeAdd.dtype)#pick one channel, convert to mono
for j in range(len(src2BEAdd_mono)):
#src2Add_mono = np.concatenate((src2Add_mono,src2BEAdd_mono), axis=0) #add
src2Add_mono[j] = src2Add_mono[j]+src2BEAdd_mono[j] #mix
chordG = src2Add_mono
src2Add_mono = zeros(25000)
chordList =['a3','c4m','e4']
for i in range(len(chordList)): # for every colunm
filename = "sounds/" + str(chordList[i])+".wav"
sr2BeAdd,src2BeAdd = wavfile.read(filename) #read audio data
src2BEAdd_mono = sum(src2BeAdd.astype(float)/src2BeAdd.shape[1], axis=1).astype(src2BeAdd.dtype)#pick one channel, convert to mono
for j in range(len(src2BEAdd_mono)):
#src2Add_mono = np.concatenate((src2Add_mono,src2BEAdd_mono), axis=0) #add
src2Add_mono[j] = src2Add_mono[j]+src2BEAdd_mono[j] #mix
chordA = src2Add_mono
src2Add_mono = zeros(25000)
chordList =['g3','c4','e4']
for i in range(len(chordList)): # for every colunm
filename = "sounds/" + str(chordList[i])+".wav"
sr2BeAdd,src2BeAdd = wavfile.read(filename) #read audio data
src2BEAdd_mono = sum(src2BeAdd.astype(float)/src2BeAdd.shape[1], axis=1).astype(src2BeAdd.dtype)#pick one channel, convert to mono
for j in range(len(src2BEAdd_mono)):
#src2Add_mono = np.concatenate((src2Add_mono,src2BEAdd_mono), axis=0) #add
src2Add_mono[j] = src2Add_mono[j]+src2BEAdd_mono[j] #mix
chordAm = src2Add_mono
src2Add_mono = zeros(25000)
chordList =['b2','d3m','f3m']
for i in range(len(chordList)): # for every colunm
filename = "sounds/" + str(chordList[i])+".wav"
sr2BeAdd,src2BeAdd = wavfile.read(filename) #read audio data
src2BEAdd_mono = sum(src2BeAdd.astype(float)/src2BeAdd.shape[1], axis=1).astype(src2BeAdd.dtype)#pick one channel, convert to mono
for j in range(len(src2BEAdd_mono)):
#src2Add_mono = np.concatenate((src2Add_mono,src2BEAdd_mono), axis=0) #add
src2Add_mono[j] = src2Add_mono[j]+src2BEAdd_mono[j] #mix
chordBm = src2Add_mono
gapCounter = 0 # too many data, sampling data with certain gap
gapTarget = 10000
musicResult = zeros(50)
mucisCounter = 0
for i in range(scaledToKeynot.size): # for every colunm
gapCounter = gapCounter+1;
if (gapCounter == gapTarget):
musicResult[mucisCounter] = scaledToKeynot[i]
mucisCounter = mucisCounter+1
gapCounter = 0
continue
musicResult = musicResult.astype(int)
#gerate base level of music
music = ['C','G','Am','Em','F','C','F','G','C','G','Am','Em','F','C','F','G','C','G','Am','Em','F','C','F','G','C','G','Am','Em','F','C','F','G','C','G','Am','Em','F','C','F','G','C','G','Am','Em','F','C','F','G','F','G']
# this part converts data from image into a chord list in order
for i in range(musicResult.size):
if musicResult[i] == 0:
music[i] = 'C'
continue
if musicResult[i] == 1:
music[i] = 'D'
continue
if musicResult[i] == 2:
music[i] = 'Em'
continue
if musicResult[i] == 3:
music[i] = 'F'
continue
if musicResult[i] == 4:
music[i] = 'G'
continue
if musicResult[i] == 5:
music[i] = 'Am'
continue
if musicResult[i] == 6:
music[i] = 'A'
continue
if musicResult[i] == 7:
music[i] = 'Bm'
continue
if musicResult[i] == 8:
music[i] = 'C'
continue
if musicResult[i] == 9:
music[i] = 'Am'
continue
if musicResult[i] >= 10:
music[i] = 'G'
continue
music =['C','G','Am','Em','F','C','F','G'] #test music can be removed to test information from image
#this part generates music of basic level based on the chord list
song =[]
for i in range(len(music)): # for every colunm
if music[i] == 'C':
song = np.concatenate((song,chordC), axis=0)
continue
if music[i] == 'D':
song = np.concatenate((song,chordD), axis=0)
continue
if music[i] == 'Em':
song = np.concatenate((song,chordEm), axis=0)
continue
if music[i] == 'F':
song = np.concatenate((song,chordF), axis=0)
continue
if music[i] == 'G':
song = np.concatenate((song,chordG), axis=0)
continue
if music[i] == 'A':
song = np.concatenate((song,chordA), axis=0)
continue
if music[i] == 'Am':
song = np.concatenate((song,chordAm), axis=0)
continue
if music[i] == 'Bm':
song = np.concatenate((song,chordBm), axis=0)
continue
Audio(song, rate=sr2BeAdd) #Play results of pureChords of C G Am Em C F C G.
ram = musicResult
# this part adds saperate keynotes into music
randomOfGap = ((numpy.random.random(size=50)+1)*2-1).astype(int) # random gap
#song = song*0.8 # lower volume
keyCounter = 0;
keyLength = 0;
k = 0;
ramCounter = 0;
gap = chordC.size/2
for i in range(song.size):
k = k+1
if k==gap:
k=0;
srcKey = zeros(3)
if(ramCounter <ram.size):
gap = chordC.size/(randomOfGap[ramCounter]*2)
filename = "sounds/" + str(ram[ramCounter])+".wav" #import seperate piano sounds
ramCounter = ramCounter+1
srKey,srcKey = wavfile.read(filename) #read audio data
Key = sum(srcKey.astype(float)/srcKey.shape[1], axis=1).astype(srcKey.dtype)#pick one channel, convert to mono
keyLength = Key.size
for j in range(keyLength):
if (j*2 < keyLength)and(j+i<song.size):
song[j+i] = song[j+i]+Key[j*2] #mix
continue
continue
continue
Audio(song, rate=sr2BeAdd) #Play results