Python 2到Python 3 Struct Pack问题(Python 2 to Python 3 Struct Pack Issue)

编程入门 行业动态 更新时间:2024-10-28 02:25:03
Python 2到Python 3 Struct Pack问题(Python 2 to Python 3 Struct Pack Issue)

我试图让下面的代码在Python 3中工作。它在Python 2中很好。我已经将xrange s更改为range ,但是此行存在问题: data = ''.join(struct.pack('f', samp) for samp in tone) :

sequence item 0: expected str instance, bytes found.

我找到了这个答案,但无法解决如何将其应用于我的情况。 任何帮助非常感谢。

import math import struct import pyaudio def play_tone(frequency, amplitude, duration, fs, stream): N = int(fs / frequency) T = int(frequency * duration) # repeat for T cycles dt = 1.0 / fs # 1 cycle tone = (amplitude * math.sin(2 * math.pi * frequency * n * dt) for n in xrange(N)) # todo: get the format from the stream; this assumes Float32 data = ''.join(struct.pack('f', samp) for samp in tone) for n in xrange(T): stream.write(data) fs = 48000 p = pyaudio.PyAudio() stream = p.open( format=pyaudio.paFloat32, channels=1, rate=fs, output=True) # play the C major scale scale = [130.8, 146.8, 164.8, 174.6, 195.0, 220.0, 246.9, 261.6] for tone in scale: play_tone(tone, 0.5, 0.75, fs, stream) # up an octave for tone in scale[1:]: play_tone(2*tone, 0.5, 0.75, fs, stream) stream.close() p.terminate()

I'm trying to get the following code to work in Python 3. It is fine in Python 2. I have changed the xranges to range, but there is a problem with this line: data = ''.join(struct.pack('f', samp) for samp in tone):

sequence item 0: expected str instance, bytes found.

I found this answer but couldn't work out how to apply it to my situation. Any help much appreciated.

import math import struct import pyaudio def play_tone(frequency, amplitude, duration, fs, stream): N = int(fs / frequency) T = int(frequency * duration) # repeat for T cycles dt = 1.0 / fs # 1 cycle tone = (amplitude * math.sin(2 * math.pi * frequency * n * dt) for n in xrange(N)) # todo: get the format from the stream; this assumes Float32 data = ''.join(struct.pack('f', samp) for samp in tone) for n in xrange(T): stream.write(data) fs = 48000 p = pyaudio.PyAudio() stream = p.open( format=pyaudio.paFloat32, channels=1, rate=fs, output=True) # play the C major scale scale = [130.8, 146.8, 164.8, 174.6, 195.0, 220.0, 246.9, 261.6] for tone in scale: play_tone(tone, 0.5, 0.75, fs, stream) # up an octave for tone in scale[1:]: play_tone(2*tone, 0.5, 0.75, fs, stream) stream.close() p.terminate()

最满意答案

问题是你要加入的空字符串实际上是一个str实例(一个Unicode字符串),但是你加入到一起的值(你从struct.pack得到的)是bytes实例。 Python 3不会让你像这样混合不同的字符串类型。

改变''为b'' ,它应该工作: data = b''.join(struct.pack('f', samp) for samp in tone)

The issue is that the empty string you're joining on is in fact a str instance (a Unicode string), but the values you're joining together (that you get from struct.pack) are bytes instances. Python 3 doesn't let you mix the different string types together like that.

Change the '' to b'' and it should work: data = b''.join(struct.pack('f', samp) for samp in tone)

更多推荐

本文发布于:2023-08-01 00:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351775.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Struct   Python   Issue   Pack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!