Issues generating sound on Linux

Hello. I have been playing with sound generation for my current project and finally got generation working the other day. I found this topic and started from the code samples obscure provided, but couldn’t get sound to play. It seems that the problem was SampleDataEvent not being supported on native yet, which lead me to this thread. Using the HeapsIO NativeChannel worked, and I can generate a soundwave. However, the generated sound seems to only work below a certain frequency, and works strangely up to that point.

I’ve tested my headset and speakers with this video, and I’m able to hear up to around 16000Hz, though my headset and speakers lose some volume as the frequency gets higher as expected.

I built a simple gui slider to allow me to scroll through frequencies, starting at 1000Hz, in increments of 100Hz.

I’m overriding the onSample NativeChannel function as demonstrated in the second thread I linked.
I tried a square wave with the following code:

for(i in 0...out.length)
{
	out[i] = (Std.int(runningSampleIndex++ / halfSquareWavePeriod) % 2 == 0) ? toneVolume : -toneVolume;
}

which stops around 7300Hz.

I also tried a sin wave:

for(i in 0...out.length)
{
	out[i] = Math.sin(runningSampleIndex++ / sampleRate * Math.PI * toneHertz);
}

which stops around 17100Hz.

When I say it “stops” I mean I don’t hear anything past that point. In the case of the sin wave it could be due to the limits of my hardware’s response, but that doesn’t explain why the square wave stops at a much lower frequency. With each wave I hear slight popping when moving through frequencies past the “cutoff” frequency, so it seems to me that the audio system is still outputting, and it’s the data that is being sent to it that is the problem.

Additionally, the square wave has a range of about 600Hz where there is no change in the frequency I am hearing, and the sin wave seems to overflow and start outputting lower frequencies for another range and has ranges where the output is not stereo as well as points where I seem to be hearing “background” frequencies. All this leads me to believe that the data is not in the expected format an so is being interpreted strangely, but I don’t know how that happened.

In short, I’m confused. I don’t know a lot about sound (one of the reasons I’m doing this project), so I would greatly appreciate some help.

I recorded examples of what I’m experiencing. I created an ENTER_FRAME listener and increased the frequency being generated by 10Hz each frame:

public function enterFrame(e:Event)
{
	freq += 10;
	setFrequency(freq);
}

The channel frequency is then changed:

public function setFrequency(freq:Int):Void
{
	channel.toneHertz = freq;
}

During the silent parts the code is still running; for some reason no sound is being generated. Volume warning: it gets loud at points.
Here is the sine wave from 800Hz to about 18000Hz.
Here is the square wave from 800Hz to about 15000Hz

The generated audio is clearly not what was expected.

I also uploaded audacity project files.
Sine wave
Square wave

Good news! After playing with this for days and not figuring out the issue, I got an html5 build working, and the audio generation works as expected there, with the generated audio actually being the correct frequency! Unfortunately, I need this to work in cpp.

Due to this discovery, the issue seems to be with the NativeChannel/ALChannel classes from HeapsIO. I know that generating audio isn’t particularly common (especially in Haxe it seems), but does anyone have any idea what the issue is? Or is there a better way to generate audio that I missed?

[Update]
After being directed to this pull request and applying it locally, I can play generated sound using the SampleDataEvent! Unfortunately, at least on Linux, the sound that is played still is not correct, as mentioned in the above post. However, this does work fine on html5 and Android, my target platform, so at least this is no longer a blocker. :slight_smile:

Hopefully this will be of help to anyone else confused by this issue.

2 Likes