简体   繁体   中英

pyaudio plays different tones in python2 vs python3

I'm just getting started with pyaudio and I wrote a simple function to play a note. However the note sounds different depending on the version of Python I'm using:

from __future__ import division
import math
import pyaudio


BITS_PER_BYTE = 8  # for clarity
SAMPLE_BIT_DEPTH = 8  # i.e. each sample is 1 byte
SAMPLES_PER_SECOND = 16000
NOTE_TIME_SECONDS = 1
MIDDLE_C_HZ = 523.3

CYCLES_PER_SECOND = SAMPLES_PER_SECOND / MIDDLE_C_HZ
NUM_SAMPLES = SAMPLES_PER_SECOND * NOTE_TIME_SECONDS


def play_note():
    audio = pyaudio.PyAudio()

    stream = audio.open(
        format=audio.get_format_from_width(SAMPLE_BIT_DEPTH / BITS_PER_BYTE),
        channels=1,
        rate=SAMPLES_PER_SECOND,
        output=True,
    )

    byte_string = str()

    for i in range(NUM_SAMPLES):
        # calculate the amplitude for this frame as a float between -1 and 1
        frame_amplitude = math.sin(i / (CYCLES_PER_SECOND / math.pi))
        # scale the amplitude to an integer between 0 and 255 (inclusive)
        scaled_amplitude = int(frame_amplitude * 127 + 128)
        # convert amplitude to byte string (ascii value)
        byte_string += chr(scaled_amplitude)

    stream.write(byte_string)
    stream.close()
    audio.terminate()


if __name__ == '__main__':
    play_note()

In Python 2.7.13 I hear the correct, clear tone. In 3.6.2 it sounds rough, like a square wave.

Why is that, and how would I fix this (or at least start to debug)?

I am on OSX v10.11.6 using portaudio v19.6.0 .

It's because you're using a str when you should be using byte s.

This works for me:

byte_array = bytearray()  # bytearray instead of str

for i in range(NUM_SAMPLES):
    frame_amplitude = math.sin(i / (CYCLES_PER_SECOND / math.pi))
    scaled_amplitude = int(frame_amplitude * 127 + 128)
    # Note the append here, not +=
    byte_array.append(scaled_amplitude)

stream.write(bytes(byte_array))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM