def exec_2_Random_Pitches(): ''' Gen sounds ''' A = 1.0 fs = 8000.0 phase = 1.0 length = 1.0 dpath = 'audio' # A = 1.0; fs = 8000.0; phase = 1.0; length = 2.0; dpath = 'audio' timelabel = get_TimeLabel_Now() base_tone = 262 # 'A' pitch #ref seed https://docs.python.jp/3/library/random.html rnd.seed() #ref https://stackoverflow.com/questions/4172131/create-random-list-of-integers-in-python 'answered Nov 13 '10 at 11:14' #ref http://www.python-izm.com/contents/application/random.shtml indexes = [rnd.randint(0, len(values) - 1) for i in range(0, 8)] # indexes = [rnd.randint(0, len(values)) for i in range(0, 8)] count = 1 for num in indexes: # for val in values : base_tone = 262 # base_tone = values[num] current_tone = base_tone * values[num] #ref multiple string https://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string 'answered May 18 '12 at 22:22' fname = "test_1.EqualTempera.%s.(%d).base-%d_val-%.6f"\ "_tone-%.3f_A-%.3f_phase-%.3f_len-%.3f.wav" \ % (timelabel, count, base_tone, values[num], \ (base_tone * values[num]), A, phase, length) # fname = "test_1.EqualTempera.val-%.6f_tone-%.3f_A-%.3f_phase-%.3f_len-%.3f.%s.wav" \ # % (val, (base_tone * val), A, phase, length, timelabel) wf = wl.get_SineWF(A, current_tone, fs, phase, length, dpath, fname) # wf = wl.get_SineWF(A, base_tone * val, fs, phase, length, dpath, fname) dpath_dst = "audio" print "[%s:%d] wf.fname => '%s'" % (thisfile(), linenum(), wf.fname) wl.save_WaveFile__Class(wf, dpath_dst=dpath_dst) # increment count count += 1 return
def exec_1_Equal_Temperament(): values = [ 1.000000, 1.059463, 1.122462, 1.189207, 1.259921, 1.334840, 1.414214, 1.498307, 1.587401, 1.681793, 1.781797, 1.887749, 2.000000 ] ### test wl.get_SineWF(1.0, 262, fs=8000.0, phase=1.0, length=1.0, dpath="audio", fname='') ''' Gen sounds ''' A = 1.0 fs = 8000.0 phase = 1.0 length = 2.0 dpath = 'audio' timelabel = get_TimeLabel_Now() base_tone = 262 # 'A' pitch for val in values: fname = "test_1.EqualTempera.val-%.6f_tone-%.3f_A-%.3f_phase-%.3f_len-%.3f.%s.wav" \ % (val, (base_tone * val), A, phase, length, timelabel) wf = wl.get_SineWF(A, base_tone * val, fs, phase, length, dpath, fname) dpath_dst = "audio" print "[%s:%d] wf.fname => '%s'" % (thisfile(), linenum(), wf.fname) wl.save_WaveFile__Class(wf, dpath_dst=dpath_dst) return
def exec_1__SinePlusCosine(): '''################### prep : wfs ###################''' A = 1.0; f0 = 262; fs = 8000.0; length = 1.0; phase = 1.0; type = "sine" timelabel = get_TimeLabel_Now() fname_sines = "test_1.sines.%s.f0=%d_phase-%1.2f.wav" % (timelabel, f0, phase) wf_sines = wl.get_WaveFile__Sines (fname_sines, A, f0, fs, length, phase, type) fname_cosines = "test_1.cosines.%s.f0=%d_phase-%1.2f.wav" % (timelabel, f0, phase) wf_cosines = wl.get_WaveFile__Sines (fname_cosines, A, f0, fs, length, phase, "cosine") '''################### save : wav fles ###################''' dpath_dst = "audio" wl.save_WaveFile__Class(wf_sines, dpath_dst=dpath_dst) wl.save_WaveFile__Class(wf_cosines, dpath_dst=dpath_dst) '''################### addition : sines + cosines ###################''' pairsOf_analogdata = zip(wf_sines.analogdata, wf_cosines.analogdata) analogdata_Stardardized = [1/sqrt(2) * (x + y) for x, y in pairsOf_analogdata] fname_addition = "test_1.addition.%s.wav" % (timelabel) wf_addition = wl.get_WaveFile__AnalogData(\ fname_addition, analogdata_Stardardized, A, \ length, nchannels = 1, \ radians = wf_sines.radians, \ f0 = wf_sines.basefreq) wl.save_WaveFile__Class(wf_addition, dpath_dst=dpath_dst) '''################### ending ###################''' print "[%s:%d] exec_1__SinePlusCosine ==> done" % (thisfile(), linenum()) return
def exec_1__SinePlusCosine_Standardize(): '''################### options ###################''' options = get_opt_2(sys.argv, "f") if not "-f" in [x[0] for x in options]: print "'-f' option needed" return # print options # # return '''################### prep : wfs ###################''' '''################### prep : wfs : sines ###################''' A = 1.0 fs = 8000.0 length = 1.0 phase = 1.0 type = "sine" f0_val = [x[1] for x in options if x[0] == '-f'][0] f0 = int(f0_val) # f0 = 262 * wl.EQUAL_TEMPERAMENTS[6]; # f0 = 262 * wl.EQUAL_TEMPERAMENTS[3]; # f0 = 262; timelabel = get_TimeLabel_Now() fname_Sines = "test_1.sines.%s.f0=%d_phase-%1.2f.wav" % (timelabel, f0, phase) wf_Sines = wl.get_WaveFile__Sines(fname_Sines, A, f0, fs, length, phase, type) '''################### prep : wfs : cosines ###################''' A = 1.0 fs = 8000.0 length = 1.0 phase = 1.0 type = "cosine" f0_val = [x[1] for x in options if x[0] == '-f'][0] f0 = int(f0_val) # f0 = 262 * wl.EQUAL_TEMPERAMENTS[6]; # f0 = 262 * wl.EQUAL_TEMPERAMENTS[3]; # f0 = 262; timelabel = get_TimeLabel_Now() fname_Cosines = "test_1.cosines.%s.f0=%d_phase-%1.2f.wav" % (timelabel, f0, phase) wf_Cosines = wl.get_WaveFile__Sines(fname_Cosines, A, f0, fs, length, phase, type) '''################### prep : wfs : sines + cosines ###################''' pairsOf_analogdata = zip(wf_Sines.analogdata, wf_Cosines.analogdata) analogdata_Stardardized = [ 1 / sqrt(2) * (x + y) for x, y in pairsOf_analogdata ] print "[%s:%d] analogdata_Stardardized : max = %.4f / min = %.4f" \ % (thisfile(), linenum(), \ max(analogdata_Stardardized), \ min(analogdata_Stardardized)) fname_SinesPlusCosines = "test_1.SinesPlusCosines.%s.wav" % (timelabel) wf_SinesPlusCosines = wl.get_WaveFile__AnalogData(\ fname = fname_SinesPlusCosines, \ analogdata = analogdata_Stardardized, \ A = 1.0, length = wf_Sines.length,\ radians = wf_Sines.radians, \ f0 = wf_Sines.basefreq) # A, length, nchannels = 1, \ '''################### measure freq ###################''' result_Sines = wl.measure_Frequency_4(wf_Sines) result_Cosines = wl.measure_Frequency_4(wf_Cosines) result_SinesPlusCosines = wl.measure_Frequency_4(wf_SinesPlusCosines) print "[%s:%d] freq of sines ==> %d" % (thisfile(), linenum(), result_Sines['freq']) # print "[%s:%d] freq of sines ==> %d" % (thisfile(), linenum(), result_Sines) print "[%s:%d] freq of cosines ==> %d" % (thisfile(), linenum(), result_Cosines['freq']) # print "[%s:%d] freq of cosines ==> %d" % (thisfile(), linenum(), result_Cosines) print "[%s:%d] result_Sines =>" % (thisfile(), linenum()) print result_Sines print "[%s:%d] result_Cosines =>" % (thisfile(), linenum()) print result_Cosines print "[%s:%d] result_SinesPlusCosines =>" % (thisfile(), linenum()) print result_SinesPlusCosines '''################### save to file ###################''' # sines dpath = "audio" wl.save_WaveFile__Class(wf_Sines, dpath_dst=dpath) # cosines wl.save_WaveFile__Class(wf_Cosines, dpath_dst=dpath) # sines + cosines wl.save_WaveFile__Class(wf_SinesPlusCosines, dpath_dst=dpath) print "[%s:%d] files saved" % (thisfile(), linenum()) '''################### ending ###################''' print "[%s:%d] exec_1__Measure_Frequency ==> done" % (thisfile(), linenum()) '''################### return ###################''' return
def exec_3__SinePlusSineNX_Standardize(): '''################### options ###################''' options = get_opt_2(sys.argv, "fN") # options = get_opt_2(sys.argv, "f") if not "-f" in [x[0] for x in options]: print "'-f' option needed" return if not "-N" in [x[0] for x in options]: print "'-N' option needed" return # print options # # return '''################### prep : wfs ###################''' '''################### prep : wfs : Sines ###################''' A = 1.0 fs = 16000.0 length = 1.0 phase = 1.0 type = "sine" # A = 1.0; fs = 8000.0; length = 1.0; phase = 1.0; type = "sine" f0_val = [x[1] for x in options if x[0] == '-f'][0] f0 = int(f0_val) # f0 = 262 * wl.EQUAL_TEMPERAMENTS[6]; # f0 = 262 * wl.EQUAL_TEMPERAMENTS[3]; # f0 = 262; timelabel = get_TimeLabel_Now() fname_Sines = "test_1.sines.%s.f0=%d_fs=%.1f_phase-%1.2f.wav" % ( timelabel, f0, fs, phase) wf_Sines = wl.get_WaveFile__Sines(fname_Sines, A, f0, fs, length, phase, type) # #debug # for i in range(0, 20) : # # print "[%s:%d] wf_Sines.radians[%d] => %.4f (of 2 * pi = %.4f)"\ # % (thisfile(), linenum(), \ # i, wf_Sines.radians[i], wf_Sines.radians[i] / np.pi) '''################### prep : wfs : Sines NX ###################''' # set N value N = int([x[1] for x in options if x[0] == '-N'][0]) # N = [x[1] for x in options if x[0] == '-N'][0] # default value if N == None: N = 3 type = "sine" fname_SinesNX = "test_1.Sines%dX.%s.f0=%d_fs=%.1f_phase-%1.2f.wav" \ % (N, timelabel, f0, fs, phase) wf_SinesNX_tmp = wl.get_WaveFile__Sines (\ fname_SinesNX, A, f0, fs, length, phase, type) radian_values = [N * x for x in wf_SinesNX_tmp.radians] # radian_values = [2 * x for x in wf_SinesNX_tmp.radians] # radian_values = wf_Sines2X_tmp.radians wf_SinesNX_tmp.radians = radian_values wf_SinesNX = wl.update_WaveFile(wf_SinesNX_tmp, "radians") #=> works '''################### prep : wfs : Sines + Sines nX ###################''' analogdata_SinesPlusSinesNX = [sum(x) for x \ in zip(wf_Sines.analogdata, wf_SinesNX.analogdata)] ### memo # if not standardized, then struct.pack with "h" param in wablibs.py ==> Gets error analogdata_SinesPlusSinesNX = wl.standardize_Data( analogdata_SinesPlusSinesNX) fname_SinesPlusSinesNX = \ "test_1.SinesPlusSines%dX.%s.f0=%d_fs=%.1f_phase-%1.2f.wav" \ % (N, timelabel, f0, fs, phase) wf_SinesPlusSinesNX = wl.get_WaveFile__AnalogData(\ fname_SinesPlusSinesNX, \ analogdata_SinesPlusSinesNX, \ A, length, \ nchannels = 1, radians = None, \ f0 = None, fs = None) '''################### Freq : Sines ###################''' ### Sines result_Sines = wl.measure_Frequency_4(wf_Sines) print "[%s:%d] result_Sines =>" % (thisfile(), linenum()) print result_Sines ### SinesNX result_SinesNX = wl.measure_Frequency_4(wf_SinesNX) print "[%s:%d] result_SinesNX =>" % (thisfile(), linenum()) print result_SinesNX '''################### save : wave file ###################''' ### Sines dpath = "audio" wl.save_WaveFile__Class(wavefile=wf_Sines, fname_dst=wf_Sines.fname, dpath_dst=dpath) ### Sines 2X wf = wf_SinesNX wl.save_WaveFile__Class(wavefile=wf, fname_dst=wf.fname, dpath_dst=dpath) ### Sines + Sines 2X wf = wf_SinesPlusSinesNX wl.save_WaveFile__Class(wavefile=wf, fname_dst=wf.fname, dpath_dst=dpath) '''################### ending ###################''' print "[%s:%d] exec_2__SinePlusSineNX_Standardize ==> done" % (thisfile(), linenum()) '''################### return ###################''' return
def exec_4(): '''################### Build wavefile : source #####################''' freqList = [262, 294, 330, 349, 392, 440, 494, 523] # ドレミファソラシド timelabel = get_TimeLabel_Now() """振幅A、基本周波数f0、サンプリング周波数 fs、 長さlength秒の正弦波を作成して返す""" #createSineWave (A, f0, fs, length) A = 1.0 f0 = freqList[0] fs = 8000.0 length = 1.0 (analogdata, bindata) = createSineWave_2(A, f0, fs, length) # (analogdata, bindata) = createSineWave_2(1.0, f[0], 8000.0, 1.0) dpath = "audio" fname = "test_2.sinewave-%d.%s.wav" % (f0, timelabel) fpath = "%s/%s" % (dpath, fname) wf = wl.WaveFile(fname) wf.nchannels = 1 wf.samplewidth = fs # wf.framerate=framerate # wf.nframes=nframes # wf.comptype = wavefile_src.comptype # wf.compname = wavefile_src.compname wf.analogdata = analogdata wf.bindata = bindata print "[%s:%d] wf =>" % (thisfile(), linenum()) for i in range(0, 10) : print "analogdata[%d] = %f" % (i, analogdata[i]) '''################### save file : wf #####################''' dpath_dst = "audio" wl.save_WaveFile__Class(wf, dpath_dst=dpath_dst) # wl.save_WaveFile__Class(wf, dpath_dst) # wl.save_WaveFile__Class(wf) '''################### copy #####################''' wf_2 = wl.copy_WaveFile(wf) # wf_2 = wablibs.copy_WaveFile(wf) ### report for i in range(0, 10) : print "wf_2.analogdata[%d] = %f" % (i, wf_2.analogdata[i]) print "[%s:%d] wf_2.fname => '%s'" % (thisfile(), linenum(), wf_2.fname) '''################### save file #####################''' wl.save_WaveFile__Class(wf_2, dpath_dst=dpath_dst)
def test_2_Get_SineWF(options): # print options freqList = [262, 294, 330, 349, 392, 440, 494, 523] # ドレミファソラシド timelabel = get_TimeLabel_Now() """振幅A、基本周波数f0、サンプリング周波数 fs、 長さlength秒の正弦波を作成して返す""" #createSineWave (A, f0, fs, length) ### param : A A = 1.0 ### param : f0 f0 = None #ref x in a : https://stackoverflow.com/questions/7571635/fastest-way-to-check-if-a-value-exist-in-a-list "answered Sep 27 '11 at 15:25" aryOf_f0 = [x for x in options if x[0] == '-f'] lenOf_f0 = len(aryOf_f0) if lenOf_f0 > 0 : f0 = int(aryOf_f0[0][1]) # if lenOf_f0 > 0 : f0 = aryOf_f0[0][1] else : f0 = freqList[0] # default ### param : phase phase = None aryOf_phase = [x for x in options if x[0] == '-p'] lenOf_phase = len(aryOf_phase) if lenOf_phase > 0 : phase = float(aryOf_phase[0][1]) # if lenOf_f0 > 0 : f0 = aryOf_f0[0][1] else : phase = 1.0 # default # if "-v" in [x[0] for x in options] : f0 = # f0 = freqList[0] ### param : fs # fs = 16000.0 # fs = 44100.0 fs = 8000.0 ### param : length length = 2.0 # length = 1.0 ### param : data (analogdata, bindata, radians) = wl.createSineWave(A, f0, fs, length) # (analogdata, bindata) = createSineWave_2(A, f0, fs, length) # (analogdata, bindata) = createSineWave_2(1.0, f[0], 8000.0, 1.0) ### param : file name, file path dpath = "audio" fname = "test_1.sine-%d.%s.wav" % (f0, timelabel) fpath = "%s/%s" % (dpath, fname) '''################### build : wavefile ###################''' wf = wl.get_WaveFile__Sines(fname, A, f0, fs, length, phase = 1.0) dpath_dst = "audio" wl.save_WaveFile__Class(wf, dpath_dst=dpath_dst) print "[%s:%d] file saved => '%s'" % (thisfile(), linenum(), fname)
def exec_2(options): '''################### Build wavefile : source #####################''' freqList = [262, 294, 330, 349, 392, 440, 494, 523] # ドレミファソラシド timelabel = get_TimeLabel_Now() """振幅A、基本周波数f0、サンプリング周波数 fs、 長さlength秒の正弦波を作成して返す""" #createSineWave (A, f0, fs, length) A = 1.0 f0 = freqList[0] # fs = 16000.0 # fs = 44100.0 fs = 8000.0 length = 2.0 # length = 1.0 (analogdata, bindata, radians) = wl.createSineWave(A, f0, fs, length) # (analogdata, bindata) = createSineWave_2(A, f0, fs, length) # (analogdata, bindata) = createSineWave_2(1.0, f[0], 8000.0, 1.0) dpath = "audio" fname = "test_1.sine-%d.%s.wav" % (f0, timelabel) fpath = "%s/%s" % (dpath, fname) wf = wl.WaveFile(fname) wf.nchannels = 1 wf.samplewidth = fs # wf.framerate=framerate # wf.nframes=nframes # wf.comptype = wavefile_src.comptype # wf.compname = wavefile_src.compname wf.analogdata = analogdata wf.bindata = bindata wf.radians = radians print "[%s:%d] wf =>" % (thisfile(), linenum()) for i in range(100, 110) : # for i in range(0, 10) : print "wf.analogdata[%d] = %f (radian = %f)" % (i, wf.analogdata[i], wf.radians[i]) # print "wf.analogdata[%d] = %f" % (i, wf.analogdata[i]) # print "analogdata[%d] = %f" % (i, analogdata[i]) '''################### save file : wf #####################''' dpath_dst = "audio" wl.save_WaveFile__Class(wf, dpath_dst=dpath_dst) '''################### Amplitude down ###################''' ### set value val = None # print "[%s:%d] len(result) => %d" % (thisfile(), linenum(), len(result)) # print "[%s:%d] options =>" % (thisfile(), linenum()) # print options for pair in options : # for pair in result : if pair[0] == '-v' : val = int(pair[1]) if val == None : val = 200 # default value # val = 200 # 600/1000 # val = 600 # 600/1000 amplitude_Down(wf, val) print "[%s:%d] absolutize => processed" % (thisfile(), linenum()) # for i in range(100, 110) : # # for i in range(0, 10) : # # print "wf.analogdata[%d] = %f" % (i, wf.analogdata[i]) # # print "analogdata[%d] = %f" % (i, analogdata[i]) '''################### save file : wf #####################''' dpath_dst = "audio" fname_trunk, fname_ext = os.path.splitext(wf.fname) description = "Amplitude-down" wf.fname = "%s_%s_val-%d%s" % (fname_trunk, description, val, fname_ext) wl.save_WaveFile__Class(wf, dpath_dst=dpath_dst) print "[%s:%d] file saved => '%s'" % (thisfile(), linenum(), wf.fname)
def exec_1(): '''################### Build wavefile : source #####################''' freqList = [262, 294, 330, 349, 392, 440, 494, 523] # ドレミファソラシド timelabel = get_TimeLabel_Now() """振幅A、基本周波数f0、サンプリング周波数 fs、 長さlength秒の正弦波を作成して返す""" #createSineWave (A, f0, fs, length) A = 1.0 f0 = freqList[0] fs = 16000.0 # fs = 44100.0 # fs = 8000.0 length = 2.0 # length = 1.0 (analogdata, bindata) = createSineWave_2(A, f0, fs, length) # (analogdata, bindata) = createSineWave_2(1.0, f[0], 8000.0, 1.0) dpath = "audio" fname = "test_1.sine-%d.%s.wav" % (f0, timelabel) fpath = "%s/%s" % (dpath, fname) wf = wl.WaveFile(fname) wf.nchannels = 1 wf.samplewidth = fs # wf.framerate=framerate # wf.nframes=nframes # wf.comptype = wavefile_src.comptype # wf.compname = wavefile_src.compname wf.analogdata = analogdata wf.bindata = bindata print "[%s:%d] wf =>" % (thisfile(), linenum()) for i in range(100, 110) : # for i in range(0, 10) : print "wf.analogdata[%d] = %f" % (i, wf.analogdata[i]) # print "analogdata[%d] = %f" % (i, analogdata[i]) '''################### save file : wf #####################''' dpath_dst = "audio" wl.save_WaveFile__Class(wf, dpath_dst=dpath_dst) '''################### abosolutize ###################''' data_Absolutize(wf) print "[%s:%d] absolutize => processed" % (thisfile(), linenum()) for i in range(100, 110) : # for i in range(0, 10) : print "wf.analogdata[%d] = %f" % (i, wf.analogdata[i]) # print "analogdata[%d] = %f" % (i, analogdata[i]) '''################### save file : wf #####################''' dpath_dst = "audio" fname_trunk, fname_ext = os.path.splitext(wf.fname) wf.fname = "%s_Absolute%s" % (fname_trunk, fname_ext) wl.save_WaveFile__Class(wf, dpath_dst=dpath_dst) print "[%s:%d] file saved => '%s'" % (thisfile(), linenum(), wf.fname)