Merge ~kissiel/plainbox-provider-snappy:set-volume-in-alsa-test into plainbox-provider-snappy:master

Proposed by Maciej Kisielewski
Status: Merged
Approved by: Chris Wayne
Approved revision: 8ce5f92c38edb6916ac89a8b074a7d6050fc9742
Merged at revision: 030106d2b0984ed7792dca2a042e00b80c765ad9
Proposed branch: ~kissiel/plainbox-provider-snappy:set-volume-in-alsa-test
Merge into: plainbox-provider-snappy:master
Diff against target: 69 lines (+51/-0)
1 file modified
src/alsa_test.cpp (+51/-0)
Reviewer Review Type Date Requested Status
Chris Wayne (community) Needs Fixing
Sylvain Pineau (community) Approve
Review via email: mp+369412@code.launchpad.net

Description of the change

alsa_test: set playback and capture volumes when starting the test

This should improve the fails on some device that default to muted or low gain capture.

If any of the mixer shenanigans fails the test plows on, so this should be "backwards compatible".

To post a comment you must log in.
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

LGTM

review: Approve
Revision history for this message
Chris Wayne (cwayne) wrote :

Doesnt fix the issue on Dawson-j

review: Needs Fixing
Revision history for this message
Maciej Kisielewski (kissiel) wrote :

> Doesnt fix the issue on Dawson-j

This patch is a general improvement, that did not fix the problem on that particular device, while it fixes a problem where the mixer state would prevent the testing.

Also from what I can tell it doesn't break anything, so I would like to land it.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/src/alsa_test.cpp b/src/alsa_test.cpp
2index e536f82..b5f4bd6 100644
3--- a/src/alsa_test.cpp
4+++ b/src/alsa_test.cpp
5@@ -266,6 +266,46 @@ template<>
6 snd_pcm_format_t Alsa::Pcm<uint8_t>::get_alsa_format() {
7 return SND_PCM_FORMAT_U8;
8 }
9+
10+struct Mixer {
11+ Mixer(string card_name, string mixer_name) {
12+ int res;
13+ res = snd_mixer_open(&mixer_handle, 0);
14+ if (res < 0) throw AlsaError("Failed to open an empty Mixer");
15+ res = snd_mixer_attach(mixer_handle, card_name.c_str());
16+ if (res < 0) throw AlsaError("Failed to attach HCTL to a Mixer");
17+ res = snd_mixer_selem_register(mixer_handle, NULL, NULL);
18+ if (res < 0) throw AlsaError("Failed to register a Mixer");
19+ res = snd_mixer_load(mixer_handle);
20+ if (res < 0) throw AlsaError("Failed to load a Mixer");
21+ snd_mixer_selem_id_alloca(&sid);
22+ snd_mixer_selem_id_set_index(sid, 0);
23+ snd_mixer_selem_id_set_name(sid, mixer_name.c_str());
24+ elem = snd_mixer_find_selem(mixer_handle, sid);
25+ if (!elem) throw AlsaError(mixer_name + " mixer not found.");
26+ }
27+ ~Mixer() {
28+ snd_mixer_close(mixer_handle);
29+ }
30+ void set_all_playback_volume(float volume) {
31+ long min, max;
32+ snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
33+ int new_vol = int(float(max) * volume);
34+ snd_mixer_selem_set_playback_switch_all(elem, 1);
35+ snd_mixer_selem_set_playback_volume_all(elem, new_vol);
36+ }
37+ void set_all_capture_volume(float volume) {
38+ long min, max;
39+ snd_mixer_selem_get_capture_volume_range(elem, &min, &max);
40+ int new_vol = int(float(max) * volume);
41+ snd_mixer_selem_set_capture_switch_all(elem, 1);
42+ snd_mixer_selem_set_capture_volume_all(elem, new_vol);
43+ }
44+private:
45+ snd_mixer_t *mixer_handle;
46+ snd_mixer_selem_id_t *sid;
47+ snd_mixer_elem_t* elem;
48+};
49 }; //namespace Alsa
50
51 template<class storage_type>
52@@ -359,6 +399,17 @@ int main(int argc, char *argv[]) {
53 if (std::find(args.begin(), args.end(), std::string("-v")) != args.end()) {
54 logger.set_level(Logger::Level::info);
55 }
56+ try {
57+ auto playback_mixer = Alsa::Mixer("default", "Master");
58+ auto capture_mixer = Alsa::Mixer("default", "Capture");
59+ playback_mixer.set_all_playback_volume(0.75f);
60+ capture_mixer.set_all_capture_volume(0.75f);
61+ } catch(Alsa::AlsaError err) {
62+ logger.normal() << "Failed to change volume: " << err.what() << std::endl;
63+ // not being able to change the volume is not critical to the test
64+ // and for some devices "Master" and "Caputre" mixers may not exist
65+ // so let's just print warning if that's the case
66+ }
67 auto format = std::string("int16_48000");
68 auto format_it = std::find(args.begin(), args.end(), std::string("--format"));
69 if (format_it != args.end()) { // not doing && because of sequence points

Subscribers

People subscribed via source and target branches