Neun Lautsprecher separat ansteuern

Rufus.Mueller

Bekanntes Mitglied
Moin,

ich habe einen Rechner mit zwei Soundkarten und neun Lautsprechern. Ich will nun mit Java eine Soundausgabe produzieren, so dass ich die Lautsprecher einzeln ansteuern kann, also dass immer nur an einem der neun Lautsprecher die Soundausgabe ertönt und an den anderen nicht.

Geht das überhaupt mit Java? Welche Klassen und Methoden brauche ich dazu?

Gruß,
Rufus
 

Andi_CH

Top Contributor
Das geht mit 2 - musst halt Versuchen wie das mit 9 geht - das mit den 2 habe ich auch eher durch Zufall herausgefunden.
9 Tönt nach dolby -meines hier ist primitives Stereo :)

Java:
package com.javaforum.audio;

import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.AudioFormat;
 
public class ToneGeneratorExample {
 
    public byte[] getMonoSinusTone(int frequency, AudioFormat af) {
        byte sampleSize = (byte) (af.getSampleSizeInBits() / 8);
        byte[] data = new byte[(int) af.getSampleRate() * sampleSize];
        double step_width = (2 * Math.PI) / af.getSampleRate();
        double x = 0;
        for (int i = 0; i < data.length; i += sampleSize) {
            int sample_max_value = (int) Math.pow(2, af.getSampleSizeInBits()) / 2 - 1;
            int value = (int) (sample_max_value * Math.sin(frequency * x));
            for (int j = 0; j < sampleSize; j++) {
                byte sample_byte = (byte) ((value >> (8 * j)) & 0xff);
                data[i + j] = sample_byte;
            }
            x += step_width;
        }
        return data;
    }
 
    public byte[] getStereoSinusTone(int frequency1, int frequency2, AudioFormat af) {
        byte sampleSize = (byte) (af.getSampleSizeInBits() / 8);
        byte[] data = new byte[(int) af.getSampleRate() * sampleSize  * 2];
        double stepWidth = (2 * Math.PI) / af.getSampleRate();
        double x = 0;
        for (int i = 0; i < data.length; i += sampleSize * 2) {
            int sample_max_value = (int) Math.pow(2, af.getSampleSizeInBits()) / 2 - 1;
            int value = (int) (sample_max_value * Math.sin(frequency1 * x));
            for (int j = 0; j < sampleSize; j++) {
                byte sampleByte = (byte) ((value >> (8 * j)) & 0xff);
                data[i + j] = sampleByte;
            }
            value = (int) (sample_max_value * Math.sin(frequency2 * x));
            for (int j = 0; j < sampleSize; j++) {
                byte sampleByte = (byte) ((value >> (8 * j)) & 0xff);
                int index = i + j + sampleSize;
                data[index] = sampleByte;
            }
            x += stepWidth;
        }
        return data;
    }
 
    public void play(int frequenzy) {
        AudioFormat af = new AudioFormat(44100, 16, 1, true, false);
        byte[] data = getMonoSinusTone(frequenzy, af);
        try {
            Clip c = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
            c.open(af, data, 0, data.length);
            c.loop(1);
            while(c.isRunning()) {
                try {
                    Thread.sleep(50);
                } catch (Exception ex) {}
            }
        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        }
    }
 
    public void play(int frequenzyLeft, int  frequencyRight) {
        AudioFormat af = new AudioFormat(44100, 16, 2, true, false);
        byte[] data = getStereoSinusTone(frequenzyLeft, frequencyRight, af);
        try {
            Clip c = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
            c.open(af, data, 0, data.length);
            c.loop(1);
            while(c.isRunning()) {
                try {
                    Thread.sleep(50);
                } catch (Exception ex) {}
            }
        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
        ToneGeneratorExample tge = new ToneGeneratorExample();
        if (args.length == 1) {
            tge.play(Integer.parseInt(args[0]));
        } else if (args.length == 2) {
            tge.play(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
        } else {
        	System.out.println("Mono Ton");
            tge.play(500);
            System.out.println("Ton auf dem linken Kanal");
            tge.play(500, 0);
            System.out.println("Ton auf dem rechten Kanal");
            tge.play(0, 500);
            System.out.println("Ton auf dem beiden Kanälen");
            tge.play(500, 1300);
            System.out.println("Fertig");
        }
    }
}
 

Nicer

Bekanntes Mitglied
Der Tonegenerator kann meineswissens auch nur Mono und Stereo. Aber warum 9 Lautsprecher ?

5.1 = 5 Hoch / Mitteltöner und 1 Subwoofer = 6 Lautsprecher
7.1 = 7 Hoch / Mitteltöner und 1 Subwoofer = 8 Lautsprecher
( Korrigiert mich wennich Falsch liege :D )

Du könntest versuchen die ToneGenerator klasse zu überladen und so vllt irgendwas mit 9 Kanälen hinzubekommen. Das nächste Problem sind die 2 Soundkarten. Ich schätze mal der Tonegenerator gibt über die vom OS Definierte Standartausgabequelle aus , also immer nur die eine oder die andere Soundkarte.
 

Ähnliche Java Themen


Oben