Re: Silent noise while running simple OSS program and moving mouse?

From: John-Mark Gurney <jmg_at_funkthat.com>
Date: Sun, 16 May 2021 16:47:52 -0700
Florian Walpen wrote this message on Mon, May 17, 2021 at 00:56 +0200:
> On Monday, May 17, 2021 12:01:31 AM CEST Evgeniy Khramtsov via freebsd-
> multimedia wrote:
> > Hi.
> > 
> > I wrote this simple OSS program that outputs sound from microphone back
> > to the speakers (the close() and stuff is omitted):
> > https://pastebin.com/raw/yX4gm5fL
> 
> Maybe not the culprit, but I'd definitely recommend to prefill the playback 
> buffer with some silence before starting the loop. Like 3-4 times your buffer 
> size of zeros. Otherwise you are very likely to produce underruns.

Per your comment below, this sounds very likely the problem.  If you don't have
some buffering, and your program don't run quickly enough, you can end up w/
the stuttering.

The issue is that when the mouse moves, your program doesn't get scheduled
to run quickly enough before the playback buffer runs out.  Pushing more
data into the playback buffer before you start the loop will give more
time for the program to be scheduled before the playback buffer runs out.

Another method you can use to help reduce the scheduling issue is to use
rtprio(1).  This will boost the scheduling priority of the program so
that it will run sooner instead of letting other processes run.

Evgeniy Khramtsov via freebsd-multimedia wrote this message on Mon, May 17, 2021 at 01:56 +0300:
> > Is it gaps in the sound?
> 
> According to OSS documentation at opensound.com, read and write are
> blocking until there should be enough data for a buffer. If I don't
> touch mouse, e.g. typing only or doing some CPU stress stuff, there
> is no noise. But once I move mouse around, there is some silent noise.

See above.

> > Maybe post a recording of it.
> 
> Writing to a fd doesn't produce a formatted .wav file, unfortunately.
> I'll report later once I figure out this and how to output as .wav.

It is likely just writing from your program to a raw file won't reproduce
the silence that you are hearing, as it won't acurately represent the gaps
that are happening due to buffer issues in the driver..

BTW, you can use a program like sox to convert raw samples into a WAV file...

Something like:
sox -r 48k -e signed -b 16 -c 2 file.raw file.wav

(slightly modified from the verion in the sox manpage)

> On Sun, May 16, 2021 at 03:36:49PM -0700, John-Mark Gurney wrote:
> > Evgeniy Khramtsov via freebsd-multimedia wrote this message on Mon, May 17, 2021 at 01:01 +0300:
> > > I wrote this simple OSS program that outputs sound from microphone back
> > > to the speakers (the close() and stuff is omitted):
> > > https://pastebin.com/raw/yX4gm5fL
> > > 
> > > It works, though for some reason I get some silent noise while moving
> > > mouse around. Any ideas what could be wrong? Thanks.
> > 
> > Can you define silent noise better?  Maybe post a recording of it.
> > 
> > Is it gaps in the sound?  where there's a gap in either playback
> > or recording and it's absolutely silent, or do you mean that there's
> > an additional sound that only appears when you move the mouse?

-- 
  John-Mark Gurney				Voice: +1 415 225 5579

     "All that I will do, has been done, All that I have, has not."
Received on Sun May 16 2021 - 23:47:52 UTC

Original text of this message