aboutsummaryrefslogtreecommitdiff
path: root/CS2452/Skype/src/Skype/speaker.java
blob: 596986f9d80419600432e61b818dc402bc0c255d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package Skype;

import java.util.*;
import java.util.concurrent.Semaphore;
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

class speaker extends Thread
{
	Socket s;
	AudioFormat format;
	audio aud;
	Semaphore flg;
	Gui g;

	public speaker(Socket socket, AudioFormat format, audio aud, Semaphore audflg, Gui g)
	{
		this.g = g;
		this.aud = aud;
		this.flg = audflg;
		s = socket;
		this.format = format;
	}

	public void run()
	{
			try
			{
				DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
				final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
				line.open(format, 1024);
				line.start();

				final AudioInputStream ais = new AudioInputStream(s.getInputStream(), format, 2000000);
				int count;
				int bufferSize = (int) format.getSampleRate() * format.getFrameSize();
				byte buffer[] = new byte[bufferSize];

				//System.out.printf("speakers started\n");
				while (((count = ais.read(buffer, 0, buffer.length)) != -1) && aud.open)
				{
					flg.release();
					if (count > 0)
					{
						if (flg.tryAcquire())
						{

							for (int i = 0; i < buffer.length; i++ )
							{
								buffer[i] *= aud.vol;
							}
							flg.release();
						}

						line.write(buffer, 0, count);
					}

				}
				line.drain();
				line.close();
			} catch (IOException e)
			{
				System.err.println("I/O problems: " + e);

			} catch (LineUnavailableException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try
			{
				flg.acquire();
			} catch (InterruptedException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		//System.out.println("out of while");
		try
		{
			s.close();
			//System.out.println("S closed");
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}