Skip to content

Commit

Permalink
* audio player support config volume
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed Nov 21, 2024
1 parent 8c88ddb commit 4e8d746
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
4 changes: 2 additions & 2 deletions components/voice/include/maix_audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ namespace maix::audio
~Player();

/**
* Set/Get player volume(Not support now)
* Set/Get player volume
* @param value volume value, If you use this parameter, audio will set the value to volume,
* if you don't, it will return the current volume.
* if you don't, it will return the current volume. range is [0, 100].
* @return the current volume
* @maixpy maix.audio.Player.volume
*/
Expand Down
30 changes: 28 additions & 2 deletions components/voice/port/maixcam/maix_audio_mmf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,34 @@ namespace maix::audio
}

int Player::volume(int value) {
err::check_raise(err::ERR_NOT_IMPL, "Not support now\r\n");
return -1;
char buffer[512];
value = value > 100 ? 100 : value;
value = value < 0 ? -1 : value;
// set
if (value != -1) {
value = 100 - value;
snprintf(buffer, sizeof(buffer), "amixer -c 1 set 'DAC' %d%% &> /dev/zero", value);
system(buffer);
}

// get
FILE *fp;
fp = popen("amixer -c 1 get 'DAC'", "r");
if (fp == NULL) {
return -1;
}

int read_value = -1;
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
if (strstr(buffer, " Front Right: Playback")) {
sscanf(buffer, " Front Right: Playback %*d [%d%%]", &read_value);
}
}
pclose(fp);

read_value = ((100 - read_value)) * 2;

return read_value;
}

err::Err Player::play(maix::Bytes *data) {
Expand Down
17 changes: 12 additions & 5 deletions examples/audio_demo/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ int _main(int argc, char* argv[])
}

log::info("Ready to record and save to %s\r\n", path.c_str());
audio::Recorder r = audio::Recorder();
audio::Recorder r = audio::Recorder(nullptr, sample_rate, format, channel);

while (!app::need_exit()) {
Bytes *b = r.record(-1);
Expand Down Expand Up @@ -384,7 +384,6 @@ int _main(int argc, char* argv[])

log::info("Playback %s\r\n", path.c_str());
audio::Player p = audio::Player("", sample_rate, format, channel);

FILE *file;
file = fopen(path.c_str(), "rb+");
err::check_null_raise(file);
Expand All @@ -397,9 +396,17 @@ int _main(int argc, char* argv[])
}
fclose(file);

while (!app::need_exit()) {
time::sleep_ms(1000);
}
break;
}
case 7:
{
int volume = 10;
if (argc > 2) volume = atoi(argv[2]);

log::info("Set player volume:%d\r\n", volume);
audio::Player r = audio::Player();
int new_volume = r.volume(volume);
log::info("Get player volume:%d\r\n", new_volume);
break;
}
case 100:
Expand Down

0 comments on commit 4e8d746

Please sign in to comment.