0

Javaにはシステムのマイクとスピーカーにアクセスする簡単な方法がないため、自分でそのようなものを実装しようとしました。このために、AudioDevice任意のデバイスを表すクラスがあります。

これは、オーディオ デバイスが必要とするすべてのメソッドを定義する抽象クラスであり、 に基づいていjavax.sound.sampled.Mixerます。次に、から拡張され、 の抽象メソッドを実装しているクラスMicrophoneおよびがあります。、、など。_SpeakerAudioDeviceAudioDevicetransferData(byte[])open(AudioFormat)isOpen()close()

テストコードを実行すると、エラーが発生していないため、機能しているようです。しかし、何らかの理由transferData(byte[])で、マイクの が配列にデータを書き込んでいません。事前に入力された byte[] を使用し、それをスピーカーのパラメーターとして使用するとtransferData(byte[])、スピーカーもサウンドを再生しません。

この問題の原因となっているエラーの検索に数時間を費やしました。また、ここに記載されているようなサウンドをキャプチャ/再生しようとしましたが、コンピューターでも機能しませんでした。

キャプチャー _

それが動作しない理由である可能性がある場合、私は Windows Professional N 64 ビット OS を持っています。プログラムはJava 8で実行されています。

これは私が思いついたコードです

オーディオ装置:

public abstract class AudioDevice<E> {

private final static Set<AudioFormat> AUDIO_FORMATS = new HashSet<>();
static {
    final float[] sampleRates = new float[] { 8000.0F, 11025.0F, 16000.0F, 22050.0F, 44100.0F };
    final int[] sampleSizes = new int[] { 8, 16 };
    final int[] channels = new int[] { 1, 2 };

    for (final float sampleRate : sampleRates) {
        for (final int sampleSize : sampleSizes) {
            for (final int channel : channels) {
                final AudioFormat bigEndian_signed = new AudioFormat(sampleRate, sampleSize, channel, true, true);
                final AudioFormat bigEndian_unsigned = new AudioFormat(sampleRate, sampleSize, channel, false,
                        true);
                final AudioFormat littleEndian_signed = new AudioFormat(sampleRate, sampleSize, channel, true,
                        false);
                final AudioFormat littleEndian_unsigned = new AudioFormat(sampleRate, sampleSize, channel, false,
                        false);
                AUDIO_FORMATS.add(littleEndian_unsigned);
                AUDIO_FORMATS.add(bigEndian_unsigned);
                AUDIO_FORMATS.add(bigEndian_signed);
                AUDIO_FORMATS.add(littleEndian_signed);
            }
        }

    }
}

protected final Mixer mixer;
private final Mixer.Info mixerInfo;
private final Class<E> lineType;
private final Set<AudioFormat> supportedFormats = new HashSet<>();

protected AudioFormat currentFormat;
protected E currentDataLine;

public AudioDevice(final Mixer pMixer, final Class<E> pLineType) {
    if (pMixer == null) {
        throw new IllegalArgumentException();
    }
    this.mixer = pMixer;
    this.mixerInfo = pMixer.getMixerInfo();
    this.lineType = pLineType;
    loadSupportedFormats(pLineType);
    if (this.supportedFormats.isEmpty()) {
        throw new IllegalArgumentException();
    }
}

public abstract boolean transferData(final byte[] pBuffer);

public abstract boolean open(final AudioFormat pFormat);

public abstract boolean isOpen();

public abstract boolean close();

public final boolean isUsingFormat(final AudioFormat pFormat) {
    if (isOpen() && this.currentFormat != null) {
        return this.currentFormat.equals(pFormat);
    }
    return false;
}

public final Set<AudioFormat> getSupportedFormats() {
    return this.supportedFormats;
}

public final AudioFormat getCurrentFormat() {
    return this.currentFormat;
}

public final boolean isFormatSupported(final AudioFormat pFormat) {
    if (pFormat != null) {
        final Set<AudioFormat> supportedFormats = getSupportedFormats();
        if (supportedFormats != null) {
            final DataLine.Info info = new Info(this.lineType, pFormat);
            return this.mixer.isLineSupported(info);
        }
    }
    return false;
}

public final String getName() {
    return this.mixerInfo.getName();
}

public final String getDescription() {
    return this.mixerInfo.getDescription();
}

public final String getVendor() {
    return this.mixerInfo.getVendor();
}

public final String getVersion() {
    return this.mixerInfo.getVersion();
}

private final void loadSupportedFormats(final Class<E> pLineType) {
    for (final AudioFormat format : AUDIO_FORMATS) {
        final DataLine.Info info = new Info(pLineType, format);
        if (this.mixer.isLineSupported(info)) {
            this.supportedFormats.add(format);
        }
    }
}
}

マイクロフォン:

public final class Microphone extends AudioDevice<TargetDataLine> {

public Microphone(final Mixer pMixer) {
    super(pMixer, TargetDataLine.class);
}

@Override
public final boolean transferData(final byte[] pBuffer) {
    if (isOpen()) {
        this.currentDataLine.read(pBuffer, 0, pBuffer.length);
        return true;
    }
    return false;
}

@Override
public final boolean open(final AudioFormat pFormat) {
    if (!isOpen() && isFormatSupported(pFormat)) {
        try {
            this.mixer.open();
            final DataLine.Info info = new Info(TargetDataLine.class, pFormat);
            this.currentDataLine = (TargetDataLine) this.mixer.getLine(info);
            this.currentFormat = pFormat;
            return true;
        } catch (final LineUnavailableException e) {
            close();
            return false;
        }
    }
    return false;

}

@Override
public final boolean isOpen() {
    return this.mixer.isOpen() && this.currentDataLine != null;
}

@Override
public final boolean close() {
    if (isOpen()) {
        this.currentDataLine.close();
        this.mixer.close();
        this.currentDataLine = null;
        this.currentFormat = null;
        return true;
    }
    this.currentDataLine = null;
    this.currentFormat = null;
    return false;
}
}

スピーカー:

public final class Speaker extends AudioDevice<SourceDataLine> {

public Speaker(final Mixer pMixer) {
    super(pMixer, SourceDataLine.class);
}

@Override
public final boolean transferData(final byte[] pBuffer) {
    if (isOpen()) {
        this.currentDataLine.write(pBuffer, 0, pBuffer.length);
        return true;
    }
    return false;
}

@Override
public final boolean open(final AudioFormat pFormat) {
    if (!isOpen() && isFormatSupported(pFormat)) {
        try {
            this.mixer.open();
            final DataLine.Info info = new Info(SourceDataLine.class, pFormat);
            this.currentDataLine = (SourceDataLine) this.mixer.getLine(info);
            this.currentFormat = pFormat;
            return true;
        } catch (final LineUnavailableException e) {
            close();
            return false;
        }
    }
    return false;

}

@Override
public final boolean isOpen() {
    return this.mixer.isOpen() && this.currentDataLine != null;
}

@Override
public final boolean close() {
    if (isOpen()) {
        this.currentDataLine.close();
        this.mixer.close();
        this.currentDataLine = null;
        this.currentFormat = null;
        return true;
    }
    this.currentDataLine = null;
    this.currentFormat = null;
    return false;
}
}
4

0 に答える 0