2 * Copyright (c) 2003-2009 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 package com.jmex.audio.openal;
36 import java.io.IOException;
38 import java.net.URLDecoder;
39 import java.util.logging.Level;
40 import java.util.logging.Logger;
42 import com.jcraft.jorbis.JOrbisException;
43 import com.jcraft.jorbis.VorbisFile;
44 import com.jmex.audio.AudioTrack;
45 import com.jmex.audio.stream.AudioInputStream;
46 import com.jmex.audio.stream.OggInputStream;
47 import com.jmex.audio.stream.WavInputStream;
48 import com.jmex.audio.util.AudioLoader;
52 * @author Joshua Slack
53 * @version $Id: OpenALAudioTrack.java,v 1.7 2007/12/03 17:59:28 nca Exp $
55 public class OpenALAudioTrack extends AudioTrack {
56 private static final Logger logger = Logger
57 .getLogger(OpenALAudioTrack.class.getName());
59 public OpenALAudioTrack(URL resource, boolean stream) {
60 super(resource, stream);
61 if (resource != null) {
64 Format type = AudioInputStream.sniffFormat(resource.openStream());
65 if (Format.WAV.equals(type)) {
66 WavInputStream inputStream = new WavInputStream(resource);
67 setPlayer(new OpenALStreamedAudioPlayer(inputStream, this));
68 } else if (Format.OGG.equals(type)) {
72 if (!resource.getProtocol().equals("file")) {
73 vf = new VorbisFile(resource.openStream(),
77 URLDecoder.decode(new File(resource.getFile()).getPath(), "UTF-8"));
79 length = vf.time_total(-1);
80 } catch (JOrbisException e) {
81 logger.log(Level.WARNING, "Error creating VorbisFile", e);
83 OggInputStream inputStream = new OggInputStream(resource, length);
84 setPlayer(new OpenALStreamedAudioPlayer(inputStream, this));
86 throw new IllegalArgumentException("Given url is not a recognized audio type. Must be OGG or RIFF/WAV: "+resource);
89 } catch (IOException e) {
90 logger.logp(Level.SEVERE, this.getClass().toString(),
91 "OpenALAudioTrack(URL resource, boolean stream)", "Exception", e);
94 OpenALAudioBuffer buffer = OpenALAudioBuffer.generateBuffer();
96 AudioLoader.fillBuffer(buffer, resource);
97 } catch (IOException e) {
98 logger.logp(Level.SEVERE, this.getClass().toString(),
99 "OpenALAudioTrack(URL resource, boolean stream)", "Exception", e);
102 setPlayer(new OpenALMemoryAudioPlayer(buffer, this));
107 public OpenALAudioTrack(URL resource, OpenALAudioBuffer buffer) {
108 super(resource, false);
109 setPlayer(new OpenALMemoryAudioPlayer(buffer, this));
112 public OpenALAudioTrack(URL resource, AudioInputStream inputStream) {
113 super(resource, true);
114 setPlayer(new OpenALStreamedAudioPlayer(inputStream, this));