Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[audio] Remove 'clac' noise when playing wave (javasound) #2670

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.audio.AudioStream;
import org.openhab.core.audio.utils.AudioWaveUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -60,7 +61,10 @@ public AudioPlayer(AudioStream audioStream) {
@Override
public void run() {
SourceDataLine line;
AudioFormat audioFormat = convertAudioFormat(this.audioStream.getFormat());

org.openhab.core.audio.AudioFormat openhabAudioFormat = audioStream.getFormat();

AudioFormat audioFormat = convertAudioFormat(openhabAudioFormat);
if (audioFormat == null) {
logger.warn("Audio format is unsupported or does not have enough details in order to be played");
return;
Expand All @@ -87,6 +91,11 @@ public void run() {
int nRead = 0;
byte[] abData = new byte[65532]; // needs to be a multiple of 4 and 6, to support both 16 and 24 bit stereo
try {
// If this is a wav container, we should remove the header from the stream
// to avoid a "clack" noise at the beginning
if (org.openhab.core.audio.AudioFormat.CONTAINER_WAVE.equals(openhabAudioFormat.getContainer())) {
AudioWaveUtils.removeFMT(audioStream);
}
while (-1 != nRead) {
nRead = audioStream.read(abData, 0, abData.length);
if (nRead >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.core.audio.utils;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;

Expand All @@ -31,6 +32,11 @@
@NonNullByDefault
public class AudioWaveUtils {

/**
* This "magic" packet marks the beginning of the read data
*/
private final static int DATA_MAGIC = 0x64617461;

private static final AudioFormat DEFAULT_WAVE_AUDIO_FORMAT = new AudioFormat(AudioFormat.CONTAINER_WAVE,
AudioFormat.CODEC_PCM_SIGNED, false, 16, 705600, 44100L, 1);

Expand Down Expand Up @@ -69,4 +75,26 @@ public static AudioFormat parseWavFormat(InputStream inputStream) throws IOExcep
inputStream.reset();
}
}

/**
* Remove FMT block (WAV header) from a stream by consuming it until
* the magic packet signaling data. Limit to 200 readInt() (arbitrary value
* used in sun audio package).
* If you don't remove/consume the FMT and pass the data to a player
* as if it is a pure PCM stream, it could try to play it and will
* do a "click" noise at the beginning.
*
* @param audio A wav container in an InputStream
* @throws IOException
*/
public static void removeFMT(InputStream data) throws IOException {
DataInputStream dataInputStream = new DataInputStream(data);
Integer nextInt = dataInputStream.readInt();
int i = 0;
while (nextInt != DATA_MAGIC && i < 200) {
nextInt = dataInputStream.readInt();
i++;
}
dataInputStream.readInt();
}
}