Source code :: SoundGenerator

[Return]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os
#from matplotlib.pyplot import *
from numpy import *
from scipy.stats.mstats  import mquantiles



def soundGenerator(signal,downsampl_freq,  name = 'sound', absmax=0):

    print "sound gen. start"

    
    signal-=median(signal)
    MIN = min(mquantiles(signal,0.0001), -absmax)
    MAX = max(mquantiles(signal,0.9999), absmax)
    signal =(signal-MIN)/(MAX-MIN)
    signal *=2**16-1
 
    signal_16bit = uint16(signal)

    #plot(signal_16bit)
    #show()
    fi=open(name+'.raw',"wb")
    fi.write(signal_16bit)
    fi.close()

    os.system("sox -r "+str(int(downsampl_freq))+" -e unsigned -b 16 -c1 "+name+".raw  "+name+".wav")
        
    os.system("lame -b 512 "+name+".wav "+name+".mp3")
    
    try:
      os.remove(name+'.wav')
    except:
      pass
    try:
      os.remove(name+'.raw')
    except:
      pass

#use the http://flash-mp3-player.net/players/maxi/generator/

Navigation