有没有一种方法可以将字节数组转换为python中的.wav文件?

时间:2020-06-25 08:49:17

标签: python arrays wav

我一直在将字节数组流式传输到python,并且试图将这些数组转换为wav文件。我尝试使用几个库,但无法解决此问题。

Python代码:

import scipy.io.wavfile
 import numpy as np
 import soundfile as sf

 # scipy writes the bytes to audio file using numpyarray. Here 16 denotes sample rate (samples/sec)
 # opened a file containg byte strings and then converted them to a numpyarray and to .wav file

 def convert_bytes_to_audio():
     f = open("test.txt","r")
     for line in f:
         scipy.io.wavfile.write('format.wav',16, np.frombuffer(str.encode(line),dtype ='B'))
     f.close()

 def main():
     convert_bytes_to_audio()

 if __name__ == '__main__':
     convert_bytes_to_audio()

这是“ test.txt”中的字节数据

[-1, -7, 96, 96, 1, 96, 0, 0, -52, 0, 7, -1, -7, 96, 96, 69, -64, 0, 1, 28, 18, 47, -31, -95, 8, 64, 47, -15, -28, 69, 84, -123, 70, 66, -110, -119, 74, -91, 118, -74, -109, -46, 64, 3, -14, -45, 13, -2, -104, 38, -6, -106, 114, -55, -110, 97, 4, -16, 18, 30, -85, -11, 65, 15, 19, 112, -94, 50, -120, 79, 49, -62, 73, 106, -42, 71, 40, 2, 124, 3, -126, -110, 70, -104, -114, 78, -63, 61, -66, -125, 59, 91, 33, -122, -61, 16, -65, 30, -76, 81, 12, 46, 54, -35, -49, -109, -30, 59, -126, 120, -119, 100, -110, -14, 24, 107, -7, -33, 24, 79, 45, -126, 39, 99, 16, 79, 35, 76, -125, -16, 118, 45, 114, 118, -84, 93, 84, 72, 84, 97, 51, -54, 39, 20, -77, -41, -37, 51, -67, 114, 121, 108, -79, 41, 83, 8, 101, -14, 100, 46, 64, 38, -116, 89, 61, -114, -28, -98, -126, -31, 56, -123, 32, 58, -28, 48, -43, 107, 123, 68, -15, -40, -126, 81, -90, 16, -47, -17, -56, 110, 50, 68, 71, 46, -78, -104, 66, -42, 40, -123, -125, -49, -116, 33, 33, -60, -59, 44, -100, -73, -109, 90, 109, -54, 100, 49, 22, -78, 13, 2, 120, 12, 17, 59, -79, -56, 47, 12, 67, 37, -118, 33, 126, -122, 119, -54, 19, -38, 102, 8, 15, 38, 67, 21, 0, -126, 112, -60, 52, -103, 58, 38, 14, 12, 98, 17, 27, -52, -73, 21, 110, -94, 113, -107, 116, 34, 125, 113, 39, 16, -123, 106, 24, 61, 58, 42, 81, 19, -96, -99, 105, 68, 9, 48, -119, -111, -107, -89, 16, -60, 88, -83, -25, -52, -46, 8, 72, -106, 69, 3, 39, 26, 21, 6, 2, 108, -90, 78, 123, 45, -60, 16, 74, 72, 58, 9, 7, 44, -126, -47, 102, 7, -1, -55, -36, 116, 80, 104, 81, 19, 108, 58, -104, 127, -124, -63, 13, 68, 32, -103, 24, 77, -47, -119, -46, -95, 43, 46, 125, 25, 33, -108, -123, 59, 68, 39, -63, 32, -40, 22, 34, -67, 63, -41, -14, -72, 73, -108, -110, 114, 72, 68, 92, -2, 58, -31, -106, 99, -55, -116, -36, -59, -65, 122, -113, 58, 23, -23, -92, -52, 57, 48, -78, -56, -70, -65, -2, 31, -11, -79, -63, 38, 16, -125, 15, -53, 50, -128, 100, -43, 19, -124, 63, 81, 39, 34, 77, 69, 12, -124, 67, 16, 52, 89, 114, 33, 55, -100, -128, 79, -112, -34, 72, 38, 34, 84, -109, -95, 31, 73, 103, 117, 101, 96, 17, 0, 9, -64, 78, -113, 38, 119, 86, 102, -76, -63, -29, -7, -12, -103, 9, 93, 28, -125, -41, -12, -101, -88, 21, -126, -119, -74, 61, -116, 127, -20, -109, 80, 8, 9, -44, 41, -55, -116, -46, -40, 61, 115, -25, -4, -49, 70, 61, -7, 57, 51, 18, 78, 13, 96, 52, -47, -27, -1, -42, 34, 60, -7, -69, 54, 59, -69, 72, 58, 116, -73, -19, 50, 44, -35, -110, 45, -74, -75, 117, -119, -67, -36, -5, -27, 125, 127, -29, 63, 3, -113, -9, 15, -108, 104, -13, -113, 120, 20, -15, 49, 6, 112, 26, -28, 77, 29, -89, -50, -22, -26, 44, -105, 74, -40, 16, -64, -110, -30, -120, -32, -26, 0, -32, -1, -7, 96, 96, 15, -128, 0, 1, 62, 18, 24, -59, 37, 25, 24, 66, 2, 84, -91, -18, -27, 73, 68, 93, 18, -119, 9, 119, 44, 0, 19, -126, 91, -105, -66, -124, 99, 102, 2, 35, 53, 53, -111, -117, 96, 46, -39, 68, -55, 26, -29, 41, 71, 58, -84, 83, 104, 39, 84, 54, -111, -106, -98, 9, 20, 68, 99, 44, 31, 22, -8, 105, -77, 25, 19, -66, -2, 127, -23, 88, -37, 35, 75, 68, -126, -43, -58, -43, -115, -72, 124, 10, -60, -90, 39, 25, -29, -58, -100, 20, 44, 59, 30, 20, 56, 106, 13, -113, 33, 111, 103, -22, 103, 87, 115, 15, 114, 36, 53, -40, -95, -80, -120, 112, -1, -7, 96, 96, 14, -64, 0, 1, 64, 18, 20, -59, 68, 33, -112, 4, -43, 35, 105, 42, -27, 42, 1, 18, 84, 94, -106, 21, -120, 49, -6, -65, -29, -92, -114, 42, -59, -102, -34, 123, -116, 101, 40, -128, 65, -110, 99, 6, 39, 16, -92, -95, 82, -103, -62, 100, 33, 108, 16, 32, -79, -24, 98, 115, 92, -110, 7, 99, 97, 43, 10, 102, 95, 17, 67, 46, 121, 4, 48, 42, -46, -114, 113, -77, -44, 73, -32, -7, 13, 110, 72, 113, -61, 124, 122, -86, 123, 100, -61, 8, 52, -92]

我不确定这是否是完成此类任务的正确方法。有谁知道我该如何解决或迁移到新方法?

0 个答案:

没有答案