Search in sources :

Example 6 with AudioBuffer

use of jmri.jmrit.audio.AudioBuffer in project JMRI by JMRI.

the class Diesel3Sound method setXml.

@Override
public void setXml(Element e, VSDFile vf) {
    Element el;
    String fn;
    D3Notch sb;
    // Handle the common stuff.
    super.setXml(e, vf);
    //log.debug("Diesel EngineSound: " + e.getAttribute("name").getValue());
    _soundName = this.getName() + ":LoopSound";
    log.debug("Diesel3: name: " + this.getName() + " soundName " + _soundName);
    notch_sounds = new HashMap<Integer, D3Notch>();
    String in = e.getChildText("idle-notch");
    Integer idle_notch = null;
    if (in != null) {
        idle_notch = Integer.parseInt(in);
    } else {
        // leave idle_notch null for now. We'll use it at the end to trigger a "grandfathering" action
        log.warn("No Idle Notch Specified!");
    }
    // Get the notch sounds
    Iterator<Element> itr = (e.getChildren("notch-sound")).iterator();
    int i = 0;
    while (itr.hasNext()) {
        el = itr.next();
        sb = new D3Notch();
        int nn = Integer.parseInt(el.getChildText("notch"));
        sb.setNotch(nn);
        if ((idle_notch != null) && (nn == idle_notch)) {
            sb.setIdleNotch(true);
            log.debug("This Notch (" + nn + ") is Idle.");
        }
        List<Element> elist = el.getChildren("file");
        int j = 0;
        for (Element fe : elist) {
            fn = fe.getText();
            //AudioBuffer b = D3Notch.getBuffer(vf, fn, "Engine_n" + i + "_" + j, "Engine_" + i + "_" + j);
            //log.debug("Buffer created: " + b + " name: " + b.getSystemName());
            //sb.addLoopBuffer(b);
            List<AudioBuffer> l = D3Notch.getBufferList(vf, fn, "Engine_n" + i + "_" + j, "Engine_" + i + "_" + j);
            log.debug("Buffers Created: ");
            for (AudioBuffer b : l) {
                log.debug("\tSubBuffer: " + b.getSystemName());
            }
            sb.addLoopBuffers(l);
            j++;
        }
        //log.debug("Notch: " + nn + " File: " + fn);
        // Gain is broken, for the moment.  Buffers don't have gain. Sources do.
        //_sound.setGain(setXMLGain(el));
        //_sound.setGain(default_gain);
        sb.setNextNotch(el.getChildText("next-notch"));
        sb.setPrevNotch(el.getChildText("prev-notch"));
        sb.setAccelLimit(el.getChildText("accel-limit"));
        sb.setDecelLimit(el.getChildText("decel-limit"));
        if (el.getChildText("accel-file") != null) {
            sb.setAccelBuffer(D3Notch.getBuffer(vf, el.getChildText("accel-file"), "Engine_na" + i, "Engine_na" + i));
        } else {
            sb.setAccelBuffer(null);
        }
        if (el.getChildText("decel-file") != null) {
            sb.setDecelBuffer(D3Notch.getBuffer(vf, el.getChildText("decel-file"), "Engine_nd" + i, "Engine_nd" + i));
        } else {
            sb.setDecelBuffer(null);
        }
        // Store in the list.
        notch_sounds.put(nn, sb);
        i++;
    }
    // Get the start and stop sounds
    el = e.getChild("start-sound");
    if (el != null) {
        fn = el.getChild("file").getValue();
        //log.debug("Start sound: " + fn);
        start_buffer = D3Notch.getBuffer(vf, fn, "Engine_start", "Engine_Start");
    }
    el = e.getChild("shutdown-sound");
    if (el != null) {
        fn = el.getChild("file").getValue();
        //log.debug("Shutdown sound: " + fn);
        stop_buffer = D3Notch.getBuffer(vf, fn, "Engine_shutdown", "Engine_Shutdown");
    }
    // all have unique notch numbers.
    if (idle_notch == null) {
        D3Notch min_notch = null;
        // No, this is not a terribly efficient "min" operation.  But that's OK.
        for (D3Notch n : notch_sounds.values()) {
            if ((min_notch == null) || (n.getNotch() < min_notch.getNotch())) {
                min_notch = n;
            }
        }
        log.debug("No Idle Notch Specified.  Choosing Notch (" + (min_notch != null ? min_notch.getNotch() : "min_notch not set") + ") to be the Idle Notch.");
        if (min_notch != null) {
            min_notch.setIdleNotch(true);
        } else {
            log.warn("Could not set idle notch because min_notch was still null");
        }
    }
    // Kick-start the loop thread.
    this.startThread();
}
Also used : Element(org.jdom2.Element) AudioBuffer(jmri.jmrit.audio.AudioBuffer)

Example 7 with AudioBuffer

use of jmri.jmrit.audio.AudioBuffer in project JMRI by JMRI.

the class Steam1Sound method setXml.

