-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Extract platform dependent code out of Skill #76
Open
krisgesling
wants to merge
5
commits into
21.02
Choose a base branch
from
feature/audio-hal
base: 21.02
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6d737da
Extract _get_mixer and clarify that it's ALSA
krisgesling 9cd8e11
Add HAL constructor and simple example
krisgesling af27b60
Add first pass of ALSA Audio HAL
krisgesling a484298
convert to factory consistent with other plugin services
krisgesling 1827852
Add minimal Mark 1 example
krisgesling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Copyright 2021 Mycroft AI Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
"""This is a temporary module to emulate the Hardware Abstraction Layer (HAL). | ||
|
||
It is intended to make a simpler transition from the current Volume Skill that | ||
directly interfaces with ALSA, to one that communicates changes to the HAL via | ||
the message bus. | ||
|
||
This module will be deprecated at the earliest possible moment. | ||
""" | ||
from .alsa import get_alsa_mixer | ||
from .hal import HALFactory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Copyright 2021 Mycroft AI Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
"""Provides access to the ALSA audio system. | ||
|
||
DEPRECATION NOTICE: | ||
This will shortly be moved out of the Volume Skill to the Hardware Abstraction | ||
Layer (HAL). The Volume Skill will be emitting messages to the bus, and the | ||
HAL will be responsible for managing the system state. | ||
""" | ||
|
||
from alsaaudio import Mixer, mixers as alsa_mixers | ||
|
||
from mycroft.util import LOG | ||
|
||
from .base import HAL | ||
|
||
class AlsaHAL(HAL): | ||
"""Emulate the Hardware Abstraction Layer (HAL) for ALSA. | ||
|
||
This class will be deprecated at the earliest possible moment. | ||
""" | ||
def __init__(self, settings): | ||
super().__init__(settings) | ||
self._mixer = get_alsa_mixer() | ||
|
||
def _get_volume(self, message=None): | ||
"""Get the current volume level.""" | ||
volume = min(self._mixer.getvolume()[0], self.max_volume) | ||
LOG.debug('Current volume: {}'.format(volume)) | ||
return volume | ||
|
||
def _set_volume(self, message): | ||
"""Set the volume level.""" | ||
target_volume = int(message.data['percent'] * 100) | ||
settable_volume = self.constrain_volume(target_volume) | ||
LOG.debug("Setting volume to: {}".format(settable_volume)) | ||
self._mixer.setvolume(settable_volume) | ||
success_data = {'success': True, 'volume': settable_volume} | ||
self.bus.emit(message.reply('mycroft.volume.updated', | ||
data=success_data)) | ||
return success_data | ||
|
||
def get_alsa_mixer(): | ||
LOG.debug('Finding Alsa Mixer for control...') | ||
mixer = None | ||
try: | ||
# If there are only 1 mixer use that one | ||
mixers = alsa_mixers() | ||
if len(mixers) == 1: | ||
mixer = Mixer(mixers[0]) | ||
elif 'Master' in mixers: | ||
# Try using the default mixer (Master) | ||
mixer = Mixer('Master') | ||
elif 'PCM' in mixers: | ||
# PCM is another common one | ||
mixer = Mixer('PCM') | ||
elif 'Digital' in mixers: | ||
# My mixer is called 'Digital' (JustBoom DAC) | ||
mixer = Mixer('Digital') | ||
else: | ||
# should be equivalent to 'Master' | ||
mixer = Mixer() | ||
except Exception: | ||
# Retry instanciating the mixer with the built-in default | ||
try: | ||
mixer = Mixer() | ||
except Exception as e: | ||
LOG.error('Couldn\'t allocate mixer, {}'.format(repr(e))) | ||
return mixer |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pardon the intrusion, especially for a kind of style comment, it's not very important, I'm kind of wondering why this doesn't read
And let "create" decide what to return rather than needing to switch on platform here (given that it's a HAL).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even
self.HAL = HALFactory.create(self.config_core, self.settings)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No intrusion at all - glad to have more eyes on it.
You're totally right here too - I'm thinking passing in the
self.platform
should be sufficient for now.I expect that this will change when it moves to the real HAL service, as each HAL plugin will either select their audio output service or provide their own methods that meet a minimum spec.