Skip to content

Commit

Permalink
Working!!! Next: clean the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
bencall committed May 1, 2011
1 parent e939b50 commit 8720594
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/AudioServer.java
Expand Up @@ -54,7 +54,7 @@ public class AudioServer implements UDPDelegate{
* @param controlPort
* @param timingPort
*/
public AudioServer(byte[] aesiv, byte[] aeskey, int[] fmtp, int controlPort, int timingPort){
public AudioServer(byte[] aesiv, byte[] aeskey, int[] fmtp, int controlPort, int timingPort){
// Init instance var
this.fmtp = fmtp;
this.aesiv = aesiv;
Expand Down Expand Up @@ -134,6 +134,10 @@ public int[] getNextFrame(){
}


public biquadFilter getFilter(){
return bFilter;
}

/**
* Sets the packets as not ready. Audio thread will only listen to ready packets.
* No audio more.
Expand Down
68 changes: 62 additions & 6 deletions src/PCMPlayer.java
Expand Up @@ -14,6 +14,8 @@ public class PCMPlayer extends Thread{
Info info;
SourceDataLine dataLine;
AudioServer server;
long fix_volume = 0x10000;
short rand_a, rand_b;

public PCMPlayer(AudioServer server){
super();
Expand All @@ -39,16 +41,70 @@ public void run(){
if(buf==null){
continue;
}
byte[] input = new byte[buf.length*2];

for(int i=0; i<buf.length; i++){
//(byte)(value >>> 24), (byte)(value >> 16 & 0xff), (byte)(value >> 16 & 0xff), (byte)(value & 0xff)
input[i] = (byte)(buf[i] >> 16 & 0xff);
input[i+1] = (byte)(buf[i] & 0xff);
int[] outbuf = new int[4*(server.getFrameSize()+3)];
int k = stuff_buffer(server.getFilter().bf_playback_rate, buf, outbuf);

byte[] input = new byte[outbuf.length*2];

int j = 0;
for(int i=0; i<outbuf.length; i++){
input[j++] = (byte)(outbuf[i] >> 8);
input[j++] = (byte)(outbuf[i]);
}

System.err.println("Play!!!");
dataLine.write(input, 0, server.getFrameSize()*2);
dataLine.write(input, 0, k*4);
}
}

private int stuff_buffer(double playback_rate, int[] input, int[] output) {

int stuffsamp = server.getFrameSize();
int stuff = 0;
double p_stuff;

p_stuff = 1.0 - Math.pow(1.0 - Math.abs(playback_rate-1.0), server.getFrameSize());

if (Math.random() < p_stuff) {
stuff = playback_rate > 1.0 ? -1 : 1;
stuffsamp = (int) (Math.random() * (server.getFrameSize() - 2));
}

int j=0;
int l=0;
for (int i=0; i<stuffsamp; i++) { // the whole frame, if no stuffing
output[j++] = dithered_vol(input[l++]);
output[j++] = dithered_vol(input[l++]);
}

if (stuff!=0) {
if (stuff==1) {
// interpolate one sample
output[j++] = dithered_vol(((int)input[l-2] + (int)input[l]) >> 1);
output[j++] = dithered_vol(((int)input[l-1] + (int)input[l+1]) >> 1);
} else if (stuff==-1) {
l-=2;
}
for (int i=stuffsamp; i<server.getFrameSize() + stuff; i++) {
output[j++] = dithered_vol(input[l++]);
output[j++] = dithered_vol(input[l++]);
}
}
return server.getFrameSize() + stuff;
}


private short dithered_vol(int sample) {
long out;
rand_b = rand_a;
rand_a = (short) (Math.random() * 65535);

out = (long)sample * fix_volume;
if (fix_volume < 0x10000) {
out += rand_a;
out -= rand_b;
}
return (short) (out>>16);
}
}

0 comments on commit 8720594

Please sign in to comment.