私は以下のように書かれたコードを持っています。それは完璧に動作します。マイクの出力をテキストファイルに保存したいと思います。
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16,
channels=18,
rate=44100,
input=True,
frames_per_buffer=1024,
output_device_index=1)
for i in range(0, 1000):
data = stream.read(CHUNK)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
target = open('target.txt', 'w')
target.write(repr(frames))
target.close()
以下に示すように、出力をテキストファイルに保存します。(コンマで区切られた str 要素を持つリスト。
Ex - ['' , '' , ''].
['\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc9\x00\xb2\xfe\x82\x02\xf7\ts\xfen\x00i\xff\xeb\xff\xa9\xff\xed\xff\xc5\xff\xd1\xff\xcd\xff\xaf\xff\xde\xff\xb8\xff\xc9\xff\xb0\xff\xca\xff\xd1\xff\xc9\xff\xcb\xff\xdf\xff\xdb\xff\xe2\xff\xe6\xff\xd9\xff\xf8\xff\xe1\xff\xf6\xff\xf1\xff\xd8\xff\xe1\xff\xe4\xff\xd4\xff\xdd\xff\xef\xff\xef\xff\xdc\xff\xd2\xff\xd6\xf ....]
受信側では、このファイルを転送します。
受信者がテキスト ファイルを開きます。eval () 関数を使用してコンテンツを変換します。
with open("targetRec.txt",'r') as inf:
Rnewdiffdict = eval(inf.read())
inf.Read() は string object を返します。戻りリスト オブジェクトを評価します。次のコードは、リストを wave ファイルに書き込みます。
wf = wave.open("recaudio.wav", 'wb')
wf.setnchannels(int(recmetadata[0]))
wf.setsampwidth(int(recmetadata[2]))
wf.setframerate(int(recmetadata[3]))
wf.writeframes(b''.join(Rnewdiffdict))
# Write frames in wave file .
wf.close() # Close wave file.
ここで、送信者側で、送信中に \x を '' に置き換えたいと考えています。テキストファイルのサイズを小さくすることができます。
target.write(repr(frames).replace('\\x',' '))
受信側で ' ' を \x に置き換えて、送信側で置換操作を行う前のファイルを再作成したいと考えています。
Rnewdiffdict = eval(inf.read().replace(' ','\\x'))
エラーが発生し、プログラムがハングします。
Traceback (most recent call last):
File "I:\Users\Administrator\Desktop\read wave.py", line 239, in <module>
ReceiveAudio()
File "I:\Users\Administrator\Desktop\read wave.py", line 101, in ReceiveAudio
Rnewdiffdict = eval(inf.read().replace(' ','\\x'))
File "<string>", line 1