S
steven.sampson
I have this idea of making a modem, that requires the soundcard to act
as my A/D and D/A, and in the middle will be my attempts at DSP. I
came up with this object, and it seems to test OK, but I could use
some constructive criticism, as I am definately at the OO 101 stage.
Merry Christmas, and a Happy New Years soon!
/**
* SoundCard.java
*
* @version 1.0, 1 December 2007
* @author Steve Sampson, K5OKC
*
* Public Domain (p) December 2007
*/
package modem;
import javax.sound.sampled.*;
import java.io.*;
public class SoundCard {
private static AudioFormat format;
private static TargetDataLine targetLine;
private static SourceDataLine sourceLine;
private static AudioInputStream sound;
public void SoundCard() {
}
/*
* Initialize Sound Card
*/
public boolean initPCM(double dblSampleRate) {
/*
* Encoding,
* Sample Rate (float),
* Sample Size (In Bits),
* Channels,
* Frame Size,
* Frame Rate (float),
* BigEndian (Boolean)
*/
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
(float)dblSampleRate,
16,
1,
2,
(float)dblSampleRate,
false);
DataLine.Info targetInfo = new
DataLine.Info(TargetDataLine.class, format);
DataLine.Info sourceInfo = new
DataLine.Info(SourceDataLine.class, format);
try {
/*
* A source data line is written to
*/
sourceLine =
(SourceDataLine)AudioSystem.getLine(sourceInfo);
sourceLine.open(format); // Typical buffer size is same
as samplerate in bytes
sourceLine.start();
/*
* A target data line is read from
*/
targetLine =
(TargetDataLine)AudioSystem.getLine(targetInfo);
targetLine.open(format); // Typical buffer size is same
as samplerate in bytes
targetLine.start();
}
catch (LineUnavailableException e1) {
return false;
}
catch (SecurityException e2) {
return false;
}
catch (IllegalArgumentException e3) {
return false;
}
catch (IllegalStateException e4) {
return false;
}
if(!AudioSystem.isLineSupported(targetInfo)) {
return false;
}
sound = new AudioInputStream(targetLine);
return true;
}
/*
* Stop, flush, and close the soundcard interface
*/
public void closePCM() {
sourceLine.stop();
targetLine.stop();
sourceLine.flush();
targetLine.flush();
try {
sourceLine.close();
targetLine.close();
}
catch (SecurityException e)
{
}
}
/*
* Provide a block of Signed 16 bit PCM sound data
*/
public int getPCM(int[] intSampleArray) {
int intBytesRead;
byte[] byteData = new byte[intSampleArray.length * 2]; //
Convert count to bytes
/*
* Read bytes into array. This call will block if there aren't
* enough requested bytes.
*/
try {
intBytesRead = sound.read(byteData, 0, byteData.length);
} catch(IOException e) {
return -1;
}
/*
* This is designed for Little-Endian Intel machines
*/
for (int i = 0, j = 0; i < intBytesRead; i += 2, j++)
intSampleArray[j] = (byteData[i + 1] << 8) + byteData;
return intBytesRead / 2; // convert count to words
}
}
as my A/D and D/A, and in the middle will be my attempts at DSP. I
came up with this object, and it seems to test OK, but I could use
some constructive criticism, as I am definately at the OO 101 stage.
Merry Christmas, and a Happy New Years soon!
/**
* SoundCard.java
*
* @version 1.0, 1 December 2007
* @author Steve Sampson, K5OKC
*
* Public Domain (p) December 2007
*/
package modem;
import javax.sound.sampled.*;
import java.io.*;
public class SoundCard {
private static AudioFormat format;
private static TargetDataLine targetLine;
private static SourceDataLine sourceLine;
private static AudioInputStream sound;
public void SoundCard() {
}
/*
* Initialize Sound Card
*/
public boolean initPCM(double dblSampleRate) {
/*
* Encoding,
* Sample Rate (float),
* Sample Size (In Bits),
* Channels,
* Frame Size,
* Frame Rate (float),
* BigEndian (Boolean)
*/
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
(float)dblSampleRate,
16,
1,
2,
(float)dblSampleRate,
false);
DataLine.Info targetInfo = new
DataLine.Info(TargetDataLine.class, format);
DataLine.Info sourceInfo = new
DataLine.Info(SourceDataLine.class, format);
try {
/*
* A source data line is written to
*/
sourceLine =
(SourceDataLine)AudioSystem.getLine(sourceInfo);
sourceLine.open(format); // Typical buffer size is same
as samplerate in bytes
sourceLine.start();
/*
* A target data line is read from
*/
targetLine =
(TargetDataLine)AudioSystem.getLine(targetInfo);
targetLine.open(format); // Typical buffer size is same
as samplerate in bytes
targetLine.start();
}
catch (LineUnavailableException e1) {
return false;
}
catch (SecurityException e2) {
return false;
}
catch (IllegalArgumentException e3) {
return false;
}
catch (IllegalStateException e4) {
return false;
}
if(!AudioSystem.isLineSupported(targetInfo)) {
return false;
}
sound = new AudioInputStream(targetLine);
return true;
}
/*
* Stop, flush, and close the soundcard interface
*/
public void closePCM() {
sourceLine.stop();
targetLine.stop();
sourceLine.flush();
targetLine.flush();
try {
sourceLine.close();
targetLine.close();
}
catch (SecurityException e)
{
}
}
/*
* Provide a block of Signed 16 bit PCM sound data
*/
public int getPCM(int[] intSampleArray) {
int intBytesRead;
byte[] byteData = new byte[intSampleArray.length * 2]; //
Convert count to bytes
/*
* Read bytes into array. This call will block if there aren't
* enough requested bytes.
*/
try {
intBytesRead = sound.read(byteData, 0, byteData.length);
} catch(IOException e) {
return -1;
}
/*
* This is designed for Little-Endian Intel machines
*/
for (int i = 0, j = 0; i < intBytesRead; i += 2, j++)
intSampleArray[j] = (byteData[i + 1] << 8) + byteData;
return intBytesRead / 2; // convert count to words
}
}