我已经建立了一个简单的web应用程序,它应该测试用户的听力频率范围。它是在JustPy框架中构建的,它使用pysinewave (基于sounddevice)连续生成声音。我已经将它部署在Google上的一个Docker容器中,网站部分工作得很好,但是产生声音的部分引起了问题。下面是调试器输出的一部分:
500 Server Error
PortAudioError: Error querying device -1
Traceback
File /usr/local/lib/python3.10/site-packages/sounddevice.py, line 564, in query_devices ‒
561. device = _get_device_id(device, kind, raise_on_error=True)
562. info = _lib.Pa_GetDeviceInfo(device)
563. if not info:
564. raise PortAudioError('Error querying device {}'.format(device))
565. assert info.structVersion == 2
566. name_bytes = _ffi.string(info.name)
567. try:
File /usr/local/lib/python3.10/site-packages/sounddevice.py, line 2654, in _get_stream_parameters ‒
2651. samplerate = default.samplerate
2652.
2653. device = _get_device_id(device, kind, raise_on_error=True)
2654. info = query_devices(device)
2655. if channels is None:
2656. channels = info['max_' + kind + '_channels']
2657. try:
File /usr/local/lib/python3.10/site-packages/sounddevice.py, line 811, in __init__ ‒
808. samplerate = isamplerate
809. else:
810. parameters, self._dtype, self._samplesize, samplerate = \
811. _get_stream_parameters(kind, device, channels, dtype, latency,
812. extra_settings, samplerate)
813. self._device = parameters.device
814. self._channels = parameters.channelCount
File /usr/local/lib/python3.10/site-packages/sounddevice.py, line 1488, in __init__ ‒
1485. Stream, RawOutputStream
1486.
1487.
1488. _StreamBase.__init__(self, kind='output', wrap_callback='array',
1489. **_remove_self(locals()))
1490.
1491. def write(self, data):
File /usr/local/lib/python3.10/site-packages/pysinewave/sinewave.py, line 21, in __init__ ‒
18. samplerate=samplerate)
19.
20. # Create the output stream
21. self.output_stream = sd.OutputStream(channels=1, callback= lambda *args: self._callback(*args),
22. samplerate=samplerate)
23.
24. def _callback(self, outdata, frames, time, status):
File /app/./soundgen.py, line 7, in __init__ ‒
4. class SoundGen(SineWave):
5.
6. def __init__(self, freq, vol):
7. super().__init__(pitch=9, pitch_per_second=10000, decibels=-30, decibels_per_second=10000)
8. self.set_frequency(freq)
9. self.set_volume(vol)
10. self.volume = self.sinewave_generator.amplitude
File /app/./main.py, line 23, in serve ‒
20. def serve(cls, req):
21.
22. # sine wave generator
23. sound = SoundGen(440, 0.15)
24.
25. freq_dict = cls.gen_freq_dict()
26.我已经在Dockerfile中安装了以下命令的附加库(基于this thread中的答案):
RUN apt-get update
RUN apt-get install libasound-dev libportaudio2 libportaudiocpp0 portaudio19-dev -y但很明显,这还不够。我是缺少了一个重要的库才能工作,还是这种功能--通过托管在远程服务器上的web应用程序在用户浏览器中生成声音--是不可能以这种方式实现的?
重申:应用程序在本地运行时运行非常好,使用JustPy生成的前端部分(禁用声音功能)在Google上部署时也可以正常工作。
如能提出建议,我将不胜感激。
发布于 2022-04-11 19:36:41
在我看来,您想要部署一个在用户设备上播放音频的web应用程序。当您在云上托管应用程序时,运行应用程序的服务器将是后端,您在其中使用了SoundGen类。当您触发它来发出声音时,它会搜索音频设备--这在云服务器中是不可用的,因此它会抛出异常。
在这种情况下,当您在计算机上承载您的应用程序,对接运行时能够访问您的音频外设,并播放您想要的声音。相反,您可能要做的是将声音生成移出后端代码,进入前端部分。触发(如)单击),只使用前端代码在客户端设备上播放您想要的声音--浏览器。不幸的是,我不熟悉JustPy,所以我无法为您编写示例代码,但希望这仍然有帮助。
https://stackoverflow.com/questions/71833225
复制相似问题