File: PlayerTest.java

package info (click to toggle)
libvorbisspi-java 1.0.3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: java: 1,481; xml: 66; makefile: 15
file content (199 lines) | stat: -rw-r--r-- 6,525 bytes parent folder | download | duplicates (5)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 *   PlayerTest.
 * 
 *   JavaZOOM : vorbisspi@javazoom.net
 *              http://www.javazoom.net
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as published
 *   by the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.Properties;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javazoom.spi.PropertiesContainer;
import junit.framework.TestCase;

/**
 * Simple player (based on Vorbis SPI) unit test.
 * It takes around 3%-5% of CPU and 10MB RAM under Win2K/P4/2.4GHz/JDK1.4.1
 * It takes around 4% of CPU and 10MB RAM under Win2K/Athlon/1GHz/JDK1.3.1
 */
public class PlayerTest extends TestCase
{
	private String basefile=null;
	private String filename=null;
	private String name=null;
    private String baseurl=null;
    private String fileurl=null;
	private Properties props = null;
	private PrintStream out = null;
	
	/**
	 * Constructor for PlayerTest.
	 * @param arg0
	 */
	public PlayerTest(String arg0)
	{
		super(arg0);
	}

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception
	{
		super.setUp();
		props = new Properties();
		InputStream pin = getClass().getClassLoader().getResourceAsStream("test.ogg.properties");
		props.load(pin);
		basefile = (String) props.getProperty("basefile");
        baseurl = (String) props.getProperty("baseurl");
		name = (String) props.getProperty("filename");		
		filename = basefile + name;
        String stream = (String) props.getProperty("stream");
        if (stream != null) fileurl = stream;
        else fileurl = baseurl + name;        
		out = System.out;
	}

	public void testPlayFile()
	{
	 try
	 {
		if (out != null) out.println("---  Start : "+filename+"  ---");
		File file = new File(filename);
		AudioFileFormat aff = AudioSystem.getAudioFileFormat(file);
		if (out != null) out.println("Audio Type : "+aff.getType());
		AudioInputStream in= AudioSystem.getAudioInputStream(file);
		AudioInputStream din = null;
		if (in != null)
		{
		  AudioFormat baseFormat = in.getFormat();
		  if (out != null) out.println("Source Format : "+baseFormat.toString());
		  AudioFormat  decodedFormat = new AudioFormat(
			  AudioFormat.Encoding.PCM_SIGNED,
			  baseFormat.getSampleRate(),
			  16,
			  baseFormat.getChannels(),
			  baseFormat.getChannels() * 2,
			  baseFormat.getSampleRate(),
			  false);
		  if (out != null) out.println("Target Format : "+decodedFormat.toString());
		  din = AudioSystem.getAudioInputStream(decodedFormat, in);
		  if (din instanceof PropertiesContainer)
		  {
			assertTrue("PropertiesContainer : OK",true);
		  }
		  else
		  {
			assertTrue("Wrong PropertiesContainer instance",false);
		  }
		  rawplay(decodedFormat, din);
		  in.close();		
		  if (out != null) out.println("---  Stop : "+filename+"  ---");
		  assertTrue("testPlay : OK",true);
		}
	 }
	 catch (Exception e)
	 {
		assertTrue("testPlay : "+e.getMessage(),false);
	 }
	}

    public void _testPlayURL()
    {
     try
     {
        if (out != null) out.println("---  Start : "+fileurl+"  ---");
        URL url = new URL(fileurl);
        AudioFileFormat aff = AudioSystem.getAudioFileFormat(url);
        if (out != null) out.println("Audio Type : "+aff.getType());
        AudioInputStream in= AudioSystem.getAudioInputStream(url);
        AudioInputStream din = null;
        if (in != null)
        {
          AudioFormat baseFormat = in.getFormat();
          if (out != null) out.println("Source Format : "+baseFormat.toString());
          AudioFormat  decodedFormat = new AudioFormat(
              AudioFormat.Encoding.PCM_SIGNED,
              baseFormat.getSampleRate(),
              16,
              baseFormat.getChannels(),
              baseFormat.getChannels() * 2,
              baseFormat.getSampleRate(),
              false);
          if (out != null) out.println("Target Format : "+decodedFormat.toString());
          din = AudioSystem.getAudioInputStream(decodedFormat, in);
          if (din instanceof PropertiesContainer)
          {
            assertTrue("PropertiesContainer : OK",true);
          }
          else
          {
            assertTrue("Wrong PropertiesContainer instance",false);
          }
          rawplay(decodedFormat, din);
          in.close();       
          if (out != null) out.println("---  Stop : "+filename+"  ---");
          assertTrue("testPlay : OK",true);
        }
     }
     catch (Exception e)
     {
        assertTrue("testPlay : "+e.getMessage(),false);
     }
    }

    private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
	{
	  SourceDataLine res = null;
	  DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
	  res = (SourceDataLine) AudioSystem.getLine(info);
	  res.open(audioFormat);
	  return res;
	}
	
	private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException
	{
		byte[] data = new byte[4096];
		SourceDataLine line = getLine(targetFormat);		
		if (line != null)
		{
		  // Start
		  line.start();
		  int nBytesRead = 0, nBytesWritten = 0;
		  while (nBytesRead != -1)
		  {
			nBytesRead = din.read(data, 0, data.length);
			if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
		  }
		  // Stop
		  line.drain();
		  line.stop();
		  line.close();
		  din.close();
		}		
	}
}