OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [examples/] [gnu/] [classpath/] [examples/] [sound/] [AudioPlayerSample.java] - Blame information for rev 781

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 781 jeremybenn
/* AudioPlayerSample.java -- Simple Java Audio Player
2
   Copyright (C) 2007 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
package gnu.classpath.examples.sound;
38
 
39
import java.io.File;
40
import java.io.IOException;
41
import java.util.Map;
42
 
43
import javax.sound.sampled.AudioFormat;
44
import javax.sound.sampled.AudioInputStream;
45
import javax.sound.sampled.AudioSystem;
46
import javax.sound.sampled.DataLine;
47
import javax.sound.sampled.LineUnavailableException;
48
import javax.sound.sampled.SourceDataLine;
49
import javax.sound.sampled.UnsupportedAudioFileException;
50
 
51
/**
52
 * A simple demo to show the use of the Java Sound API.
53
 * It plays the given file (up to the end, so don't pass the 26 minutes long
54
 * Pink Floyd's Echoes unless you really want!!).
55
 *
56
 * See: http://jsresources.org/examples/SimpleAudioPlayer.java.html
57
 *
58
 * @author Mario Torre <neugens@limasoftware.net>
59
 */
60
public class AudioPlayerSample
61
{
62
  private static final int EXTERNAL_BUFFER_SIZE = 128000;
63
 
64
  /**
65
   * @param args
66
   */
67
  public static void main(String[] args)
68
  {
69
    if (args.length < 1)
70
      {
71
        System.out.println("Radio Classpath -: Usage: " +
72
                           "AudioPlayerSample [file]");
73
        return;
74
      }
75
 
76
    String file = args[0];
77
 
78
    System.out.println("Welcome to Radio Classpath, only great music for you!");
79
    System.out.println("Today's DJ Tap The WaterDroplet");
80
 
81
    // now create the AudioInputStream
82
    AudioInputStream audioInputStream = null;
83
    try
84
      {
85
        audioInputStream = AudioSystem.getAudioInputStream(new File(file));
86
      }
87
    catch (UnsupportedAudioFileException e)
88
      {
89
        // This happen when the subsystem is unable to parse the kind of
90
        // audio file we are submitting
91
        // See the README for supported audio file types under Classpath
92
        // for the version you are using.
93
        e.printStackTrace();
94
        return;
95
      }
96
    catch (IOException e)
97
      {
98
        e.printStackTrace();
99
        return;
100
      }
101
 
102
    // get informations about the kind of file we are about to play
103
    AudioFormat audioFormat = audioInputStream.getFormat();
104
 
105
    System.out.println("Playing file: " + file);
106
    System.out.println("format: " + audioFormat.toString());
107
 
108
    System.out.print("Additional properties: ");
109
 
110
    // now, we try to get all the properties we have in this AudioFormat
111
    // and display them
112
    Map<String, Object> properties = audioFormat.properties();
113
    if (properties.size() < 0)
114
      {
115
        System.out.println("none");
116
      }
117
    else
118
      {
119
        System.out.println("found #" + properties.size() + " properties");
120
        for (String key : properties.keySet())
121
          {
122
            System.out.println(key + ": " + properties.get(key));
123
          }
124
      }
125
 
126
    // let's setup things for playing
127
    // first, we require a Line. As we are doing playing, we will ask for a
128
    // SourceDataLine
129
    SourceDataLine line = null;
130
 
131
    // To get the source line, we first need to build an Info object
132
    // this is done in one line:
133
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
134
 
135
    System.out.println("searching line...");
136
 
137
    // usually, if a backend can parse a file type, it can also
138
    // create a line to handle it, but that's not guaranteed
139
    // so we need to take care and to handle a possible
140
    // LineUnavailableException
141
    try
142
      {
143
        line = (SourceDataLine) AudioSystem.getLine(info);
144
 
145
        System.out.println("line found, opening...");
146
 
147
        // once created, a line must be opened to let data flow
148
        // though it.
149
        line.open(audioFormat);
150
      }
151
    catch (LineUnavailableException e)
152
      {
153
        // in a real application you should signal that in a kindly way to
154
        // your users
155
        e.printStackTrace();
156
        return;
157
      }
158
    catch (Exception e)
159
      {
160
        e.printStackTrace();
161
        return;
162
      }
163
 
164
    // an open line pass data to the backend only when it is in
165
    // a state called "started" ("playing" or "play" in some other
166
    // framework)
167
    System.out.print("starting line... ");
168
 
169
    line.start();
170
    System.out.println("done");
171
 
172
    // now we can start reading data from the AudioStream and writing
173
    // data to the pipeline. The Java Sound API is rather low level
174
    // so let you pass up to one byte of data at a time
175
    // (with some constraints, refer to the API documentation to know more)
176
    // We will do some buffering. You may want to check the frame size
177
    // to allow a better buffering, also.
178
 
179
    System.out.println("now playing...");
180
 
181
    int nBytesRead = 0;
182
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
183
    while (nBytesRead != - 1)
184
      {
185
        try
186
          {
187
            nBytesRead = audioInputStream.read(abData, 0, abData.length);
188
          }
189
        catch (IOException e)
190
          {
191
            e.printStackTrace();
192
          }
193
 
194
        if (nBytesRead >= 0)
195
          {
196
            // this method returns the number of bytes actuall written
197
            // to the line. You may want to use this number to check
198
            // for events, display the current position (give also a
199
            // look to the API for other ways of doing that) etc..
200
            line.write(abData, 0, nBytesRead);
201
          }
202
      }
203
 
204
    System.out.print("stream finished, draining line... ");
205
 
206
    // call this method to ensure that all the data in the internal buffer
207
    // reach the audio backend, otherwise your application will
208
    // cut the last frames of audio data (and users will not enjoy the last
209
    // seconds of their precious music)
210
    line.drain();
211
 
212
    // Once done, we can close the line. Note that a line, once closed
213
    // may not be reopened (depends on the backend, in some cases a "reopen",
214
    // if allowed, really opens a new line, reallocating all the resources)
215
 
216
    System.out.println("line drained, now exiting");
217
    line.close();
218
 
219
    System.out.println("We hope you enjoyed Radio Classpath!");
220
  }
221
 
222
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.