Skip to content

Commit

Permalink
* fix audio bug
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed Sep 6, 2024
1 parent 11815e5 commit e37ef11
Showing 1 changed file with 14 additions and 40 deletions.
54 changes: 14 additions & 40 deletions components/voice/port/maixcam/maix_audio_mmf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ namespace maix::audio
}

static int _alsa_capture_init( snd_pcm_t **handle, snd_pcm_hw_params_t **hw_params, int *period_size,
snd_pcm_format_t format, unsigned int sample_rate, unsigned int channels)
snd_pcm_format_t format, unsigned int sample_rate, unsigned int channels, snd_pcm_uframes_t buffer_size)
{
int err = 0;

Expand Down Expand Up @@ -183,6 +183,11 @@ namespace maix::audio
goto _exit;
}

if ((err = snd_pcm_hw_params_set_buffer_size_near(*handle, *hw_params, &buffer_size)) < 0) {
printf("Can't set channel count (%s)\n", snd_strerror(err));
goto _exit;
}

if ((err = snd_pcm_hw_params(*handle, *hw_params)) < 0) {
printf("Can't set hardware parameters (%s)\n", snd_strerror(err));
goto _exit;
Expand Down Expand Up @@ -252,7 +257,7 @@ namespace maix::audio
}

static int _alsa_player_init( snd_pcm_t **handle, snd_pcm_hw_params_t **hw_params, int *period_size,
snd_pcm_format_t format, unsigned int sample_rate, unsigned int channels)
snd_pcm_format_t format, unsigned int sample_rate, unsigned int channels, snd_pcm_uframes_t buffer_size)
{
int err = 0;

Expand Down Expand Up @@ -294,6 +299,11 @@ namespace maix::audio
goto _exit;
}

if ((err = snd_pcm_hw_params_set_buffer_size_near(*handle, *hw_params, &buffer_size)) < 0) {
printf("Can't set channel count (%s)\n", snd_strerror(err));
goto _exit;
}

if ((err = snd_pcm_hw_params(*handle, *hw_params)) < 0) {
printf("Can't set hardware parameters (%s)\n", snd_strerror(err));
goto _exit;
Expand Down Expand Up @@ -347,40 +357,6 @@ namespace maix::audio
return len * frame_byte * channels;
}

static bool first_init = true;
static void _trigger_segmentation_fault(void) {
pid_t pid;
int status;
pid = fork();
if (pid == -1) {
perror("pcm fix bug failed!\r\n");
return;
} else if (pid == 0) {
char buffer[1024];
snd_pcm_t *handle;
snd_pcm_hw_params_t *hwparams;
int period_size;
_alsa_capture_init(&handle, &hwparams, &period_size, SND_PCM_FORMAT_S16_LE, 48000, 1);
_alsa_capture_pop(handle, SND_PCM_FORMAT_S16_LE, 1, period_size, buffer, sizeof(buffer));
_alsa_capture_deinit(handle);
exit(EXIT_SUCCESS);
} else {
waitpid(pid, &status, 0);
}
}

static void _fix_segmentation_fault_bug(void) {
if (!first_init) {
return;
}

for (int i = 0; i < 3; i ++) {
_trigger_segmentation_fault();
}

first_init = false;
}

static snd_pcm_format_t _alsa_format_from_maix(audio::Format format) {
switch (format)
{
Expand Down Expand Up @@ -416,8 +392,7 @@ namespace maix::audio
snd_pcm_t *handle = NULL;
snd_pcm_hw_params_t *hwparams;
snd_pcm_format_t format_p = _alsa_format_from_maix(format);
_fix_segmentation_fault_bug();
err::check_bool_raise(0 <= _alsa_capture_init(&handle, &hwparams, &_period_size, format_p, sample_rate, channel), "capture init failed");
err::check_bool_raise(0 <= _alsa_capture_init(&handle, &hwparams, &_period_size, format_p, sample_rate, channel, 24000), "capture init failed");
_handle = handle;
snd_pcm_uframes_t buffer_size = 0;
_buffer = _alsa_prepare_buffer(format_p, channel, _period_size, &buffer_size);
Expand Down Expand Up @@ -669,8 +644,7 @@ namespace maix::audio
snd_pcm_t *handle = NULL;
snd_pcm_hw_params_t *hwparams;
snd_pcm_format_t format_p = _alsa_format_from_maix(format);
_fix_segmentation_fault_bug();
err::check_bool_raise(0 <= _alsa_player_init(&handle, &hwparams, &_period_size, format_p, sample_rate, channel), "capture init failed");
err::check_bool_raise(0 <= _alsa_player_init(&handle, &hwparams, &_period_size, format_p, sample_rate, channel, 24000), "capture init failed");
_handle = handle;
snd_pcm_uframes_t buffer_size = 0;
_buffer = _alsa_prepare_buffer(format_p, channel, _period_size, &buffer_size);
Expand Down

0 comments on commit e37ef11

Please sign in to comment.