Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Renamed BiquadFilter

  • Loading branch information...
commit f9596e0812d36b3c74549e7638bcea2584a2413c 1 parent 1a02aae
@bencall authored
View
74 src/BiquadFilter.java
@@ -0,0 +1,74 @@
+/**
+ * Implementation of the biquad filter. Not sure it is correct
+ * @author bencall
+ *
+ */
+public class BiquadFilter {
+ double bf_playback_rate = 1.0;
+ double bf_est_drift = 0.0; // local clock is slower by
+ biquad_t bf_drift_lpf;
+ double bf_est_err = 0.0, bf_last_err;
+ biquad_t bf_err_lpf, bf_err_deriv_lpf;
+ double desired_fill;
+ int fill_count;
+ int sampling_rate;
+ int frame_size;
+
+ private static final double CONTROL_A = (1e-4);
+ private static final double CONTROL_B = (1e-1);
+
+ public BiquadFilter(int sampling_rate, int frame_size){
+ this.sampling_rate = sampling_rate;
+ this.frame_size = frame_size;
+ bf_drift_lpf = biquad_lpf(1.0/180.0, 0.3);
+ bf_err_lpf = biquad_lpf(1.0/10.0, 0.25);
+ bf_err_deriv_lpf = biquad_lpf(1.0/2.0, 0.2);
+ fill_count = 0;
+ bf_playback_rate = 1.0;
+ bf_est_err = 0;
+ bf_last_err = 0;
+ desired_fill = 0;
+ fill_count = 0;
+ }
+
+ private biquad_t biquad_lpf(double freq, double Q) {
+ biquad_t ret = new biquad_t();
+
+ double w0 = 2*Math.PI*freq/((float)sampling_rate/(float)frame_size);
+ double alpha = Math.sin(w0)/(2.0*Q);
+
+ double a_0 = 1.0 + alpha;
+ ret.b[0] = (1.0-Math.cos(w0))/(2.0*a_0);
+ ret.b[1] = (1.0-Math.cos(w0))/a_0;
+ ret.b[2] = ret.b[0];
+ ret.a[0] = -2.0*Math.cos(w0)/a_0;
+ ret.a[1] = (1-alpha)/a_0;
+
+ return ret;
+ }
+
+ public void update(int fill){
+ if (fill_count < 1000) {
+ desired_fill += (double)fill/1000.0;
+ fill_count++;
+ return;
+ }
+
+ double buf_delta = fill - desired_fill;
+ bf_est_err = filter(bf_err_lpf, buf_delta);
+ double err_deriv = filter(bf_err_deriv_lpf, bf_est_err - bf_last_err);
+
+ bf_est_drift = filter(bf_drift_lpf, CONTROL_B*(bf_est_err*CONTROL_A + err_deriv) + bf_est_drift);
+
+ bf_playback_rate = 1.0 + CONTROL_A*bf_est_err + bf_est_drift;
+ bf_last_err = bf_est_err;
+ }
+
+ private double filter(biquad_t bq, double in) {
+ double w = in - bq.a[0]*bq.hist[0] - bq.a[1]*bq.hist[1];
+// double out = bq.b[1]*bq.hist[0] + bq.b[2]*bq.hist[1] + bq.b[0]*w;
+ bq.hist[1] = bq.hist[0];
+ bq.hist[0] = w;
+ return w;
+ }
+}
View
3  src/LaunchThread.java
@@ -27,7 +27,7 @@ public LaunchThread(String name){
public void run(){
int port = 5000;
- byte[] hwAddr = null;;
+ byte[] hwAddr = null;
InetAddress local;
try {
@@ -41,6 +41,7 @@ public void run(){
} catch (SocketException e) {
e.printStackTrace();
}
+
StringBuilder sb = new StringBuilder();
for (byte b : hwAddr) {
sb.append(String.format("%02x", b));
View
52 src/RTSPResponder.java
@@ -27,7 +27,7 @@
*
*/
public class RTSPResponder extends Thread{
-
+
private ServerSocket sock; // Initial socket
private Socket socket; // Connected socket
private int[] fmtp;
@@ -35,7 +35,7 @@
private AudioServer serv; // Audio listener
byte[] hwAddr;
boolean stopThread = false;
-
+
private String key =
"-----BEGIN RSA PRIVATE KEY-----\n"
+"MIIEpQIBAAKCAQEA59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUt\n"
@@ -68,7 +68,7 @@
*/
public RTSPResponder(int port, byte[] hwAddr) throws IOException{
this.hwAddr = hwAddr;
-
+
// Try to take the given port. If not possible, take another. If not possible: ERROR
try {
sock = new ServerSocket(5000);
@@ -76,8 +76,8 @@ public RTSPResponder(int port, byte[] hwAddr) throws IOException{
sock = new ServerSocket();
}
}
-
-
+
+
public void stopThread() throws IOException{
stopThread = true;
if(serv != null){
@@ -85,17 +85,17 @@ public void stopThread() throws IOException{
}
sock.close();
}
-
-
+
+
/**
* @return port number
*/
public int getPort(){
return sock.getLocalPort();
}
-
+
public void handlePacket(RTSPPacket packet){
-
+
// We init the response holder
StringBuilder response = new StringBuilder("RTSP/1.0 200 OK\r\n");
response.append("Audio-Jack-Status: connected; type=analog\r\n");
@@ -121,13 +121,13 @@ public void handlePacket(RTSPPacket packet){
out.write(ip);
// HW-Addr
out.write(hwAddr);
-
+
// Pad to 32 Bytes
int padLen = 32 - out.size();
for(int i = 0; i < padLen; ++i) {
out.write(0x00);
}
-
+
} catch (IOException e) {
e.printStackTrace();
}
@@ -200,7 +200,7 @@ public void handlePacket(RTSPPacket packet){
// Launching audioserver
serv = new AudioServer(new AudioSession(aesiv, aeskey, fmtp, controlPort, timingPort));
-
+
response.append("Transport: " + packet.valueOfHeader("Transport") + ";server_port=" + serv.getServerPort() + "\r\n");
// ??? Why ???
@@ -237,7 +237,7 @@ public void handlePacket(RTSPPacket packet){
System.out.println(packet.getRawPacket());
System.out.println(response.toString());
-
+
// Write the packet to the wire
BufferedWriter oStream;
try {
@@ -270,7 +270,7 @@ public void handlePacket(RTSPPacket packet){
PEMReader pemReader = new PEMReader(new StringReader(key));
KeyPair pObj = (KeyPair) pemReader.readObject();
-
+
// Encrypt
Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, pObj.getPrivate());
@@ -279,10 +279,10 @@ public void handlePacket(RTSPPacket packet){
}catch(Exception e){
e.printStackTrace();
}
-
+
return null;
}
-
+
/**
* Decrypt with RSA priv key
* @param array
@@ -292,10 +292,10 @@ public void handlePacket(RTSPPacket packet){
try{
Security.addProvider(new BouncyCastleProvider());
- // La clŽ RSA
+ // La cl RSA
PEMReader pemReader = new PEMReader(new StringReader(key));
KeyPair pObj = (KeyPair) pemReader.readObject();
-
+
// Encrypt
Cipher cipher = Cipher.getInstance("RSA/NONE/OAEPPadding");
cipher.init(Cipher.DECRYPT_MODE, pObj.getPrivate());
@@ -304,26 +304,26 @@ public void handlePacket(RTSPPacket packet){
}catch(Exception e){
e.printStackTrace();
}
-
+
return null;
}
-
+
/**
* Thread to listen packets
*/
public void run() {
boolean fin = stopThread;
try {
-
+
// Socket & Streams
// Socket init
if(socket == null && !stopThread){
socket = sock.accept();
}
-
+
if(socket != null){
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
-
+
String packet = "";
while(!fin){
// Buffer
@@ -331,7 +331,7 @@ public void run() {
in.read(buffer);
String temp = new String(buffer);
packet = packet + temp;
-
+
// If packet completed
Pattern p = Pattern.compile("(.*)\r\n\r\n");
Matcher m = p.matcher(packet);
@@ -343,7 +343,7 @@ public void run() {
packet = "";
break;
}
-
+
synchronized(this){
fin = stopThread;
}
@@ -355,4 +355,4 @@ public void run() {
e.printStackTrace();
}
}
-}
+}
Please sign in to comment.
Something went wrong with that request. Please try again.