From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 331EF3858D20; Mon, 30 Oct 2023 11:03:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 331EF3858D20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1698663795; bh=KomIB3mMROWYzFim3gvXWIgWyRdlPkI3T608Q+TYsaw=; h=From:To:Subject:Date:From; b=l7yHs8jshf61bYUHpOsQ9GerM4XvacGSXueDXnv6dObqeXw+Qnqo2gobBNhB+B+fL AK3cuE1TmVarHuHZjY/i8NzbWhU1tG2siOLaoxkYY6GQ6+8BDHHZFAdeh5uDBB33B9 do+JtUrdDh0laXICi/82OgJm+SCkFxLV5ICSd1uA= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: dsp: Improve minimum buffser size estimation. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 39f734a857e2692224715b03b99fc7bd83e94a0f X-Git-Newrev: d56d58ace27b00e6b11f25d8a02b6deab3db272b Message-Id: <20231030110315.331EF3858D20@sourceware.org> Date: Mon, 30 Oct 2023 11:03:15 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Dd56d58ace27= b00e6b11f25d8a02b6deab3db272b commit d56d58ace27b00e6b11f25d8a02b6deab3db272b Author: Takashi Yano Date: Thu Oct 5 21:52:59 2023 +0900 Cygwin: dsp: Improve minimum buffser size estimation. =20 The commit 322c7150b25e restricts buffer size with a fixed length, however, the minimum buffer size should be varied by the sample rate. With this patch, it is estimated using sample rate, sample width and number of channels so that the buffer length is not less than 80 msec which is almost the minimum value of Win MME to work. =20 Fixes: 322c7150b25e ("Cygwin: dsp: Avoid setting buffer that is too sma= ll.") Signed-off-by: Takashi Yano Diff: --- winsup/cygwin/fhandler/dsp.cc | 60 ++++++++++++++++++++++++++-------------= ---- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/winsup/cygwin/fhandler/dsp.cc b/winsup/cygwin/fhandler/dsp.cc index 97f3eaa27..59c11ac23 100644 --- a/winsup/cygwin/fhandler/dsp.cc +++ b/winsup/cygwin/fhandler/dsp.cc @@ -65,7 +65,7 @@ class fhandler_dev_dsp::Audio void convert_S16LE_S16BE (unsigned char *buffer, int size_bytes); void fillFormat (WAVEFORMATEX * format, int rate, int bits, int channels); - static unsigned blockSize (int rate, int bits, int channels); + static unsigned blockSize (double ms, int rate, int bits, int channels); void (fhandler_dev_dsp::Audio::*convert_) (unsigned char *buffer, int size_bytes); =20 @@ -352,10 +352,10 @@ fhandler_dev_dsp::Audio::fillFormat (WAVEFORMATEX * f= ormat, =20 // calculate a good block size unsigned -fhandler_dev_dsp::Audio::blockSize (int rate, int bits, int channels) +fhandler_dev_dsp::Audio::blockSize (double ms, int rate, int bits, int cha= nnels) { unsigned blockSize; - blockSize =3D ((bits / 8) * channels * rate) / 8; // approx 125ms per bl= ock + blockSize =3D ms * ((bits / 8) * channels * rate) / 1000; // round up to multiple of 64 blockSize +=3D 0x3f; blockSize &=3D ~0x3f; @@ -525,7 +525,7 @@ void fhandler_dev_dsp::Audio_out::default_buf_info (aud= io_buf_info *p, int rate, int bits, int ch= annels) { p->fragstotal =3D DEFAULT_BLOCKS; - p->fragsize =3D blockSize (rate, bits, channels); + p->fragsize =3D blockSize (125, rate, bits, channels); p->fragments =3D p->fragstotal; p->bytes =3D p->fragsize * p->fragments; } @@ -537,7 +537,7 @@ fhandler_dev_dsp::Audio_out::callback_sampledone (WAVEH= DR *pHdr) { Qisr2app_->send (pHdr); ReleaseSemaphore (fh->get_select_sem (), - get_obj_handle_count (fh->get_select_sem ()) - 1, NULL); + get_obj_handle_count (fh->get_select_sem ()), NULL); } =20 bool @@ -555,8 +555,7 @@ fhandler_dev_dsp::Audio_out::waitforspace () set_errno (EAGAIN); return false; } - debug_printf ("1ms"); - switch (cygwait (1)) + switch (cygwait (fh->get_select_sem (), 10)) { case WAIT_SIGNALED: if (!_my_tls.call_signal_handler ()) @@ -934,8 +933,7 @@ fhandler_dev_dsp::Audio_in::waitfordata () set_errno (EAGAIN); return false; } - debug_printf ("1ms"); - switch (cygwait (1)) + switch (cygwait (fh->get_select_sem (), 10)) { case WAIT_SIGNALED: if (!_my_tls.call_signal_handler ()) @@ -967,7 +965,7 @@ void fhandler_dev_dsp::Audio_in::default_buf_info (audi= o_buf_info *p, int rate, int bits, int ch= annels) { p->fragstotal =3D DEFAULT_BLOCKS; - p->fragsize =3D blockSize (rate, bits, channels); + p->fragsize =3D blockSize (125, rate, bits, channels); p->fragments =3D 0; p->bytes =3D 0; } @@ -998,7 +996,7 @@ fhandler_dev_dsp::Audio_in::callback_blockfull (WAVEHDR= *pHdr) { Qisr2app_->send (pHdr); ReleaseSemaphore (fh->get_select_sem (), - get_obj_handle_count (fh->get_select_sem ()) - 1, NULL); + get_obj_handle_count (fh->get_select_sem ()), NULL); } =20 static void CALLBACK @@ -1127,8 +1125,13 @@ fhandler_dev_dsp::_write (const void *ptr, size_t le= n) /* nothing to do */; else if (IS_WRITE ()) { - if (!fragment_has_been_set) - fragsize_ =3D Audio::blockSize (audiofreq_, audiobits_, audiochannels_); + if (fragment_has_been_set) + fragsize_ =3D max (Audio::blockSize (80.0 / fragstotal_, audiofreq_, + audiobits_, audiochannels_), + fragsize_); + else + fragsize_ =3D Audio::blockSize (125, audiofreq_, audiobits_, + audiochannels_); debug_printf ("Allocating"); if (!(audio_out_ =3D new Audio_out (this))) return -1; @@ -1174,7 +1177,8 @@ fhandler_dev_dsp::_read (void *ptr, size_t& len) else if (IS_READ ()) { if (!fragment_has_been_set) - fragsize_ =3D Audio::blockSize (audiofreq_, audiobits_, audiochannels_); + fragsize_ =3D Audio::blockSize (125, audiofreq_, audiobits_, + audiochannels_); debug_printf ("Allocating"); if (!(audio_in_ =3D new Audio_in (this))) { @@ -1233,7 +1237,7 @@ fhandler_dev_dsp::close () being_closed =3D true; close_audio_in (); close_audio_out (); - ReleaseSemaphore (select_sem, get_obj_handle_count (select_sem) - 1, NUL= L); + ReleaseSemaphore (select_sem, get_obj_handle_count (select_sem), NULL); CloseHandle (select_sem); select_sem =3D NULL; return fhandler_base::close (); @@ -1255,9 +1259,13 @@ fhandler_dev_dsp::_ioctl (unsigned int cmd, void *bu= f) break; =20 CASE (SNDCTL_DSP_GETBLKSIZE) - if (!fragment_has_been_set) - fragsize_ =3D Audio::blockSize (audiofreq_, audiobits_, audiochannels_); - *intbuf =3D fragsize_; + if (fragment_has_been_set) + *intbuf =3D max (Audio::blockSize (80.0 / fragstotal_, audiofreq_, + audiobits_, audiochannels_), + fragsize_); + else + *intbuf =3D Audio::blockSize (125, audiofreq_, audiobits_, + audiochannels_); return 0; =20 CASE (SNDCTL_DSP_SETFMT) @@ -1387,8 +1395,11 @@ fhandler_dev_dsp::_ioctl (unsigned int cmd, void *bu= f) audio_out_->buf_info (p, audiofreq_, audiobits_, audiochannels_); else if (fragment_has_been_set) { - p->bytes =3D fragsize_ * fragstotal_; - p->fragsize =3D fragsize_; + p->fragsize =3D max (Audio::blockSize (80.0 / fragstotal_, + audiofreq_, audiobits_, + audiochannels_), + fragsize_); + p->bytes =3D p->fragsize * fragstotal_; p->fragstotal =3D fragstotal_; p->fragments =3D fragstotal_; } @@ -1412,7 +1423,10 @@ fhandler_dev_dsp::_ioctl (unsigned int cmd, void *bu= f) else if (fragment_has_been_set) { p->bytes =3D 0; - p->fragsize =3D fragsize_; + p->fragsize =3D max (Audio::blockSize (80.0 / fragstotal_, + audiofreq_, audiobits_, + audiochannels_), + fragsize_); p->fragstotal =3D fragstotal_; p->fragments =3D 0; } @@ -1430,8 +1444,8 @@ fhandler_dev_dsp::_ioctl (unsigned int cmd, void *buf) int *p =3D (int *) buf; fragstotal_ =3D min (*p >> 16, MAX_BLOCKS); fragsize_ =3D 1 << (*p & 0xffff); - while (fragsize_ * fragstotal_ < 16384) - fragsize_ *=3D 2; + if (fragstotal_ < 2) + fragstotal_ =3D 2; fragment_has_been_set =3D true; return 0; }