diff --git a/components/voice/include/maix_audio.hpp b/components/voice/include/maix_audio.hpp index b9b53fe9..75d4b1e4 100644 --- a/components/voice/include/maix_audio.hpp +++ b/components/voice/include/maix_audio.hpp @@ -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 */ diff --git a/components/voice/port/maixcam/maix_audio_mmf.cpp b/components/voice/port/maixcam/maix_audio_mmf.cpp index 884b8d27..32a049ca 100644 --- a/components/voice/port/maixcam/maix_audio_mmf.cpp +++ b/components/voice/port/maixcam/maix_audio_mmf.cpp @@ -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) { diff --git a/examples/audio_demo/main/src/main.cpp b/examples/audio_demo/main/src/main.cpp index 856f9d68..5fcc277a 100644 --- a/examples/audio_demo/main/src/main.cpp +++ b/examples/audio_demo/main/src/main.cpp @@ -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); @@ -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); @@ -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: