-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
audio_buffer.py
executable file
·103 lines (82 loc) · 4.47 KB
/
audio_buffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
#####################################################################
# This script presents how to read and use the sound buffer.
# This script stores a "basic_sounds.wav" file of recorded audio.
# Note: This requires scipy library
#####################################################################
import os
from random import choice
from time import sleep
import numpy as np
from scipy.io import wavfile
import vizdoom as vzd
if __name__ == "__main__":
game = vzd.DoomGame()
# Load config of the basic scenario
game.load_config(os.path.join(vzd.scenarios_path, "basic.cfg"))
# Turns on the audio buffer. (turned off by default)
# If this is switched on, the audio will stop playing on device, even with game.set_sound_enabled(True)
# Setting game.set_sound_enabled(True) is not required for audio buffer to work.
# Note: This requires OpenAL library to be installed on your system.
# It is installed by default on many Linux desktop distros.
# And it can be installed from package manager, see: https://vizdoom.farama.org/introduction/python_quickstart/#audio-buffer-requirements
AUDIO_BUFFER_ENABLED = True
game.set_audio_buffer_enabled(AUDIO_BUFFER_ENABLED)
# Set the sampling rate used in the observation window. Has to be one from:
# - vzd.SamplingRate.SR_44100 (default)
# - vzd.SamplingRate.SR_22050
# - vzd.SamplingRate.SR_11025
# Remember to also set audio saving code at the bottom to use same sampling rate!
game.set_audio_sampling_rate(vzd.SamplingRate.SR_22050)
# When using frameskip (`tics` parameter of the `make_actions` function),
# we would only get the latest "frame" of audio (1/35 seconds).
# With this function you can set how many last "frames" of audio will be stored in audio buffer.
# Note that if you use larger frameskip than size of audio buffer you will lost some information about the audio.
# If you use frameskip smaller than size of audio buffer, some audio information will overlap.
frameskip = 4
game.set_audio_buffer_size(frameskip)
# This could fix "BiquadFilter_setParams: Assertion `gain > 0.00001f' failed" issue
# or "no audio in buffer" issue caused by a bug in OpenAL version 1.19.
# game.add_game_args("+snd_efx 0")
# Initialize the game. Further configuration won't take any effect from now on.
try:
game.init()
except Exception:
print(
"[ERROR] Could not launch ViZDoom. If you see an error above about BiquadFilter and gain,\n"
" try setting game.add_game_args('+snd_efx 0'). If that fails, see\n"
" https://github.com/Farama-Foundation/ViZDoom/pull/486"
)
exit(1)
actions = [[True, False, False], [False, True, False], [False, False, True]]
sleep_time = 1.0 / vzd.DEFAULT_TICRATE # = 0.028
episodes = 3
audio_slices = []
for i in range(episodes):
print(f"Episode #{i + 1}")
game.new_episode()
while not game.is_episode_finished():
# Gets the state
state = game.get_state()
audio_buffer = state.audio_buffer
audio_slices.append(audio_buffer)
# Makes a random action and get remember reward.
r = game.make_action(choice(actions), frameskip)
if not AUDIO_BUFFER_ENABLED:
sleep(sleep_time * frameskip)
game.close()
if AUDIO_BUFFER_ENABLED:
# Check that we have audio (having no audio is a common bug, see
# https://github.com/mwydmuch/ViZDoom/pull/486
audio_data = np.concatenate(audio_slices, axis=0)
if audio_data.max() == 0:
print(
"[WARNING] Audio buffers were full of silence. This is a common bug on e.g. Ubuntu 20.04\n"
" See https://github.com/Farama-Foundation/ViZDoom/pull/486\n"
" There are some possible fixes:\n"
" 1) Check that you have OpenAL installed, if not install, see: https://vizdoom.farama.org/introduction/python_quickstart/#audio-buffer-requirements\n"
" 2) Try setting game.add_game_args('+snd_efx 0'). This my disable some audio effects\n"
" 3) Try installing a newer version of OpenAL Soft library, see https://github.com/Farama-Foundation/ViZDoom/pull/486#issuecomment-889389185"
)
# Save audio file
wavfile.write("basic_sounds.wav", 22050, np.concatenate(audio_slices, axis=0))