Help With Rounding Number of Samples

Audio software developers forum.

If you require help using Audacity, please post on the forum board relevant to your operating system:
Windows
Mac OS X
GNU/Linux and Unix-like

Help With Rounding Number of Samples

Permanent link to this post Posted by Rammschnev » Wed Dec 13, 2017 2:20 am

I've just come on board helping a team with an app that generates lots of different tones with a sine function and plays them at various intervals (in the interest of health, apparently). There are no external libraries being used to do this; we're doing it in Java with a straight-up Math.sin call and writing bytes to a 16-bit .wav file.

We are having quite a tremendous bit of trouble with an issue where, under certain circumstances that we have somewhat narrowed down, sounds that ought to be gentle sine waves come out sounding like machine clanking. The head engineer for the project has narrowed this down to an issue with the number of samples used. Sometimes, based on the total duration for the entire list of frequencies to be played, the calculation for the number of samples is not a whole number, so the program rounds up in this instance. When this happens, the distortion is produced. (Oddly, though, this isn't an issue at all if the number of frequencies generated, which affects the number of samples in each pass, is less than 71. Very strange.) We have been comparing this to the 'Generate Tone' functionality of Audacity. I have experimented with creating tones at arbitrary lengths to create situations where the amount of seconds I specify will result in a non-integer number of samples, forcing Audacity to round. While Audacity will round up or down according to basic rounding principles, our engineer insists that rounding down will render the program unable to produce the .wav file. (We're going to be talking about this a lot tomorrow. I don't know if I definitely buy it.)

The team lead wants me to find out more about what Audacity is doing differently from us. Here is the extent of the code I've been shown so far (I am an outsider, after all). If you need to see more, say so and I can request it.

Code: Select all
double durationSecsEachFreq = lengthSeconds / freqList.size();
//get the sample count required for each freq
int numSamples = (int) Math.ceil(durationSecsEachFreq * sampleRate);

double[] frequencyArray = new double[numSamples];
byte[] frequencyByteArray = new byte[2 * numSamples];

// fill out the array
for (int i = 0; i < numSamples; ++i) {
    frequencyArray[i] = Math.sin(2 * Math.PI * i / (sampleRate / freqOfTone));
}

// convert to 16 bit pcm sound array
// assumes the sample buffer is normalised.
int idx = 0;
for (final double dVal : frequencyArray) {
     // scale to maximum amplitude
     final short val = (short) ((dVal * 32767));
     // in 16 bit wav PCM, first byte is the low order byte
     frequencyByteArray[idx++] = (byte) (val & 0x00ff);
     frequencyByteArray[idx++] = (byte) ((val & 0xff00) >>> 8);

}
return frequencyByteArray;
Rammschnev
 
Posts: 6
Joined: Wed Dec 13, 2017 2:03 am
Operating System: Windows 8 or 8.1

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by steve » Wed Dec 13, 2017 3:57 am

Java is not one of my languages, and you've only given a fragment of code, so you'll have to help me out a bit here.

Where does "freqOfTone" come from? Are you iterating over frequency values in "freqList" ?
If so, are you simply concatenating the sounds from each "frequencyByteArray"?
If so, are you expecting a click each time you start a new array? Even if each frequencyByteArray ends at zero there will be a glitch as the next tone starts at zero. If a tone does not end at zero there will be a very significant click when the next tone starts.

Please give some example numbers for "freqList" and "lengthSeconds".


For my reference, this is a literal translation of what I think you are doing, written in Nyquist (though it would not normally be written like this)
Code: Select all
(setf freqList (list 440 300 1000 400))
(setf lengthSeconds 2.0)

(setf durationSecsEachFreq (/ lengthSeconds  (length freqList)))
; get the sample count required for each freq
(setf numSamples (truncate (+ 0.5 (* durationSecsEachFreq *sound-srate*))))

(setf frequencyArray (make-array numSamples))

(defun one-tone (hz)
  (dotimes (i numSamples (snd-from-array 0 *sound-srate* frequencyArray))
    (setf (aref frequencyArray i)
          (sin (* 2 PI (/ i (/ *sound-srate* hz)))))))

(seqrep (i (length freqList))(cue (one-tone (nth i freqList))))
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 46556
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by steve » Wed Dec 13, 2017 11:18 am

Rammschnev wrote:sounds that ought to be gentle sine waves come out sounding like machine clanking.

Please post a short sample in WAV format. Just a few seconds will be fine, so long as it clearly shows the problem.
See here for how to attach files to forum posts: https://forum.audacityteam.org/viewtopi ... 49&t=64936
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 46556
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by Rammschnev » Wed Dec 13, 2017 3:42 pm

Where does "freqOfTone" come from? Are you iterating over frequency values in "freqList" ?
If so, are you simply concatenating the sounds from each "frequencyByteArray"?
If so, are you expecting a click each time you start a new array? Even if each frequencyByteArray ends at zero there will be a glitch as the next tone starts at zero. If a tone does not end at zero there will be a very significant click when the next tone starts.

Please give some example numbers for "freqList" and "lengthSeconds".


Hi, Steve. Thanks for the help on such limited information. Yes, freqList is a list of frequencies, such as:

[0] 7.83
[1] 62
[2] 9.83
...

lengthSeconds is the total length in seconds of the resulting audio file. We've been shooting for 1020, which results in a file exactly 17 minutes long.

Yes, we are concatenating all of the sounds from each frequencyByteArray. Minor clicks between frequencies are alright. This isn't for a media application really; the idea is that people listen to these frequencies to 'tune their health' or something, so it really is meant to just sound like a bunch of frequencies slapped together.

I've attached two samples of the same frequency, one as it should sound and one "distorted."
Attachments
undistorted-short.wav
The undistorted sound
(46.57 KiB) Downloaded 9 times
distorted-short.wav
The distorted sound
(46.57 KiB) Downloaded 7 times
Rammschnev
 
Posts: 6
Joined: Wed Dec 13, 2017 2:03 am
Operating System: Windows 8 or 8.1

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by Rammschnev » Wed Dec 13, 2017 4:26 pm

Oh, and as for your code translation, it's not in a format I am terribly familiar with, but it all looks right to me except that, crucially, truncate (which, if I understand, simply removes the decimal data and leaves the whole number) is a ceiling function in ours, which rounds up to the next whole number every single time.
Rammschnev
 
Posts: 6
Joined: Wed Dec 13, 2017 2:03 am
Operating System: Windows 8 or 8.1

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by steve » Wed Dec 13, 2017 4:52 pm

That is very curious "distortion". It's not at all random, but has a very clear cyclical pattern that can be clearly seen in the Spectrogram view (http://manual.audacityteam.org/man/spec ... _view.html)
Set the "Window Size" to a small value (say: 128) to see the pattern.
My first guess is that you have a coding error that is reversing the low and high bytes.
I need to go for a while - back later.

firsttrack000.png
firsttrack000.png (65.14 KiB) Viewed 1929 times
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 46556
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by steve » Wed Dec 13, 2017 6:40 pm

Does this sound familiar?
It is a sine wave with the low and high bytes reversed as signed 16-bit values.
Check that your code is always using an even number of bytes per tone.
Attachments
byte-reverse.wav
(46.57 KiB) Downloaded 8 times
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 46556
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by Rammschnev » Wed Dec 13, 2017 6:43 pm

Brilliant. That is pretty definitive. We're very confused as to how that's happening, but we're on the lookout now. I'll post updates as they come. Thanks so much, Steve.
Rammschnev
 
Posts: 6
Joined: Wed Dec 13, 2017 2:03 am
Operating System: Windows 8 or 8.1

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by Rammschnev » Wed Dec 13, 2017 6:48 pm

So I think our problem boils down to this:

A file 17 minutes long (1020 seconds) at 44.1kHz requires 89964000 bytes.
We have 169 frequencies, meaning 532331.3609467456 bytes per tone.
How can we keep that an even number while also keeping our file at exactly 17 minutes?

Any thoughts?
Rammschnev
 
Posts: 6
Joined: Wed Dec 13, 2017 2:03 am
Operating System: Windows 8 or 8.1

Re: Help With Rounding Number of Samples

Permanent link to this post Posted by steve » Wed Dec 13, 2017 7:19 pm

Each 16-bit sample has 2 bytes.
Round your durations to a number of sample - it does not matter if you round up or down or to closest, but it is essential that you have 2 bytes for each sample.
The code that you posted already appears to do that so far as i can tell, so my guess is that the problem occurs when you switch from one tone to the next.

To answer your original question "about what Audacity is doing differently from us", Audacity works internally with 32-bit floats. Conversion to 16-bit only occurs on mix-down and export (though in my opinion it should only occur on export).
9/10 questions are answered in the FREQUENTLY ASKED QUESTIONS (FAQ)
steve
Site Admin
 
Posts: 46556
Joined: Sat Dec 01, 2007 11:43 am
Operating System: Linux *buntu

Next

Return to General Audio Programming



Who is online

Users browsing this forum: No registered users and 1 guest