Java Code Examples for javax.sound.sampled.Clip#setFramePosition()

The following examples show how to use javax.sound.sampled.Clip#setFramePosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WaveEngine.java    From javagame with MIT License 8 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example 2
Source File: WaveEngine.java    From javagame with MIT License 7 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example 3
Source File: ClipOpenBug.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    boolean res = true;
    try {
        AudioInputStream ais = new AudioInputStream(
                new ByteArrayInputStream(new byte[2000]),
                new AudioFormat(8000.0f, 8, 1, false, false), 2000); //
        AudioFormat format = ais.getFormat();
        DataLine.Info info = new DataLine.Info(Clip.class, format,
                                               ((int) ais.getFrameLength()
                                                        * format
                                                       .getFrameSize()));
        Clip clip = (Clip) AudioSystem.getLine(info);
        clip.open();
        FloatControl rateControl = (FloatControl) clip.getControl(
                FloatControl.Type.SAMPLE_RATE);
        int c = 0;
        while (c++ < 10) {
            clip.stop();
            clip.setFramePosition(0);
            clip.start();
            for (float frq = 22000; frq < 44100; frq = frq + 100) {
                try {
                    Thread.currentThread().sleep(20);
                } catch (Exception e) {
                    break;
                }
                rateControl.setValue(frq);
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        res = ex.getMessage().indexOf(
                "This method should not have been invoked!") < 0;
    }
    if (res) {
        System.out.println("Test passed");
    } else {
        System.out.println("Test failed");
        throw new Exception("Test failed");
    }
}
 
Example 4
Source File: MultiClip.java    From opsu-dance with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Plays the clip with the specified volume.
 * @param volume the volume the play at
 * @param listener the line listener
 * @throws LineUnavailableException if a clip object is not available or
 *         if the line cannot be opened due to resource restrictions
 */
public void start(float volume, LineListener listener) throws LineUnavailableException {
	Clip clip = getClip();
	if (clip == null)
		return;

	// PulseAudio does not support Master Gain
	if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
		// set volume
		FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
		float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
		gainControl.setValue(dB);
	}

	if (listener != null)
		clip.addLineListener(listener);
	clip.setFramePosition(0);
	clip.start();
}
 
Example 5
Source File: WaveEngine.java    From nullpomino with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Playback
 * @param name Registered name
 */
public void play(String name) {
	Clip clip = clipMap.get(name);

	if(clip != null) {
		// Stop
		clip.stop();
		// Playback position back to the beginning
		clip.setFramePosition(0);
		// Playback
		clip.start();
	}
}
 
Example 6
Source File: WaveEngine.java    From nullpomino with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void update(LineEvent event) {
	// If you stop playback or to the end
	if(event.getType() == LineEvent.Type.STOP) {
		Clip clip = (Clip) event.getSource();
		clip.stop();
		clip.setFramePosition(0); // Playback position back to the beginning
	}
}
 
Example 7
Source File: MultiClip.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Plays the clip with the specified volume.
 * @param volume the volume the play at
 * @param listener the line listener
 * @throws LineUnavailableException if a clip object is not available or
 *         if the line cannot be opened due to resource restrictions
 */
public void start(float volume, LineListener listener) throws LineUnavailableException {
	Clip clip = getClip();
	if (clip == null)
		return;

	// PulseAudio does not support Master Gain
	if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
		// set volume
		FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
		float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
		gainControl.setValue(Utils.clamp(dB, gainControl.getMinimum(), gainControl.getMaximum()));
	} else if (clip.isControlSupported(FloatControl.Type.VOLUME)) {
		// The docs don't mention what unit "volume" is supposed to be,
		// but for PulseAudio it seems to be amplitude
		FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.VOLUME);
		float amplitude = (float) Math.sqrt(volume) * volumeControl.getMaximum();
		volumeControl.setValue(Utils.clamp(amplitude, volumeControl.getMinimum(), volumeControl.getMaximum()));
	}

	if (listener != null)
		clip.addLineListener(listener);
	clip.setFramePosition(0);
	clip.start();
}
 
Example 8
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example 9
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example 10
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example 11
Source File: WaveEngine.java    From javagame with MIT License 6 votes vote down vote up
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
Example 12
Source File: Sound.java    From WorldGrower with GNU General Public License v3.0 5 votes vote down vote up
public void play() {
	try {
		final Clip clipToPlay;
		if (preLoadedClip != null) {
			clipToPlay = preLoadedClip;
			clipToPlay.setFramePosition(0);
		} else {
			clipToPlay = openClip(CLOSE_AFTER_PLAYING);
		}
		clipToPlay.start();

	} catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
		throw new IllegalStateException(e);
	}
}
 
Example 13
Source File: bug6251460.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static protected void test()
        throws LineUnavailableException, InterruptedException {
    DataLine.Info info = new DataLine.Info(Clip.class, format);
    Clip clip = (Clip)AudioSystem.getLine(info);
    final MutableBoolean clipStoppedEvent = new MutableBoolean(false);
    clip.addLineListener(new LineListener() {
        @Override
        public void update(LineEvent event) {
            if (event.getType() == LineEvent.Type.STOP) {
                synchronized (clipStoppedEvent) {
                    clipStoppedEvent.value = true;
                    clipStoppedEvent.notifyAll();
                }
            }
        }
    });
    clip.open(format, soundData, 0, soundData.length);

    long lengthClip = clip.getMicrosecondLength() / 1000;
    log("Clip length " + lengthClip + " ms");
    log("Playing...");
    for (int i=1; i<=LOOP_COUNT; i++) {
        long startTime = currentTimeMillis();
        log(" Loop " + i);
        clip.start();

        synchronized (clipStoppedEvent) {
            while (!clipStoppedEvent.value) {
                clipStoppedEvent.wait();
            }
            clipStoppedEvent.value = false;
        }

        long endTime = currentTimeMillis();
        long lengthPlayed = endTime - startTime;

        if (lengthClip > lengthPlayed + 20) {
            log(" ERR: Looks like sound didn't play: played " + lengthPlayed + " ms instead " + lengthClip);
            countErrors++;
        } else {
            log(" OK: played " + lengthPlayed + " ms");
        }
        clip.setFramePosition(0);

    }
    log("Played " + LOOP_COUNT + " times, " + countErrors + " errors detected.");
}