OSDN Git Service

f208d0533f2d35b9f444c4c5b79a711bfbe7f43f
[mikumikustudio/MikuMikuStudio.git] / src / com / jmex / audio / openal / OpenALAudioTrack.java
1 /*
2  * Copyright (c) 2003-2009 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32
33 package com.jmex.audio.openal;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.net.URL;
38 import java.net.URLDecoder;
39 import java.util.logging.Level;
40 import java.util.logging.Logger;
41
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;
49
50 /**
51  * @see AudioTrack
52  * @author Joshua Slack
53  * @version $Id: OpenALAudioTrack.java,v 1.7 2007/12/03 17:59:28 nca Exp $
54  */
55 public class OpenALAudioTrack extends AudioTrack {
56     private static final Logger logger = Logger
57             .getLogger(OpenALAudioTrack.class.getName());
58
59     public OpenALAudioTrack(URL resource, boolean stream) {
60         super(resource, stream);
61         if (resource != null) {
62             if (stream) {
63                 try {
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)) {
69                         float length = -1; 
70                         try {
71                             VorbisFile vf;
72                             if (!resource.getProtocol().equals("file")) {
73                                 vf = new VorbisFile(resource.openStream(),
74                                         null, 0);
75                             } else {
76                                 vf = new VorbisFile(
77                                         URLDecoder.decode(new File(resource.getFile()).getPath(), "UTF-8"));
78                             }
79                             length = vf.time_total(-1);
80                         } catch (JOrbisException e) {
81                             logger.log(Level.WARNING, "Error creating VorbisFile", e);
82                         }
83                         OggInputStream inputStream = new OggInputStream(resource, length);
84                         setPlayer(new OpenALStreamedAudioPlayer(inputStream, this));
85                     } else {
86                         throw new IllegalArgumentException("Given url is not a recognized audio type. Must be OGG or RIFF/WAV: "+resource);
87                     }
88                     getPlayer().init();
89                 } catch (IOException e) {
90                     logger.logp(Level.SEVERE, this.getClass().toString(),
91                             "OpenALAudioTrack(URL resource, boolean stream)", "Exception", e);
92                 }
93             } else {
94                 OpenALAudioBuffer buffer = OpenALAudioBuffer.generateBuffer();
95                 try {
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);
100                     return;
101                 }
102                 setPlayer(new OpenALMemoryAudioPlayer(buffer, this));
103             }
104         }
105     }
106
107     public OpenALAudioTrack(URL resource, OpenALAudioBuffer buffer) {
108         super(resource, false);
109         setPlayer(new OpenALMemoryAudioPlayer(buffer, this));
110     }
111
112     public OpenALAudioTrack(URL resource, AudioInputStream inputStream) {
113         super(resource, true);
114         setPlayer(new OpenALStreamedAudioPlayer(inputStream, this));
115     }
116
117 }