如何在显示文本的同时在 Python 2.6.6 中播放音频?

发布于 2024-11-09 03:03:27 字数 442 浏览 4 评论 0原文

我正在尝试使用 Python 2.6.6 制作一个程序,在后台播放音频的同时显示文本。我只完成了部分文字。

print "Well here we are again"
print "It’s always such a pleasure"
print "Remember when you tried to kill me twice?"
print "Oh, how we laughed and laughed"
print "Except I wasn’t laughing"
print "Under the circumstances I’ve been shockingly nice."

我有一个 .wav 文件,我想在程序开始时播放它。我还希望文本与音乐一起播放(我可以指定文本在歌曲中显示的时间,例如 00:00:02)。我认为这可以通过某种音频模块来实现。

谢谢!

I am trying to make a program with Python 2.6.6 that displays text while having audio playing in the background. I have only completed some of the text.

print "Well here we are again"
print "It’s always such a pleasure"
print "Remember when you tried to kill me twice?"
print "Oh, how we laughed and laughed"
print "Except I wasn’t laughing"
print "Under the circumstances I’ve been shockingly nice."

I have a .wav file that I would like to play at the beginning of the program. I would also like the text to play in cue with the music (I would be able to specify when the text displays during the song such as 00:00:02). I assume this would be possible with an audio module of some sort.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄风 2024-11-16 03:03:27

我最近做了类似的事情并使用了audiere模块

import audiere
ds = audiere.open_device()
os = ds.open_array(input, fs)
os.play()

这将打开第一个可用的音频设备,因为你在Windows上它可能是DirectSound。 input 只是一个 numpy 数组,fs 是采样频率(因为输入是原始数组,您需要指定它)。 os.play() 是一个非阻塞调用,因此您可以打印 txt 或同时执行任何您需要执行的操作,还有其他方法可以暂停/停止等。要播放其他文件类型我只是先将它们转换为 wav 。

以下是我解压 wav 文件的方法

def wave_unpack(fname):
  """
  input: wave filename as string 
  output: left, right, params

  unpacks a wave file and return left and right channels as arrays
  (in case of a mono file, left and right channels will be copies)

  params returns a tuple containing:
  -number of audio channels (1 for mono, 2 for stereo)
  -sample width in bytes
  -sampling frequency in Hz
  -number of audio frames
  -compression type
  -compression name
  """
  import sndhdr, os, wave, struct
  from scipy import array
      assert os.path.isfile(fname), "file location must be valid"
  assert sndhdr.what(fname)[0] == 'wav', "file must have valid header"
  try:
    wav = wave.open(fname)
    params = (nchannels,sampwidth,rate,nframes,comp,compname) = wav.getparams()
    frames = wav.readframes(nframes*nchannels)
  finally:
    wav.close()
  out = struct.unpack_from ("%dh" % nframes*nchannels, frames)
  if nchannels == 2:
    left = array(out[0::2])
    right = array(out[1::2])
  elif nchannels == 1:
    right = left = array(out)
  else:
    assert 0, "number of channels must be 1 or 2"
  return left, right, params

例如,要制作 inputfs 你可以这样做:

from scipy import c_
left, right, params = wave_unpack(fn)
fs = params[2]
left_float  =  left.astype('f')/2**15
right_float = right.astype('f')/2**15
stereo = c_[left_float, right_float]
input = mono = stereo.mean(1)

这适合我,但我的要求是 FFT 输入,而不是卡拉 OK :)

我'我确信只要提供一个 2-dim 数组,audiere 就能实现立体声播放。

I did a similar thing recently and used the audiere module

import audiere
ds = audiere.open_device()
os = ds.open_array(input, fs)
os.play()

This will open the first available audio device, since you're on windows it's probably DirectSound. input is just a numpy array, fs is the sampling frequency (since the input is a raw array you'll need to specify that). os.play() is a non-blocking call so you can print your txt or whatever you need to do at the same time, there are other methods to pause/stop etc. To play other file types I simply converted them to wav first.

Here's how I unpacked the wav file

def wave_unpack(fname):
  """
  input: wave filename as string 
  output: left, right, params

  unpacks a wave file and return left and right channels as arrays
  (in case of a mono file, left and right channels will be copies)

  params returns a tuple containing:
  -number of audio channels (1 for mono, 2 for stereo)
  -sample width in bytes
  -sampling frequency in Hz
  -number of audio frames
  -compression type
  -compression name
  """
  import sndhdr, os, wave, struct
  from scipy import array
      assert os.path.isfile(fname), "file location must be valid"
  assert sndhdr.what(fname)[0] == 'wav', "file must have valid header"
  try:
    wav = wave.open(fname)
    params = (nchannels,sampwidth,rate,nframes,comp,compname) = wav.getparams()
    frames = wav.readframes(nframes*nchannels)
  finally:
    wav.close()
  out = struct.unpack_from ("%dh" % nframes*nchannels, frames)
  if nchannels == 2:
    left = array(out[0::2])
    right = array(out[1::2])
  elif nchannels == 1:
    right = left = array(out)
  else:
    assert 0, "number of channels must be 1 or 2"
  return left, right, params

So for example to make input and fs you could go:

from scipy import c_
left, right, params = wave_unpack(fn)
fs = params[2]
left_float  =  left.astype('f')/2**15
right_float = right.astype('f')/2**15
stereo = c_[left_float, right_float]
input = mono = stereo.mean(1)

This suited me but my requirements were for FFT input, not karaoke :)

I'm sure audiere has stereo playback just by giving a 2-dim array.

画中仙 2024-11-16 03:03:27

您可能会发现 pygame 有帮助。

http://www.pygame.org/docs/ref/mixer.html

You may find pygame helpful.

http://www.pygame.org/docs/ref/mixer.html

想你的星星会说话 2024-11-16 03:03:27

您需要的是适用于 Python 的 GStreamer。

查看教程,这是一个很好的起点。

编辑:
在Windows中,您可以使用标准库中的winsound模块(哇!Python有这个!)请参阅winsound 文档。

What you need is the GStreamer for Python.

Check this tutorial, it's a good place to start.

EDIT:
In Windows you can use the winsound module from standard library (Whoa! Python has that!) See winsound docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文