Skip to content
This repository has been archived by the owner on Jun 30, 2019. It is now read-only.

声卡选择 #15

Closed
dissipator opened this issue Jun 24, 2018 · 17 comments · Fixed by #18
Closed

声卡选择 #15

dissipator opened this issue Jun 24, 2018 · 17 comments · Fixed by #18

Comments

@dissipator
Copy link

这个可以选择播放的声卡吗?怎么操作呢?

@cosven
Copy link
Owner

cosven commented Jun 25, 2018

目前还没有考虑 “选择声卡” 的功能。
不过乍一听,感觉操作系统自身应该会提供这个功能。 🤔

@dissipator
Copy link
Author

我在树莓派上的tty1下使用,调了默认声卡,也切换不过去;能像aplay -D一样选择声卡吗?

@cosven
Copy link
Owner

cosven commented Jul 3, 2018

ummm,这个项目的音乐播放是基于 (libmpv) mpv,由于我这边没有树莓派,有些测试我这边也不是很方便进行。

你可以先测试一下 mpv 在树莓派上能不能正常播放音乐?能不能选择声卡?如果可以的话,我们应该也能让这个项目支持相关功能。

@dissipator
Copy link
Author

就是不能选择声卡,每次调用的都是系统的[bcm2835 ALSA]我想用的是[seeed-2mic-voicecard]。
下面是我的声卡信息
card 0: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 []
Subdevices: 0/1
Subdevice #0: subdevice #0
card 1: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 7/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
card 1: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0

@cosven
Copy link
Owner

cosven commented Jul 6, 2018

你可以先测试一下: mpv 是否支持选择一个声卡播放音乐?

具体的测试方法:

mpv --audio-device=help  # List of detected audio devices

mpv --audio-device=xxx filepath  # 指定为你的声卡,播放音乐

如果这个测试是成功的,我们或许也就有办法让 feeluown-core 支持类似的配置

@dissipator
Copy link
Author

List of detected audio devices:
'auto' (Autoselect device)
'pulse/alsa_output.platform-soc_audio.analog-stereo' (bcm2835 ALSA Analog Stereo)
'pulse/alsa_output.platform-soc_sound.analog-stereo' (seeed-2mic-voicecard Analog Stereo)
'alsa' (Default (alsa))
'alsa/playback' (playback)
'alsa/capture' (capture)
'alsa/dmixed' (dmixed)
'alsa/array' (array)
'alsa/plughw:CARD=ALSA,DEV=0' (bcm2835 ALSA, bcm2835 ALSA/Hardware device with all software conversions)
'alsa/plughw:CARD=ALSA,DEV=1' (bcm2835 ALSA, bcm2835 IEC958/HDMI/Hardware device with all software conversions)
'alsa/plughw:CARD=seeed2micvoicec,DEV=0' (seeed-2mic-voicecard, /Hardware device with all software conversions)
'jack' (Default (jack))
'sdl' (Default (sdl))
'sndio' (Default (sndio))


'alsa/plughw:CARD=seeed2micvoicec,DEV=0' (seeed-2mic-voicecard, /Hardware device with all 

@dissipator
Copy link
Author

mpv --audio-device=alsa/plughw:CARD=seeed2micvoicec,DEV=0 2.mp3
没有问题,可以播放

@cosven
Copy link
Owner

cosven commented Jul 17, 2018

棒!我这周看看怎么支持这个东西 ~

@dissipator
Copy link
Author

谢谢,用专用声卡的,效果是要nice一些,谢谢了。我在树莓派上和ubuntu上使用的时候,修改了一点源代码,才用起来的

@dissipator
Copy link
Author

你能给我说,改哪里吗?或者有没有可参考的资料。我会一点python。

@cosven
Copy link
Owner

cosven commented Jul 26, 2018

大概的思路是这样子

from mpv import _mpv_get_property_string
from mpv import _mpv_set_property_string

player = MPV()
url = ''  # 给一个可以播放的 url
player.play(url)

# 可以查看当前 audio device,默认应该是 `auto`
_mpv_get_property_string(player.handle, b'audio-device')
# 可以看到所有的 audio device
_mpv_get_property_string(player.handle, b'audio-device-list')
# 设置 audio device,这时候注意听声音是否正常
_mpv_set_property_string(player.handle, b'audio-device', b'xxx') 

参考的资料主要是:

  1. mpv player 文档: https://mpv.io/manual/stable/#command-interface-audio-device-list
  2. mpv.py 源码 https://github.com/cosven/feeluown-core/blob/master/mpv.py

对应的 feeluown-core 代码的地方是: https://github.com/cosven/feeluown-core/blob/master/fuocore/core/player.py#L295


之后我想把这个设置加入到命令参数设置中去,大概类似:

fuo -d  # 原来是这样启动
fuo -d --mpv-audio-device=xxx  # 之后支持启动时给 mpv 播放器配置参数 --mpv-xx=value

@dissipator
Copy link
Author

已经完成了

@dissipator
Copy link
Author

dissipator commented Jul 28, 2018

main.py

def main():
    debug = '--debug' in sys.argv
    setup_logger(debug=debug)
    logger = logging.getLogger(__name__)
    logger.info('{} mode.'.format('Debug' if debug else 'Release'))

    import re
    audio = ''
    for arg in sys.argv:
        matchobj = re.match('--mpv-audio-device=',arg)
        if matchobj:
            print(matchobj.group())
            audio = bytes(arg[19:],'utf-8')
            print(audio)
            break

    pubsub_gateway, pubsub_server = run_pubsub()  # runs in another thread

    app = App()
    app.initialize(audio)

@dissipator
Copy link
Author

dissipator commented Jul 28, 2018

`main.py

class App(object):
    def __init__(self):
        self.player = MpvPlayer()
        self.playlist = self.player.playlist
        self.source = Source()

    def initialize(self,audio=''):
        self.player.initialize(audio)

@dissipator
Copy link
Author

dissipator commented Jul 28, 2018

player.py

   from mpv import MPV, MpvEventID, MpvEventEndFile, _mpv_set_property_string
 ........
 .......  
  def initialize(self,audio=''):
        self._mpv.observe_property(
            'time-pos',
            lambda name, position: self._on_position_changed(position)
        )
        self._mpv.observe_property(
            'duration',
            lambda name, duration: self._on_duration_changed(duration)
        )
        if audio: _mpv_set_property_string(self._mpv.handle, b'audio-device', audio)

@cosven
Copy link
Owner

cosven commented Jul 28, 2018

赞,我今天晚上集成一下 ~
改成明早 =。=

@cosven
Copy link
Owner

cosven commented Jul 30, 2018

最新代码应该可以支持了 --mpv-audio-device 的选项 ~

(过一两天,会给这个包发个新的 alpha 版本

@cosven cosven reopened this Jul 30, 2018
@cosven cosven closed this as completed May 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants