private void openLine() throws Exception {
try {
// align to frame size
bufferSize = (int) AudioUtils.millis2bytes(bufferSizeMillis, lineFormat);
bufferSize -= bufferSize % lineFormat.getFrameSize();
openLineImpl();
if (DEBUG) out(title+": opened line");
bufferSize = line.getBufferSize();
if (VERBOSE) {
out(title+": buffersize="+bufferSize+" bytes.");
}
} catch (LineUnavailableException ex) {
throw new Exception("Unable to open "+title+": "+ex.getMessage());
}
}
public void start() throws Exception {
if (line == null) {
if (DEBUG) out(title+": Call to start(), but line not created!");
throw new Exception(title+": cannot start");
}
line.flush();
line.start();
if (DEBUG) out(title+": started line");
}
public void close() {
close(false);
}
public void close(boolean willReopen) {
closeLine(willReopen);
destroyLine();
}
protected void closeLine(boolean willReopen) {
if (!willReopen) lastLevel = -1;
if (line!=null) {
line.flush();
line.stop();
line.close();
if (DEBUG && title!=null) out(title+": line closed.");
}
}
private void destroyLine() {
if (line != null) {
line.removeLineListener(this);
}
line = null;
}
if (wasOpen) {
createLine();
openLine();
if (wasStarted) {
start();
}
}
}
public void setMuted(boolean muted) {
this.muted = muted;
}
public boolean isMuted() {
return this.muted;
}
public int getLevel() {
return lastLevel;
}
// find the current playback level: the maximum value of this buffer
protected void calcCurrVol(byte[] b, int off, int len) {
int end = off+len;
int sampleSize = (lineFormat.getSampleSizeInBits() + 7) / 8;
int max = 0;
if (sampleSize == 1) {
// 8-bit
for ( ; off < end; off++) {
int sample = (byte) (b[off] + 128);
if (sample < 0) sample = -sample;
if (sample > max) max = sample;
}
lastLevel = max;
} else if (sampleSize == 2) {
if (lineFormat.isBigEndian()) {
// 16-bit big endian
for ( ; off < end; off+=2) {
int sample = (short) ((b[off]<<8) | (b[off+1] & 0xFF));
if (sample < 0) sample = -sample;
if (sample > max) max = sample;
}
} else {
// 16-bit little endian
for ( ; off < end; off+=2) {
int sample = (short) ((b[off+1]<<8) | (b[off] & 0xFF));
if (sample < 0) sample = -sample;
if (sample > max) max = sample;
}
}
//System.out.print("max="+max+" ");
lastLevel = max >> 8;
//System.out.print(":"+len+":");
//System.out.print("[lL="+lastLevel+" "+getClass().toString()+"]");
} else {
lastLevel = -1;
}
}
// find the current playback level: the maximum value of this buffer
protected void muteBuffer(byte[] b, int off, int len) {
int end = off+len;
int sampleSize = (lineFormat.getSampleSizeInBits() + 7) / 8;
byte filler = 0;
if (sampleSize == 1) {
filler = -128;
}
for ( ; off < end; off++) {
b[off] = filler;
}
}
/** the selected port */
private Port[] port = new Port[2];
/** the selected control for port volume */
private FloatControl[] portVolume = new FloatControl[2];
/** the selected control for port select (only for Source Ports) */
private BooleanControl[] portSelect = new BooleanControl[2];
/** all compound controls available on the system, as a display list */
private List<String> portNames[] = new List[2]; // $$ generics
/** all Ports available on the system (maybe the same ones) */
private List[] ports = new List[2];
/** the index of the controls array for the respective compound control */
private List[] controlIndex = new List[2];
/** the selected mixer. If null, use default */
private Mixer[] mixer = new Mixer[2];
/** all mixers */
private List<Mixer>[] mixers = new List[2];
/** index in BUFFER_SIZE_MILLIS */
private int[] bufferSizeIndex = new int[2];
private boolean inited = false;
public AudioSettings () {
portNames[0] = new ArrayList<String>();
portNames[1] = new ArrayList<String>();
ports[0] = new ArrayList();
ports[1] = new ArrayList();
controlIndex[0] = new ArrayList();
controlIndex[1] = new ArrayList();
mixers[0] = new ArrayList<Mixer>();
mixers[1] = new ArrayList<Mixer>();
bufferSizeIndex[0] = BUFFER_SIZE_INDEX_DEFAULT;
bufferSizeIndex[1] = BUFFER_SIZE_INDEX_DEFAULT;
}
public void init() {
for (int d = 0; d < 2; d++) {
portNames[d].clear();
ports[d].clear();
controlIndex[d].clear();
mixers[d].clear();
}
// go through all mixer and fill all lists
Mixer.Info[] infos = AudioSystem.getMixerInfo();
for (int i = 0; i < infos.length; i++) {
try {
Mixer mixer = AudioSystem.getMixer(infos[i]);
addMixer(mixer, mixer.getSourceLineInfo(), DIR_SPK);
addMixer(mixer, mixer.getTargetLineInfo(), DIR_MIC);
} catch (Exception e) {
if (DEBUG) e.printStackTrace();
}
}
// add defaults, if multiples exist
for (int d = 0; d < 2; d++) {
if (mixers[d].size() > 1) {
mixers[d].add(0, null);
}
}
inited = true;
if (VERBOSE) {
out("Microphone Ports: "+ports[DIR_MIC].size());
out("Microphone Mixers: "+mixers[DIR_MIC].size());
out("Speaker Ports: "+ports[DIR_SPK].size());
out("Speaker Mixers: "+mixers[DIR_SPK].size());
}
}
public void exit() {
for (int d = 0; d < 2; d++) {
closePort(d);
portNames[d].clear();
controlIndex[d].clear();
ports[d].clear();
mixers[d].clear();
}
}
private void addMixer(Mixer mixer, Line.Info[] infos, int dirDataLine) {
for (int i = 0; i getMixers(int d) {
if (!inited) init();
List<String> res = new ArrayList<String>();
for (Mixer m : mixers[d]) { // $$ enhanced for
if (m == null) {
res.add("(Default)");
} else {
res.add(m.getMixerInfo().getName());
}
}
return res;
}
public Mixer getSelMixer(int d) {
return mixer[d];
}
/** set index in list returned in getMixers */
public void setSelMixer(int d, int index) {
if (index < 0 || index >= mixers[d].size()) {
if (DEBUG) out("setSelMixer out of range: iondex="+index);
return;
} else {
mixer[d] = mixers[d].get(index);
}
}
// PORT HANDLING //
public List<String> getPorts(int d) {
if (!inited) init();
return portNames[d];
}
public Port getSelPort(int d) {
return port[d];
}
public FloatControl getSelVolControl(int d) {
return portVolume[d];
}
/** set index in list returned in getMixers */
public void setSelPort(int d, int index) {
setSelPort(d, index, true);
}
public void setSelPort(int d, int index, boolean doSelect) {
if (index < 0 || index >= ports[d].size()) {
if (DEBUG) out("setSelPort out of range: iondex="+index);
closePort(d);
return;
} else {
setupPort(d, ports[d].get(index), controlIndex[d].get(index)); // $$ autoboxing
if (doSelect && d == DIR_MIC && portSelect[d] != null) {
portSelect[d].setValue(true);
}
if (DEBUG) out("Selected "+portNames[d].get(index));
}
}
class Constants {
public static final int DIR_MIC = 0;
public static final int DIR_SPK = 1;
public static boolean DEBUG = true;
public static boolean VERBOSE = true;
public static final int FORMAT_CODE_CD=1;
public static final int FORMAT_CODE_FM=2;
public static final int FORMAT_CODE_TELEPHONE=3;
public static final int FORMAT_CODE_GSM=4;
public static final int[] FORMAT_CODES={
FORMAT_CODE_GSM,
FORMAT_CODE_TELEPHONE,
FORMAT_CODE_FM,
FORMAT_CODE_CD
};
public static final int[] BUFFER_SIZE_MILLIS = {
30, 40, 50, 70, 85, 100, 130, 150, 180, 220, 400
};
public static final String[] BUFFER_SIZE_MILLIS_STR = {
"30", "40", "50", "70", "85", "100", "130", "150", "180", "220", "400"
};
public static final int BUFFER_SIZE_INDEX_DEFAULT = 2;
public static final String CONNECTION_PROPERTY = "CONNECTION";
public static final String AUDIO_PROPERTY = "AUDIO";
public static final int PROTOCOL_MAGIC = 0x43484154;
public static final int PROTOCOL_VERSION = 1;
public static final int PROTOCOL_ACK = 1001;
public static final int PROTOCOL_ERROR = 1002;
// Socket options
public static final boolean TCP_NODELAY = false;
// -1 means do not set the value
public static final int TCP_RECEIVE_BUFFER_SIZE = 1024;
public static final int TCP_SEND_BUFFER_SIZE = 1024;
public static final int CONNECTION_TYPE_TCP = 1;
public static final int CONNECTION_TYPE_UDP = 2;
public static final int[] CONNECTION_TYPES =
{
CONNECTION_TYPE_TCP,
CONNECTION_TYPE_UDP
};
public static final String[] CONNECTION_TYPE_NAMES =
{
"TCP (for LAN)",
"UDP (for WAN)"
};
public static final int DEFAULT_PORT = 8765;
public static final int DEFAULT_CONNECTION_TYPE = CONNECTION_TYPE_TCP;
public static final int DEFAULT_FORMAT_CODE = FORMAT_CODE_TELEPHONE;
public static void out(String s) {
//wSystem.out.println(s);
}
}
zarmin
Messages postés49Date d'inscriptionmardi 28 juin 2005StatutMembreDernière intervention16 avril 2008 8 janv. 2007 à 14:12
Cest assez bizarre car je peux modifier avec ce code le volume du Speaker etc mais rien du cote du microphone.
Quelqu un aurait des idees pour m aguiller merci!