Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
修复可视化音频输入的Python代码_Python_Python 3.x_Pyaudio - Fatal编程技术网

修复可视化音频输入的Python代码

修复可视化音频输入的Python代码,python,python-3.x,pyaudio,Python,Python 3.x,Pyaudio,我在网上找到了一些将音频输入可视化为图形的代码。我已经做了一段时间了,但是我不断地得到不同的错误。如果有人能抽出时间来看看它,并帮助我使它工作,将不胜感激 import struct import wave import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np import pyaudio SAVE = 0.0 TITLE = '' WIDTH = 1280 HE

我在网上找到了一些将音频输入可视化为图形的代码。我已经做了一段时间了,但是我不断地得到不同的错误。如果有人能抽出时间来看看它,并帮助我使它工作,将不胜感激

import struct
import wave

import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import pyaudio

SAVE = 0.0
TITLE = ''
WIDTH = 1280
HEIGHT = 720
FPS = 25.0

nFFT = 512
BUF_SIZE = 4 * nFFT
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100


def animate(i, line, stream, wf, MAX_y):

  # Read n*nFFT frames from stream, n > 0
  N = max(stream.get_read_available() / nFFT, 1) * nFFT
  data = stream.read(N)
  if SAVE:
    wf.writeframes(data)

  # Unpack data, LRLRLR...
  y = np.array(struct.unpack("%dh" % (N * CHANNELS), data)) / MAX_y
  y_L = y[::2]
  y_R = y[1::2]

  Y_L = np.fft.fft(y_L, nFFT)
  Y_R = np.fft.fft(y_R, nFFT)

  # Sewing FFT of two channels together, DC part uses right channel's
  Y = abs(np.hstack((Y_L[-nFFT / 2:-1], Y_R[:nFFT / 2])))

  line.set_ydata(Y)
  return line,


def init(line):

  # This data is a clear frame for animation
  line.set_ydata(np.zeros(nFFT - 1))
  return line,


def main():

  dpi = plt.rcParams['figure.dpi']
  plt.rcParams['savefig.dpi'] = dpi
  plt.rcParams["figure.figsize"] = (1.0 * WIDTH / dpi, 1.0 * HEIGHT / dpi)

  fig = plt.figure()

  # Frequency range
  x_f = 1.0 * np.arange(-nFFT / 2 + 1, nFFT / 2) / nFFT * RATE
  ax = fig.add_subplot(111, title=TITLE, xlim=(x_f[0], x_f[-1]),
                       ylim=(0, 2 * np.pi * nFFT ** 2 / RATE))
  ax.set_yscale('symlog', linthreshy=nFFT ** 0.5)

  line, = ax.plot(x_f, np.zeros(nFFT - 1))

  # Change x tick labels for left channel
  def change_xlabel(evt):
    labels = [label.get_text().replace(u'\u2212', '')
              for label in ax.get_xticklabels()]
    ax.set_xticklabels(labels)
    fig.canvas.mpl_disconnect(drawid)
  drawid = fig.canvas.mpl_connect('draw_event', change_xlabel)

  p = pyaudio.PyAudio()
  # Used for normalizing signal. If use paFloat32, then it's already -1..1.
  # Because of saving wave, paInt16 will be easier.
  MAX_y = 2.0 ** (p.get_sample_size(FORMAT) * 8 - 1)

  frames = None
  wf = None
  if SAVE:
    frames = int(FPS * SAVE)
    wf = wave.open('temp.wav', 'wb')
    wf.setnchannels(CHANNELS)
    wf.setsampwidth(p.get_sample_size(FORMAT))
    wf.setframerate(RATE)

  stream = p.open(format=FORMAT,
                  channels=CHANNELS,
                  rate=RATE,
                  input=True,
                  frames_per_buffer=BUF_SIZE)

  ani = animation.FuncAnimation(
    fig, animate, frames,
    init_func=lambda: init(line), fargs=(line, stream, wf, MAX_y),
    interval=1000.0 / FPS, blit=True
  )

  if SAVE:
    ani.save('temp.mp4', fps=FPS)
  else:
    plt.show()

  stream.stop_stream()
  stream.close()
  p.terminate()

  if SAVE:
    wf.close()


if __name__ == '__main__':
  main()
错误是:

File "C:\Users\Tonif\Documents\computer science\sound.py", line 26,
in animate data = stream.read(N)
TypeError: integer argument expected, got float

您会遇到哪些错误?文件“C:\Users\Tonif\Documents\computer science\sound.py”,第26行,在animate data=stream.read(N)TypeError:integer参数应为,got float错误确切地告诉您问题所在。Python3 gotcha:
x/y
始终是float,如果您想要整数除法(带截断),使用
x//y
。您会遇到哪些错误?文件“C:\Users\Tonif\Documents\computer science\sound.py”,第26行,在animate data=stream.read(N)TypeError:integer参数应为,get float错误会准确地告诉您问题出在哪里。Python3的问题是:
x/y
始终是float,如果您想要整数除法(带截断),使用
x//y