@Override
public void setXml(Element e, VSDFile vf) {
    Element el;
    String fn, n;
    S1Notch sb;
    // Handle the common stuff.
    super.setXml(e, vf);
    //log.debug("Steam EngineSound: {}", e.getAttribute("name").getValue());
    _soundName = this.getName();
    log.debug("Steam1: name: {},  soundName: {}", this.getName(), _soundName);
    // Required values
    top_speed = Integer.parseInt(e.getChildText("top-speed"));
    log.debug("top speed forward: {} MPH", top_speed);
    driver_diameter_float = Float.parseFloat(e.getChildText("driver-diameter-float"));
    log.debug("driver diameter: {} inches", driver_diameter_float);
    num_cylinders = Integer.parseInt(e.getChildText("cylinders"));
    log.debug("Number of cylinders defined: {}", num_cylinders);
    // Optional value
    // Allows to adjust speed.
    // Optional value.
    n = e.getChildText("exponent");
    if (n != null) {
        exponent = Float.parseFloat(n);
    } else {
        // default
        exponent = 1.0f;
    }
    log.debug("exponent: {}", exponent);
    // Optional value
    // Defines how many rpms in 0.5 seconds will trigger decel actions like braking.
    n = e.getChildText("decel-trigger-rpms");
    if (n != null) {
        decel_trigger_rpms = Integer.parseInt(n);
    } else {
        // Default (need a value)
        decel_trigger_rpms = 999;
    }
    log.debug("number of rpms to trigger decelerating actions: {}", decel_trigger_rpms);
    // Get the sounds.
    // Note: each sound must have equal attributes, e.g. 16-bit, 44100 Hz
    // Get the files and create a buffer and byteBuffer for each file.
    // For each notch there must be <num_cylinders * 2> chuff files.
    notch_sounds = new HashMap<>();
    // notch number (index)
    int i = 0;
    // chuff or coast number (index)
    int j = 0;
    // Sound sample format
    int fmt = 0;
    // notch number (visual)
    int nn = 1;
    // Get the notch-sounds.
    Iterator<Element> itr = (e.getChildren("s1notch-sound")).iterator();
    while (itr.hasNext()) {
        el = itr.next();
        sb = new S1Notch();
        sb.setNotch(nn);
        // Get the chuff sounds.
        List<Element> elist = el.getChildren("notch-file");
        j = 0;
        for (Element fe : elist) {
            fn = fe.getText();
            log.debug("notch: {}, file: {}", nn, fn);
            AudioBuffer b = S1Notch.getBuffer(vf, fn, _soundName + "_NOTCH_" + i + "_" + j + "_", _soundName + "_NOTCH_" + i + "_" + j + "_");
            log.debug("buffer created: {}, name: {}, format: {}", b, b.getSystemName(), b.getFormat());
            sb.addChuffBuffer(b);
            if (fmt == 0) {
                // Get the format of the (first) WAV file.
                // Since all WAV files of the notches MUST have the same format,
                // I asume this format for all WAV files for now.
                fmt = AudioUtil.getWavFormat(S1Notch.getWavStream(vf, fn));
                log.debug("fmt: {}", fmt);
            }
            ByteBuffer data = AudioUtil.getWavData(S1Notch.getWavStream(vf, fn));
            sb.addChuffData(data);
            j++;
        }
        log.debug("Number of chuff sounds for notch {} defined: {}", nn, j);
        // Create a filler Buffer for queueing and a ByteBuffer for length modification.
        fn = el.getChildText("notchfiller-file");
        if (fn != null) {
            log.debug("notch filler file: {}", fn);
            AudioBuffer bnf = S1Notch.getBuffer(vf, el.getChildText("notchfiller-file"), _soundName + "_NOTCHFILLER_" + i + "_", _soundName + "_NOTCHFILLER_" + i + "_");
            log.debug("buffer created: {}, name: {}, format: {}", bnf, bnf.getSystemName(), bnf.getFormat());
            sb.setNotchFillerBuffer(bnf);
            sb.setNotchFillerData(AudioUtil.getWavData(S1Notch.getWavStream(vf, fn)));
        } else {
            log.debug("no notchfiller available.");
            sb.setNotchFillerBuffer(null);
        }
        // VSDFile validation makes sure that there is at least one notch.
        if (nn == 1) {
            // Get the coasting sounds.
            j = 0;
            List<Element> elistc = el.getChildren("coast-file");
            for (Element fe : elistc) {
                fn = fe.getText();
                log.debug("coasting file: {}", fn);
                AudioBuffer bc = S1Notch.getBuffer(vf, fn, _soundName + "_COAST_" + j + "_", _soundName + "_COAST_" + j + "_");
                log.debug("buffer created: {}, name: {}, format: {}", bc, bc.getSystemName(), bc.getFormat());
                // WAV in Buffer for queueing.
                sb.addCoastBuffer(bc);
                ByteBuffer datac = AudioUtil.getWavData(S1Notch.getWavStream(vf, fn));
                // WAV data in ByteBuffer for length modification.
                sb.addCoastData(datac);
                j++;
            }
            log.debug("Number of coasting sounds for notch {} defined: {}", nn, j);
            // Create a filler Buffer for queueing and a ByteBuffer for length modification.
            fn = el.getChildText("coastfiller-file");
            if (fn != null) {
                log.debug("coasting filler file: {}", fn);
                AudioBuffer bcf = S1Notch.getBuffer(vf, fn, _soundName + "_COASTFILLER_", _soundName + "_COASTFILLER_");
                log.debug("buffer created: {}, name: {}, format: {}", bcf, bcf.getSystemName(), bcf.getFormat());
                sb.setCoastFillerBuffer(bcf);
                sb.setCoastFillerData(AudioUtil.getWavData(S1Notch.getWavStream(vf, fn)));
            } else {
                log.debug("no coastfiller available.");
                sb.setCoastFillerBuffer(null);
            }
            // serve well for that purpose. These buffers are bound to notch 1.
            for (int jk = 0; jk < 10; jk++) {
                AudioBuffer bh = S1Notch.getBufferHelper(_soundName + "_BUFFERHELPER_" + jk, _soundName + "_BUFFERHELPER_" + jk);
                log.debug("buffer helper created: {}, name: {}", bh, bh.getSystemName());
                sb.addHelper(bh);
            }
        }
        sb.setMinLimit(Integer.parseInt(el.getChildText("min-rpm")));
        sb.setMaxLimit(Integer.parseInt(el.getChildText("max-rpm")));
        sb.setBufferFmt(fmt);
        log.debug("sample format for notch {}: {}", nn, fmt);
        // Store in the list.
        notch_sounds.put(nn, sb);
        i++;
        nn++;
    }
    log.debug("Number of notches defined: {}", notch_sounds.size());
    // Get the trigger sounds.
    // Note: other than notch sounds, trigger sounds can have different attributes.
    trigger_sounds = new HashMap<>();
    // Get the idle sound.
    el = e.getChild("idle-sound");
    if (el != null) {
        fn = el.getChild("sound-file").getValue();
        log.debug("idle sound: {}", fn);
        idle_sound = new SoundBite(vf, fn, _soundName + "Idle", _soundName + "Idle");
        // Handle gain
        idle_sound.setGain(setXMLGain(el));
        log.debug("idle sound gain: {}", idle_sound.getGain());
        idle_sound.setLooped(true);
        idle_sound.setFadeTimes(500, 500);
        trigger_sounds.put("idle", idle_sound);
        log.debug("trigger sound idle: {}", trigger_sounds.get("idle"));
    }
    // Get the brake sound.
    el = e.getChild("brake-sound");
    if (el != null) {
        fn = el.getChild("sound-file").getValue();
        log.debug("brake sound: {}", fn);
        brake_sound = new SoundBite(vf, fn, _soundName + "Brake", _soundName + "Brake");
        brake_sound.setGain(setXMLGain(el));
        log.debug("brake sound gain: {}", brake_sound.getGain());
        brake_sound.setLooped(false);
        brake_sound.setFadeTimes(500, 500);
        trigger_sounds.put("brake", brake_sound);
        log.debug("trigger sound brake: {}", trigger_sounds.get("brake"));
    }
    // Get the pre-arrival sound
    el = e.getChild("pre-arrival-sound");
    if (el != null) {
        fn = el.getChild("sound-file").getValue();
        log.debug("pre-arrival sound: {}", fn);
        pre_arrival_sound = new SoundBite(vf, fn, _soundName + "Pre-arrival", _soundName + "Pre-arrival");
        pre_arrival_sound.setGain(setXMLGain(el));
        log.debug("pre-arrival sound gain: {}", pre_arrival_sound.getGain());
        pre_arrival_sound.setLooped(false);
        pre_arrival_sound.setFadeTimes(500, 500);
        log.debug("getGain pre-arrival-sound: {}", pre_arrival_sound.getGain());
        trigger_sounds.put("pre_arrival", pre_arrival_sound);
        log.debug("trigger sound pre_arrival: {}", trigger_sounds.get("pre_arrival"));
    }
    // Kick-start the loop thread.
    this.startThread();
}
Also used : Element(org.jdom2.Element) AudioBuffer(jmri.jmrit.audio.AudioBuffer) ByteBuffer(java.nio.ByteBuffer)

Aggregations

AudioBuffer (jmri.jmrit.audio.AudioBuffer)7 Element (org.jdom2.Element)4 AudioException (jmri.AudioException)3 AudioManager (jmri.AudioManager)3 ByteBuffer (java.nio.ByteBuffer)2 Audio (jmri.Audio)2 AudioListener (jmri.jmrit.audio.AudioListener)2 AudioSource (jmri.jmrit.audio.AudioSource)2 AL (com.jogamp.openal.AL)1 ALException (com.jogamp.openal.ALException)1 ALut (com.jogamp.openal.util.ALut)1 InputStream (java.io.InputStream)1 ByteOrder (java.nio.ByteOrder)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Vector3f (javax.vecmath.Vector3f)1 Attribute (org.jdom2.Attribute)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1