-
Notifications
You must be signed in to change notification settings - Fork 23
Add ALSA-based volume control and test logic #8
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
Open
WasabiFan
wants to merge
7
commits into
ev3dev:ev3dev-jessie
Choose a base branch
from
WasabiFan:add-audio-controls
base: ev3dev-jessie
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
7 commits
Select commit
Hold shift + click to select a range
bf709a3
Add ALSA-based audio volume control and test logic
WasabiFan 13f2b9a
Update debian/control for audio additions
WasabiFan 240ae1e
Refine audio menu and structure
WasabiFan 55d0a87
Clean up sound code based on review feedback
WasabiFan a426fe4
Format audio code to follow spacing style conventions
WasabiFan a745576
Only show sound mixer index when nonzero
WasabiFan 982690e
More clean-up of audio code
WasabiFan 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,121 @@ | ||
| /* | ||
| * brickman -- Brick Manager for LEGO MINDSTORMS EV3/ev3dev | ||
| * | ||
| * Copyright 2016 Kaelin Laundry <wasabifan@outlook.com> | ||
| * | ||
| * This program is free software; you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation; either version 2 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program; if not, write to the Free Software | ||
| * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, | ||
| * MA 02110-1301, USA. | ||
| */ | ||
|
|
||
| /* SoundController.vala - Controller for sound volume control */ | ||
|
|
||
| using Ev3devKit.Devices; | ||
| using Ev3devKit.Ui; | ||
| using Alsa; | ||
|
|
||
| namespace BrickManager { | ||
| public class SoundController : Object, IBrickManagerModule { | ||
| const int VOLUME_STEP = 10; | ||
|
|
||
| Mixer mixer; | ||
| MixerElementSelectorWindow mixer_select_window; | ||
| MixerElementVolumeWindow volume_window; | ||
|
|
||
| public string display_name { get { return "Sound"; } } | ||
|
|
||
| private void initialize_mixer () { | ||
| mixer = null; | ||
|
|
||
| int err = Mixer.open (out mixer); | ||
| if (err != 0) { | ||
| critical ("Failed to open mixer: %s", Alsa.strerror (err)); | ||
| return; | ||
| } | ||
|
|
||
| err = mixer.attach (); | ||
| if (err != 0) { | ||
| critical ("Failed to attach mixer: %s", Alsa.strerror (err)); | ||
| return; | ||
| } | ||
|
|
||
| err = mixer.register (); | ||
| if (err != 0) { | ||
| critical ("Failed to register mixer: %s", Alsa.strerror (err)); | ||
| return; | ||
| } | ||
|
|
||
| err = mixer.load (); | ||
| if (err != 0) { | ||
| critical ("Failed to load mixer: %s", Alsa.strerror (err)); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| void create_main_window () { | ||
| mixer_select_window = new MixerElementSelectorWindow (); | ||
|
|
||
| mixer_select_window.mixer_element_selected.connect ((selected_element) => { | ||
| if (volume_window == null) { | ||
| create_volume_window (); | ||
| } | ||
|
|
||
| volume_window.current_element = selected_element; | ||
| volume_window.show_element_details = true; | ||
| volume_window.show (); | ||
| }); | ||
| } | ||
|
|
||
| void create_volume_window () { | ||
| volume_window = new MixerElementVolumeWindow (); | ||
|
|
||
| weak MixerElementVolumeWindow weak_volume_window = volume_window; | ||
| // Wire up handlers for volume window signals | ||
| volume_window.volume_up.connect (() => | ||
| weak_volume_window.current_element.volume += VOLUME_STEP); | ||
|
|
||
| volume_window.volume_down.connect (() => | ||
| weak_volume_window.current_element.volume -= VOLUME_STEP); | ||
|
|
||
| volume_window.mute.connect (() => | ||
| weak_volume_window.current_element.volume = IMixerElementViewModel.MIN_VOLUME); | ||
| } | ||
|
|
||
| public void show_main_window () { | ||
| if (mixer_select_window == null) { | ||
| create_main_window (); | ||
| } | ||
|
|
||
| // Whenever the sound item is launched from the main menu, | ||
| // repopulate the mixer list | ||
| mixer_select_window.clear_elements (); | ||
| // Re-initializing will return updated data, including volume | ||
| initialize_mixer (); | ||
| for (MixerElement element = mixer.first_elem (); element != null; element = element.next ()) { | ||
| mixer_select_window.add_element (new AlsaBackedMixerElement (element)); | ||
| } | ||
|
|
||
| if (mixer_select_window.has_single_element) { | ||
| if (volume_window == null) | ||
| create_volume_window (); | ||
|
|
||
| volume_window.current_element = mixer_select_window.first_element; | ||
| volume_window.show_element_details = false; | ||
| volume_window.show (); | ||
| } else { | ||
| mixer_select_window.show (); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,130 @@ | ||
| /* | ||
| * brickman -- Brick Manager for LEGO MINDSTORMS EV3/ev3dev | ||
| * | ||
| * Copyright (C) 2016 Kaelin Laundry <wasabifan@outlook.com> | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| /* | ||
| * MixerElementSelectorWindow.vala - Lists ALSA mixer elements | ||
| */ | ||
|
|
||
| using Ev3devKit; | ||
| using Ev3devKit.Ui; | ||
|
|
||
| namespace BrickManager { | ||
|
|
||
| public class MixerElementSelectorWindow : BrickManagerWindow { | ||
| Ui.Menu element_menu; | ||
|
|
||
| public signal void mixer_element_selected (IMixerElementViewModel selected_element); | ||
|
|
||
| public MixerElementSelectorWindow () { | ||
| title = "Volume Controls"; | ||
| element_menu = new Ui.Menu (); | ||
| content_vbox.add (element_menu); | ||
| } | ||
|
|
||
| protected string get_element_label_text (IMixerElementViewModel element) { | ||
| var builder = new StringBuilder (); | ||
| builder.append (element.name); | ||
|
|
||
| if (element.index != 0) { | ||
| builder.append_printf ("[%u]", element.index); | ||
| } | ||
|
|
||
| if (element.can_mute && element.is_muted) { | ||
| builder.append (" (muted)"); | ||
| } else { | ||
| builder.append_printf (" (%ld%%)", element.volume); | ||
| } | ||
|
|
||
| return builder.str; | ||
| } | ||
|
|
||
| protected void sort_element_menu () { | ||
| // TODO: we would get much better performance if we just inserted | ||
| // the item in the correct place instead of sorting the entire list | ||
| // each time an item is inserted. | ||
| element_menu.sort_menu_items ((a, b) => { | ||
| var element_a = a.represented_object as IMixerElementViewModel; | ||
| var element_b = b.represented_object as IMixerElementViewModel; | ||
|
|
||
| // Group by name, and sort by index within the same name | ||
| if (element_a.name == element_b.name) { | ||
| return (int)element_a.index - (int)element_b.index; | ||
| } else { | ||
| return element_a.name.ascii_casecmp (element_b.name); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public void add_element (IMixerElementViewModel element) { | ||
| var menu_item = new Ui.MenuItem (get_element_label_text (element)) { | ||
| represented_object = (Object)element | ||
| }; | ||
|
|
||
| weak IMixerElementViewModel weak_element = element; | ||
| // Update the menu item whenever the represented element changes | ||
| weak_element.notify.connect ((sender, property) => { | ||
| menu_item.label.text = get_element_label_text (weak_element); | ||
| sort_element_menu (); | ||
| }); | ||
|
|
||
| // Emit a selection signal for this element when its menu item is selected | ||
| menu_item.button.pressed.connect (() => | ||
| mixer_element_selected (weak_element)); | ||
|
|
||
| element_menu.add_menu_item (menu_item); | ||
| } | ||
|
|
||
| protected void remove_menu_item (Ui.MenuItem menu_item) { | ||
| if (menu_item != null) { | ||
| element_menu.remove_menu_item (menu_item); | ||
| } | ||
| } | ||
|
|
||
| public void remove_element (IMixerElementViewModel element) { | ||
| var item = element_menu.find_menu_item<IMixerElementViewModel> (element, (menu_item, target_element) => { | ||
| return target_element == (menu_item.represented_object as IMixerElementViewModel); | ||
| }); | ||
|
|
||
| remove_menu_item (item); | ||
| } | ||
|
|
||
| public void clear_elements () { | ||
| var iter = element_menu.menu_item_iter (); | ||
| while (iter.size > 0) { | ||
| remove_menu_item (iter[0]); | ||
| } | ||
| } | ||
|
|
||
| public bool has_single_element { | ||
| get { | ||
| return element_menu.menu_item_iter ().size == 1; | ||
| } | ||
| } | ||
|
|
||
| public IMixerElementViewModel? first_element { | ||
| get { | ||
| if (element_menu.menu_item_iter ().size <= 0) { | ||
| return null; | ||
| } | ||
|
|
||
| return element_menu.menu_item_iter ().get (0).represented_object as IMixerElementViewModel; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
There is a reference cycle here.
menu_itemhas a reference toelementinrepresented_objectandelementhas a reference tomenu_itemin the signal closure.