Python Forum
Python 2 to Python 3 Struct Issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 2 to Python 3 Struct Issue
#1
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):

Error:
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()
Reply
#2
This answer might be more helpful to you.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
what is data supposed to look like? (a small sample)
Reply
#4
You have the answer in the link of stack overflow...
The problem is that struct returns a string in python2 and a bytes array in python3. So when str.join tries to concatenate the bytes complains about it.
The solution is just transform the string operations to bytes operations, and change the line 13 of your script for:
# Notice the b to transform the operation in a bytes operation
data = b''.join(struct.pack('f', samp) for samp in tone)
Reply
#5
You could have saved yourself some data manipulations if you used array
import array
data = array.array('f', tone).tostring()

And instead of
    for n in xrange(T):
        stream.write(data)
just
    stream.write(data * T)
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#6
Great, thanks everyone. This is now working a treat.
One little detail that would be icing on the cake - why does the very last note of any sequence I give this code clip in an unpleasant way? Is there a (simple) way to smooth this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with uninstalling Python that is driving me crazy traxus 0 221 Mar-30-2024, 04:37 PM
Last Post: traxus
  Facing issue in python regex newline match Shr 6 1,288 Oct-25-2023, 09:42 AM
Last Post: Shr
  Python C Extension Module loading issue on Cygwin mesibo 0 650 Sep-22-2023, 05:41 AM
Last Post: mesibo
  Python Struct Question, decimal 10, \n and \x0a. 3python 2 666 Aug-11-2023, 09:29 AM
Last Post: 3python
  Python venv and PIP version issue JanOlvegg 2 1,264 Feb-22-2023, 02:22 AM
Last Post: JanOlvegg
  Python List Issue Aggie64 5 1,616 Jun-30-2022, 09:15 PM
Last Post: Aggie64
  Issue while using ctypes in python GiggsB 6 2,777 Mar-27-2022, 03:38 AM
Last Post: GiggsB
  MAC Python IDLE issue shadow12 4 2,544 Oct-29-2021, 12:22 AM
Last Post: shadow12
Big Grin Newbie to Python - input and branching issue Sherine 3 2,238 Jul-31-2021, 01:09 AM
Last Post: Pedroski55
  Python issue - Data science - Help is needed yovel 2 2,007 Jul-29-2021, 04:27 PM
Last Post: yovel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020