Skip to content

Commit

Permalink
If no input device is found, exit
Browse files Browse the repository at this point in the history
  • Loading branch information
tlecomte committed Jun 25, 2017
1 parent 627987d commit b2abab6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
31 changes: 26 additions & 5 deletions friture/audiobackend.py
Expand Up @@ -65,6 +65,8 @@ def __init__(self, logger):
self.first_channel = None
self.second_channel = None

self.stream = None

# we will try to open all the input devices until one
# works, starting by the default input device
for device in self.input_devices:
Expand Down Expand Up @@ -104,8 +106,13 @@ def get_readable_devices_list(self):
input_devices = self.get_input_devices()

raw_devices = sounddevice.query_devices()
default_input_device = sounddevice.query_devices(kind='input')
default_input_device['index'] = raw_devices.index(default_input_device)

try:
default_input_device = sounddevice.query_devices(kind='input')
default_input_device['index'] = raw_devices.index(default_input_device)
except sounddevice.PortAudioError as exception:
self.logger.push("Failed to query the default input device: %s" % (exception))
default_input_device = None

devices_list = []
for device in input_devices:
Expand Down Expand Up @@ -172,7 +179,19 @@ def get_default_output_device(self):
def get_input_devices(self):
devices = sounddevice.query_devices()

default_input_device = sounddevice.query_devices(kind='input')
# early exit if there is no input device. Otherwise query_devices(kind='input') fails
input_devices = [device for device in devices if device['max_input_channels'] > 0]

print(input_devices)

if len(input_devices) == 0:
return []

try:
default_input_device = sounddevice.query_devices(kind='input')
except sounddevice.PortAudioError as exception:
self.logger.push("Failed to query the default input device: %s" % (exception))
default_input_device = None

input_devices = []
if default_input_device is not None:
Expand Down Expand Up @@ -398,7 +417,9 @@ def get_stream_time(self):
return 0

def pause(self):
self.stream.stop()
if self.stream != None:
self.stream.stop()

def restart(self):
self.stream.start()
if self.stream != None:
self.stream.start()
4 changes: 3 additions & 1 deletion friture/settings.py
Expand Up @@ -17,14 +17,15 @@
# You should have received a copy of the GNU General Public License
# along with Friture. If not, see <http://www.gnu.org/licenses/>.

import sys
from PyQt5 import QtCore, QtWidgets
from friture.ui_settings import Ui_Settings_Dialog

no_input_device_title = "No audio input device found"

no_input_device_message = """No audio input device has been found.
Please check your audio configuration.
Friture needs at least one input device. Please check your audio configuration.
Friture will now exit.
"""
Expand All @@ -48,6 +49,7 @@ def __init__(self, parent, logger, audiobackend):
# no audio input device: display a message and exit
QtWidgets.QMessageBox.critical(self, no_input_device_title, no_input_device_message)
QtCore.QTimer.singleShot(0, self.exitOnInit)
sys.exit(1)
return

for device in devices:
Expand Down

0 comments on commit b2abab6

Please sign in to comment.