剧情提要:
[机器小伟]在[工程师阿伟]的陪同下进行着声学神通的修炼。
这次要研究的是[复合波数据]。
正剧开始:
星历2016年05月05日 10:41:23, 银河系厄尔斯星球中华帝国江南行省。
[工程师阿伟]正在和[机器小伟]一起研究[复合波数据]。
这里面稍微有一点规律,但也不是很好描述。
遇到实际数据时找个差不多类型的对照一下。
def waveDataChoose2(): import wavedata; import wavename; array = wavename.liter; size = len(array); N = 100; sampledata = []; for i in range(N): index = random.randint(0, size-1); name = array[index]; tmpdata = eval('wavedata.'+name); if (len(tmpdata) > 0): if i%2 == 0: for j in range(6): sampledata += tmpdata; for j in range(2): sampledata += idleWave; elif i%2 == 1: for j in range(8): sampledata += tmpdata; for j in range(10): sampledata += idleWave; #print(len(sampledata)); return sampledata; #调用入口, 生成wav文件 def tmp(filename): #filename = ['combineWave_100_70_50', 'combineWave_100_80_50', 'combineWave_100_80_60', 'combineWave_110_70_50', 'combineWave_110_80_50', 'combineWave_110_80_60', 'combineWave_110_90_50', 'combineWave_110_90_60', 'combineWave_110_90_70']; size = len(filename); for i in range(size): if i > 300: break; writeWav(i, filename); #生成声波数据 def tmp2(): print('生成波形开始。>>>'); fout= open('output.txt', 'w'); for i in range(50, 500, 50): for j in range(550, 1050, 50): for k in range(1350, 1450, 50): dataStr = 'combineWave_'+str(i)+'_'+str(j)+'_'+str(k) + ' = ['; fout.write(dataStr); data = waveDataGen(i, j, k); size = len(data); for n in range(size): fout.write(str(data[n])); if (n < size-1): fout.write(', '); fout.write('];'); fout.write('\r\n'); fout.close(); print('生成波形结束,请到output.txt查收。'); #打印文件名 def tmp3(): filename = []; for i in range(50, 500, 50): for j in range(550, 1050, 50): for k in range(1350, 1450, 50): dataStr = 'combineWave_'+str(i)+'_'+str(j)+'_'+str(k); filename.append(dataStr); print(filename); #生成声波数据 def tmp2_2(): print('生成波形开始。>>>'); fout= open('output.txt', 'w'); #终点不包括 #这里要和tmp3中的改动对应上 array = [[1000,1400,2400],[1100,1550,2600],[600,1000,2000],[900,1300,3000], [600,1400,2400],[700,1450,3200],[650,2050,2600],[550,2400,3000],[400,2300,3500], [350,2900,3500], [400, 700, 700], [350, 650, 650], [450,2200, 2750], [350, 2300, 2750],[500, 1500, 2600],[500,1500,3100],[450, 1700, 2400], [450, 1900, 2600],[600, 1500, 2000], [750, 1750, 3600], [2000, 6000, 6000], [7000, 7000, 7000], [1600, 6500, 6500], [2000,6500,6500], [5500, 5500, 7000], [5000, 5000,7000], [2100, 3000, 6000], [3800, 3800, 6800], [5400, 5400, 7000], [2000, 3000, 6800], [4500, 4500, 6500], [2000, 5000, 7000], [3000, 4500, 6500], [3000, 3000, 6500]]; for n in range(len(array)): i, j, k = array[n][0], array[n][1], array[n][2]; dataStr = 'combineWave_'+str(i)+'_'+str(j)+'_'+str(k) + ' = ['; fout.write(dataStr); data = waveDataGen(i, j, k); size = len(data); for m in range(size): fout.write(str(data[m])); if (n < size-1): fout.write(' ,'); fout.write('];'); fout.write('\r\n'); fout.close(); print('生成波形结束,请到output.txt查收。'); #打印文件名 def tmp3_2(): filename = []; #终点不包括 #这里要和tmp2中的改动对应上 array = [[1000,1400,2400],[1100,1550,2600],[600,1000,2000],[900,1300,3000], [600,1400,2400],[700,1450,3200],[650,2050,2600],[550,2400,3000],[400,2300,3500], [350,2900,3500], [400, 700, 700], [350, 650, 650], [450,2200, 2750], [350, 2300, 2750],[500, 1500, 2600],[500,1500,3100],[450, 1700, 2400], [450, 1900, 2600],[600, 1500, 2000], [750, 1750, 3600], [2000, 6000, 6000], [7000, 7000, 7000], [1600, 6500, 6500], [2000,6500,6500], [5500, 5500, 7000], [5000, 5000,7000], [2100, 3000, 6000], [3800, 3800, 6800], [5400, 5400, 7000], [2000, 3000, 6800], [4500, 4500, 6500], [2000, 5000, 7000], [3000, 4500, 6500], [3000, 3000, 6500]]; for n in range(len(array)): i, j, k = array[n][0], array[n][1], array[n][2]; dataStr = 'combineWave_'+str(i)+'_'+str(j)+'_'+str(k); filename.append(dataStr); print(filename);
### # @usage 写.wav文件,能把声波数据阵列用二进制写成.wav。 # @author mw # @date 2016年04月28日 星期四 14:31:34 # @param # @return # ### def writeWav(index = 0 , filename = 'RandWave'): byteArray = []; dataArray = []; #filename = ['combineWave_100_70_50', 'combineWave_100_80_50', 'combineWave_100_80_60', 'combineWave_110_70_50', 'combineWave_110_80_50', 'combineWave_110_80_60', 'combineWave_110_90_50', 'combineWave_110_90_60', 'combineWave_110_90_70']; #样本数据阵列 sampleArray = waveDataChoose2(); #样本数据点数 N = len(sampleArray); times = 3; dataSize = N*times; fileSize = dataSize+44; #44为格式头部分所用字节数 #RIFF WAVE CHUNK RIFF_ID = [0x52, 0x49, 0x46, 0x46]; #'RIFF' RIFF_Size = littleEndian(fileSize-8, 4); #文件总字节数减去8 RIFF_Type = [0x57, 0x41,0x56, 0x45, 0x66, 0x6D, 0x74, 0x20]; #'WAVEfat ' #Format Chunk Format_10_17 = [0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00];#过滤4+格式2+声道2=8个字节 Format_18_1B = [0x11, 0x2B, 0x00, 0x00]; #采样频率0x2B11 = 11025 Format_1C_1F = [0x22, 0x56, 0x00, 0x00]; #比持率 = 频率*通道*样本位 = 22050 Format_20_23 = [0x02, 0x00, 0x10, 0x00]; #块对齐 = 通道数* 样本位数 = 1*2 = 2 #Fact Chunk(optional) #Data Chunk Data_24_27 = [0x64, 0x61, 0x74, 0x61]; #'DATA'标记 Data_Size = littleEndian(fileSize-44, 4); #下面的Data部分的字节数,文件总字节数-44 #RIFF WAVE CHUNK ''' for i in range(4): byte = struct.pack('B', RIFF_ID[i]); byteArray.append(byte); ''' RIFF = [b'R', b'I', b'F', b'F']; for i in range(4): byteArray.append(RIFF[i]); for i in range(4): byte = struct.pack('B', RIFF_Size[i]); byteArray.append(byte); ''' for i in range(8): byte = struct.pack('B', RIFF_Type[i]); byteArray.append(byte); #Format Chunk for i in range(8): byte = struct.pack('B', Format_10_17[i]); byteArray.append(byte); for i in range(4): byte = struct.pack('B', Format_18_1B[i]); byteArray.append(byte); for i in range(4): byte = struct.pack('B', Format_1C_1F[i]); byteArray.append(byte); for i in range(4): byte = struct.pack('B', Format_20_23[i]); byteArray.append(byte); #Data Chunk for i in range(4): byte = struct.pack('B', Data_24_27[i]); byteArray.append(byte); ''' #Format_28_2B是数据块大小,formatHead是它前面的所有部分 formatHead = [b'W', b'A', b'V', b'E', b'f', b'm', b't', b' ', b'\x10', b'\x00', b'\x00', b'\x00', b'\x01', b'\x00', b'\x01', b'\x00', b'\x11', b'+', b'\x00', b'\x00', b'"', b'V', b'\x00', b'\x00', b'\x02', b'\x00', b'\x10', b'\x00', b'd', b'a', b't', b'a']; for i in range(len(formatHead)): byteArray.append(formatHead[i]); for i in range(4): byte = struct.pack('B', Data_Size[i]); byteArray.append(byte); #写出到文件 print('文件写入开始。>>>'); fout= open(filename+'_'+str(index)+'.wav', 'wb'); size = len(byteArray); for i in range(size): fout.write(byteArray[i]); size = len(sampleArray); sizeIdle = len(idleWave); for j in range(times): for i in range(size): fout.write(sampleArray[i]); fout.close(); print('文件写入完毕。');
本节到此结束,欲知后事如何,请看下回分解。
时间: 2024-10-10 13:19:35