Kha icon indicating copy to clipboard operation
Kha copied to clipboard

Linux sound issues (native c++)

Open guzzard opened this issue 7 years ago • 14 comments

The audio is really bad on native c++ in Linux (at least for me). Listen to the two attached samples. Every time I build and run there are distortions, but the distortions sounds a little bit different each time (??). If i run a Krom or HTML5 from the same project there are no sound issues.

This code is playing ogg-files using kha.audio1.Audio.play().

linux_native_sound_issues.zip

guzzard avatar Nov 19 '17 22:11 guzzard

That's hard to debug for me because it works fine on the systems I could test. Do you run it in release mode?

RobDangerous avatar Nov 19 '17 22:11 RobDangerous

Hi @RobDangerous,

Made a correction above, it's ogg-files not wav-files. It's an Armory project, and the wav-files are encoded upon exporting the project.

The sound issues are present in both debug and release native c++ builds.

When playing the ogg-files in Audacity they sound fine, so there seem to be no issue with the ogg encoding.

guzzard avatar Nov 19 '17 23:11 guzzard

Do wav files work better?

RobDangerous avatar Nov 19 '17 23:11 RobDangerous

Created a minimal Kha project with this Project.hx:

package;

import kha.Framebuffer;
import kha.Scheduler;
import kha.System;
import kha.input.Keyboard;
import kha.input.KeyCode;
import kha.Sound;

class Project {

	var s:Sound;

	public function new() {
		System.notifyOnRender(render);
		Scheduler.addTimeTask(update, 0, 1 / 60);
		Keyboard.get().notify(onKeyDown, onKeyUp);
		
		var description = { files: ["run1.wav"] };
		kha.LoaderImpl.loadSoundFromDescription(description, function(b:kha.Sound) {
			b.uncompress(function () {
				s = b;
			});
		});
	}

	function update(): Void {
	}

	function render(framebuffer: Framebuffer): Void {
	}

	public function onKeyDown(key:KeyCode):Void {
	}
	
	public function onKeyUp(key:KeyCode):Void {
		switch (key){
			case KeyCode.One:
				kha.audio1.Audio.play(s, false);
			default:
		}
	}
}

It uses the same method of loading and playing the sound as the Armory project, but loads a wav-file instead. Same issues here, in both debug and release native c++ builds. Krom works fine, no sound issue. Attached a sample.

sound_issue_wav_minimal_kha.ogg.zip

guzzard avatar Nov 19 '17 23:11 guzzard

That's strange, Krom should be much more likely to cause trouble, uses the same audio backend plus has to transfer audio data from js. Can you compile Krom yourself and also test that?

RobDangerous avatar Nov 19 '17 23:11 RobDangerous

In self-compiled Krom still no sound issues, both debug or release version of Krom work fine. I compiled only the Krom executable, and took rest of the files from Deployment folder. Could that have an effect or is it a valid test?

guzzard avatar Nov 19 '17 23:11 guzzard

That's a valid test. But now I have absolutely no idea what's going on.

RobDangerous avatar Nov 19 '17 23:11 RobDangerous

Did some more testing on the minimal Kha project with native C++ build. Don't think it's related to the compiling, but rather seem to be related to starting/running the executable.

I tested 20-25 times, launching the executable and playing the sound, and two or three those times the sound played without any distortion, rest of the times the sound had distortion that sounded a bit different on every launch.

On Krom however there is no sound issues regardless of how many times I test.

Don't know if that helps, or complicates things even more..

guzzard avatar Nov 19 '17 23:11 guzzard

can it be a problem with the files itself, wrong bits per sample or bytes per second? still strange that sometimes works. just to discard that try some sounds from https://github.com/Kha-Samples/BlocksFromHeaven/tree/master/Assets

juakob avatar Jan 24 '18 04:01 juakob

We had some similar issue when only using alsa. After installing and switching to pulse as the audio device (or whatever this is called), everything sounds good.

sh-dave avatar Jan 07 '19 13:01 sh-dave

Also with Krom working fine and direct Kore builds being troublesome?

RobDangerous avatar Jan 07 '19 13:01 RobDangerous

Hm, according to https://wiki.ubuntuusers.de/Soundsystem/ pulse it the default on Ubuntu. That might open up some options to reproduce that problem.

RobDangerous avatar Jan 07 '19 13:01 RobDangerous

I only tested Kore, can't use Krom b/c of missing networking.

Edit: And we use a custom debian based distribution, that doesn't include a lot of stuff by default to keep the size small, so pulse wasn't installed by default.

sh-dave avatar Jan 07 '19 13:01 sh-dave

OK, deinstalling pulse next time I boot into Linux. Thanks!

RobDangerous avatar Jan 07 '19 13:01 RobDangerous