2

I'm trying to write a python script that produces a sine wave audio:

import pyaudio
import numpy as np

p = pyaudio.PyAudio()

volume = 0.5     # range [0.0, 1.0]
fs = 44100       # sampling rate, Hz, must be integer
duration = 1.0   # in seconds, may be float
f = 440.0        # sine frequency, Hz, may be float

# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)

# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=fs,
                output=True)

# play. May repeat with different volume values (if done interactively) 
stream.write(volume*samples)

stream.stop_stream()
stream.close()

p.terminate()

But when I try to execute in VSCode,I get:

SystemError: PY_SSIZE_T_CLEAN macro must be defined for '#' formats

I'm running Python 3.10.4

jimbob97
  • 61
  • 5
  • I tried your code and nothing wrong. Recognizing “#” as a macro is the syntax of C++, please ensure whether the file is .py file. – MingJie-MSFT Apr 20 '22 at 02:04
  • 1
    Does this answer your question? [PyAudio.write SystemError: PY\_SSIZE\_T\_CLEAN macro must be defined for '#' formats](https://stackoverflow.com/questions/70344884/pyaudio-write-systemerror-py-ssize-t-clean-macro-must-be-defined-for-format) – Keno Jun 28 '22 at 00:35

0 Answers0