QEMU下虛擬機器內的音效卡模擬方法總結
一,虛擬機器內部聲音匯出到本地宿主機內
主要是針對qemu虛擬機器下的音訊如何使用,首先要檢視help命令:
qemu-system-x86_64 -audio-help
這個命令能告訴我們audio的一些基本的資訊和使用方法,以及Qemu支援的驅動
Audio options:
QEMU_AUDIO_DAC_FIXED_SETTINGS: boolean, default = 1
Use fixed settings for host DAC
QEMU_AUDIO_DAC_FIXED_FREQ: integer, default = 44100
Frequency for fixed host DAC
QEMU_AUDIO_DAC_FIXED_FMT: format, default = S16, (one of: U8 S8 U16 S16 U32 S32)
Format for fixed host DAC
QEMU_AUDIO_DAC_FIXED_CHANNELS: integer, default = 2
Number of channels for fixed DAC (1 - mono, 2 - stereo)
QEMU_AUDIO_DAC_VOICES: integer, default = 1
Number of voices for DAC
QEMU_AUDIO_DAC_TRY_POLL: boolean, default = 1
Attempt using poll mode for DAC
QEMU_AUDIO_ADC_FIXED_SETTINGS: boolean, default = 1
Use fixed settings for host ADC
QEMU_AUDIO_ADC_FIXED_FREQ: integer, default = 44100
Frequency for fixed host ADC
QEMU_AUDIO_ADC_FIXED_FMT: format, default = S16, (one of: U8 S8 U16 S16 U32 S32)
Format for fixed host ADC
QEMU_AUDIO_ADC_FIXED_CHANNELS: integer, default = 2
Number of channels for fixed ADC (1 - mono, 2 - stereo)
QEMU_AUDIO_ADC_VOICES: integer, default = 1
Number of voices for ADC
QEMU_AUDIO_ADC_TRY_POLL: boolean, default = 1
Attempt using poll mode for ADC
QEMU_AUDIO_TIMER_PERIOD: integer, default = 100
Timer period in HZ (0 - use lowest possible)
Available drivers:
Name: spice
Description: spice audio driver
One playback voice
One capture voice
Options:
Name: oss
Description: OSS http://www.opensound.com
Theoretically supports many playback voices
Theoretically supports many capture voices
Options:
QEMU_OSS_FRAGSIZE: integer, default = 4096
Fragment size in bytes
QEMU_OSS_NFRAGS: integer, default = 4
Number of fragments
QEMU_OSS_MMAP: boolean, default = 0
Try using memory mapped access
QEMU_OSS_DAC_DEV: string, default = /dev/dsp
Path to DAC device
QEMU_OSS_ADC_DEV: string, default = /dev/dsp
Path to ADC device
QEMU_OSS_EXCLUSIVE: boolean, default = 0
Open device in exclusive mode (vmix won't work)
Name: alsa
Description: ALSA http://www.alsa-project.org
Theoretically supports many playback voices
Theoretically supports many capture voices
Options:
QEMU_ALSA_DAC_SIZE_IN_USEC: boolean, default = 0
DAC period/buffer size in microseconds (otherwise in frames)
QEMU_ALSA_DAC_PERIOD_SIZE: integer, default = 1024
DAC period size (0 to go with system default)
QEMU_ALSA_DAC_BUFFER_SIZE: integer, default = 4096
DAC buffer size (0 to go with system default)
QEMU_ALSA_ADC_SIZE_IN_USEC: boolean, default = 0
ADC period/buffer size in microseconds (otherwise in frames)
QEMU_ALSA_ADC_PERIOD_SIZE: integer, default = 0
ADC period size (0 to go with system default)
QEMU_ALSA_ADC_BUFFER_SIZE: integer, default = 0
ADC buffer size (0 to go with system default)
QEMU_ALSA_THRESHOLD: integer, default = 0
(undocumented)
QEMU_ALSA_DAC_DEV: string, default = default
DAC device name (for instance dmix)
QEMU_ALSA_ADC_DEV: string, default = default
ADC device name
Name: sdl
Description: SDL http://www.libsdl.org
One playback voice
Does not support capture
Options:
QEMU_SDL_SAMPLES: integer, default = 1024
Size of SDL buffer in samples
Name: none
Description: Timer based audio emulation
Theoretically supports many playback voices
Theoretically supports many capture voices
No options
Name: wav
Description: WAV renderer http://wikipedia.org/wiki/WAV
One playback voice
Does not support capture
Options:
QEMU_WAV_FREQUENCY: integer, default = 44100
Frequency
QEMU_WAV_FORMAT: format, default = S16, (one of: U8 S8 U16 S16 U32 S32)
Format
QEMU_WAV_DAC_FIXED_CHANNELS: integer, default = 2
Number of channels (1 - mono, 2 - stereo)
QEMU_WAV_PATH: string, default = qemu.wav
Path to wave file
Options are settable through environment variables.
Example:
export QEMU_AUDIO_DRV=wav
export QEMU_WAV_PATH=$HOME/tune.wav
(for csh replace export with setenv in the above)
qemu ...
可以看到audio的很多操作,也可以設定Audio的引數,通過export來設定,我們可以看到,支援的驅動有spice,oss,alsa,sdl, none, wav,每種驅動都給出了相應的操作方法,並且還給出了Example:
如果你是本地啟動虛擬機器二沒有通過遠端spice連線的話那麼可以如下:
#在啟動qemu虛擬機器命令前執行,修改下面的環境變數
export QEMU_AUDIO_DRV=wav #audio的驅動方式為wav
export QEMU_WAV_PATH=$HOME/tune.wav #把所有虛擬機器下的聲音儲存到tune.wav內,$HOME改寫為你的絕對路徑如:/home/tune.wav
這樣你的所有在虛擬機器下的聲音統統都匯入到了你的宿主機的檔案內了,這個方法缺點是是無法在虛擬機器內真正的發出聲音或者錄音,只有通過本地開啟的wav檔案才能收聽。
二.通過soundhw來模擬音效卡
這個方法也很簡單,首先就是檢視支援的驅動
qemu-system-x86_64 -soundhw ? #檢視支援的驅動
檢視結果如下:
可以看到支援的驅動有ac97,hda等,我們使用ac97或者hda,或者all,all是最好的就是使能所有的驅動,使用的方式如下:
在你的啟動qemu-system-x86_64的命令引數內加上下面任意一個就可以了
-soundhw ac97 #或者
-soundhw hda #或者
-soundhw all
三.使用device新增模擬裝置
如果你做了一些穿透的工作如,VGA或者usb的穿透,那麼上面的方法二就無法使用了,因為會報如下錯誤,說PCI已經被使用了,無法使用,因此上面的方法是沒辦法使用的,如下圖所示:
因此我們只能使用-device這個引數來模擬了
要想使用這種方法來模擬,必須對qemu這個庫進行重新配置編譯,編譯的選項如下
./configure --target-list=x86_64-softmmu --enable-kvm \
--enable-gtk --with-gtkabi=3.0 --enable-virtfs \
--enable-tcg-interpreter --enable-xfsctl \
--enable-libiscsi --enable-glusterfs --enable-snappy \
--enable-lzo --enable-rdma --enable-linux-aio \
--enable-vhost-net --enable-spice --enable-smartcard \
--enable-libusb --enable-usb-redir --enable-guest-agent \
--enable-libssh2 \
--enable-sdl --audio-drv-list='oss alsa sdl'
看到最後一行,就是我們需要的,新增上audio驅動列表audio-drv-list,配置結果可以檢視到下圖:
使能了sdl和audio 驅動列表。
接下來我們就可以通過-device來查詢支援的裝置了,查詢命令如下:
qemu-system-x86_64 -device ? #查詢支援新增的裝置
我們在打印出的很多內容中找到Hub devices,USB devices ,Storage devices ,Network devices , Input devices , Display devices , Sound devices , Misc devices , CPU devices , Uncategorized devices ,而我們只需要檢視Sound devices如下:
因此我們只需要在qemu啟動虛擬機器的命令內新增引數如下:
-device AC97 #這個驅動效果是做好的
-device intel-hda #這個效果很不好
-device usb-audio #這個下過沒測試過
那麼我們在執行qemu啟動虛擬機器命令是會報出一個錯誤
#在使用的過程中預設使用oss但是啟動指令碼是會報錯:無法初始化OSS
audio: Could not init `oss' audio driver
這個錯誤無關緊要,不會影響模擬的效果,但是看著就不爽,下面就是解決方法:
# 修改驅動為alsa就可以了
export QEMU_AUDIO_DRV=alsa
到此音效卡的模擬也就總結完畢了