created project for sound API sound
authorAnton Epple <toni.epple@eppleton.de>
Mon, 27 May 2013 16:59:48 +0200
branchsound
changeset 1078b55cc88621f
parent 106 877f18756b74
child 108 7cc01dacf0e2
created project for sound API
sound/src/main/java/net/java/html/sound/api/AudioClip.java
sound/src/main/java/net/java/html/sound/spi/AudioEnvironment.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sound/src/main/java/net/java/html/sound/api/AudioClip.java	Mon May 27 16:59:48 2013 +0200
     1.3 @@ -0,0 +1,103 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
     1.6 + * <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify it under
     1.9 + * the terms of the GNU General Public License as published by the Free Software
    1.10 + * Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.14 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    1.15 + * details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License along with
    1.18 + * this program. Look for COPYING file in the top folder. If not, see
    1.19 + * http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package net.java.html.sound.api;
    1.22 +
    1.23 +import java.util.ServiceLoader;
    1.24 +import net.java.html.sound.spi.AudioEnvironment;
    1.25 +
    1.26 +/**
    1.27 + *
    1.28 + * @author antonepple
    1.29 + */
    1.30 +public final class AudioClip {
    1.31 +
    1.32 +    private Object cached;
    1.33 +    private int cacheHash;
    1.34 +    private final String src;
    1.35 +    private final AudioEnvironment audioEnvironment;
    1.36 +
    1.37 +    private AudioClip(String src) {
    1.38 +        this.src = src;
    1.39 +        ServiceLoader<AudioEnvironment> loader = ServiceLoader.load(AudioEnvironment.class);
    1.40 +        audioEnvironment = loader.iterator().next();
    1.41 +    }
    1.42 +
    1.43 +    public static AudioClip create(String src){
    1.44 +        return new AudioClip(src);
    1.45 +    }
    1.46 +    
    1.47 +    public void play() {
    1.48 +        Object nativeClip = audioEnvironment.play(this, cached);
    1.49 +        cache(nativeClip);
    1.50 +    }
    1.51 +
    1.52 +    public void pause() {
    1.53 +        Object nativeClip = audioEnvironment.pause(this, cached);
    1.54 +        cache(nativeClip);
    1.55 +    }
    1.56 +
    1.57 +    public void stop() {
    1.58 +        Object nativeClip = audioEnvironment.stop(this, cached);
    1.59 +        cache(nativeClip);
    1.60 +    }
    1.61 +    
    1.62 +    public void setVolume(int volume) {
    1.63 +        Object nativeClip = audioEnvironment.setVolume(this, volume, cached);
    1.64 +        cache(nativeClip);
    1.65 +    }
    1.66 +    
    1.67 +    public void playFrom(int seconds){
    1.68 +        Object nativeClip = audioEnvironment.playFrom(this, seconds, cached);
    1.69 +        cache(nativeClip);
    1.70 +    }
    1.71 +
    1.72 +    void cache(Object toCache) {
    1.73 +        cacheHash = hashCode();
    1.74 +        this.cached = toCache;
    1.75 +    }
    1.76 +
    1.77 +    private boolean isCached() {
    1.78 +        return cacheHash == hashCode();
    1.79 +    }
    1.80 +
    1.81 +    Object getCached() {
    1.82 +        return isCached() ? cached : null;
    1.83 +    }
    1.84 +
    1.85 +    @Override
    1.86 +    public int hashCode() {
    1.87 +        int hash = 5;
    1.88 +        hash = 59 * hash + (this.src != null ? this.src.hashCode() : 0);
    1.89 +        return hash;
    1.90 +    }
    1.91 +
    1.92 +    @Override
    1.93 +    public boolean equals(Object obj) {
    1.94 +        if (obj == null) {
    1.95 +            return false;
    1.96 +        }
    1.97 +        if (getClass() != obj.getClass()) {
    1.98 +            return false;
    1.99 +        }
   1.100 +        final AudioClip other = (AudioClip) obj;
   1.101 +        if ((this.src == null) ? (other.src != null) : !this.src.equals(other.src)) {
   1.102 +            return false;
   1.103 +        }
   1.104 +        return true;
   1.105 +    }
   1.106 +}
   1.107 \ No newline at end of file
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/sound/src/main/java/net/java/html/sound/spi/AudioEnvironment.java	Mon May 27 16:59:48 2013 +0200
     2.3 @@ -0,0 +1,38 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
     2.6 + * <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify it under
     2.9 + * the terms of the GNU General Public License as published by the Free Software
    2.10 + * Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    2.14 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
    2.15 + * details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License along with
    2.18 + * this program. Look for COPYING file in the top folder. If not, see
    2.19 + * http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package net.java.html.sound.spi;
    2.22 +
    2.23 +import net.java.html.sound.api.AudioClip;
    2.24 +
    2.25 +/**
    2.26 + *
    2.27 + * @author antonepple
    2.28 + */
    2.29 +public interface AudioEnvironment {
    2.30 +
    2.31 +    public  Object play(AudioClip aThis, Object cached);
    2.32 +
    2.33 +    public  Object pause(AudioClip aThis, Object cached);
    2.34 +
    2.35 +    public  Object stop(AudioClip aThis, Object cached);
    2.36 +
    2.37 +    public  Object setVolume(AudioClip aThis, int volume, Object cached);
    2.38 +
    2.39 +    public  Object playFrom(AudioClip aThis, int seconds, Object cached);
    2.40 +    
    2.41 +}