public SonarSoundEngine(int maxChannels) throws LineUnavailableException { silentSample = new SonarSample(new float[] {0}, 44100); Mixer mixer = AudioSystem.getMixer(null); sdl = (SourceDataLine) mixer.getLine(new Line.Info(SourceDataLine.class)); sdl.open(new AudioFormat(rate, 16, 2, true, false), bufferSize * 2 * 2 * 2 * 2 * 2); soundBuffer.order(ByteOrder.LITTLE_ENDIAN); sdl.start(); try { /* FloatControl volumeControl = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN); volumeControl.setValue(volumeControl.getMaximum());*/ } catch (IllegalArgumentException e) { // System.out.println("Failed to set the sound volume"); } listenerMixer = new ListenerMixer(maxChannels); leftBuf = new float[bufferSize]; rightBuf = new float[bufferSize]; Thread thread = new Thread(this); thread.setDaemon(true); thread.setPriority(10); thread.start(); }
public static void main(String[] args) throws Exception { SoundUtils.laser(5); Thread.sleep(1000); SoundUtils.warp(10); Thread.sleep(1000); SoundUtils.bang(); SoundUtils.tone(5000, 100); Thread.sleep(1000); }
@Override public void run() { while (!stopped) { synchronized (this) { if (updateRequired_0) { if (frequency_0 == 0) { clip0.stop(); } else { playFrequency(clip0, frequency_0); } updateRequired_0 = false; } } synchronized (this) { if (updateRequired_1) { if (frequency_1 == 0) { clip1.stop(); } else { playFrequency(clip1, frequency_1); } updateRequired_1 = false; } } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } }
public static void laser(int repeat) throws LineUnavailableException, InterruptedException { AudioFormat af = new AudioFormat( SAMPLE_RATE, // sampleRate 8, // sampleSizeInBits 1, // channels true, // signed false); // bigEndian SourceDataLine sdl = AudioSystem.getSourceDataLine(af); sdl.open(af); sdl.start(); byte[] buf = new byte[1]; int step; for (int j = 0; j < repeat; j++) { step = 10; for (int i = 0; i < 2000; i++) { buf[0] = ((i % step > 0) ? 32 : (byte) 0); if (i % 250 == 0) step += 2; sdl.write(buf, 0, 1); } Thread.sleep(200); } sdl.drain(); sdl.stop(); sdl.close(); }
public int write(byte[] data, int ofs, int len) { int maxTries = 10; int totWrite = 0; int nofs = ofs, nlen = len; while (nofs < ofs + len && maxTries > 0) { int written = 0; if (format.getBits() == 16 && format.getChannels() == 2 && format.getRate() == INPUT_RATE) written = sdl.write(data, nofs, nlen); else written = writeConv(data, nofs, nlen); nofs += written; nlen -= written; totWrite += written; maxTries--; deliveredData += written; // calculations when channels and bits and rate varies... // int divisor = (format.getBits() / 8) * format.getChannels(); // int deliveredTime = // (int) (1000 * deliveredData / (format.getRate() * divisor)); int deliveredTime = (int) (1000 * deliveredData / (INPUT_RATE * 4)); long pos = sdl.getMicrosecondPosition() / 1000; long sleepTime = deliveredTime - pos - bufferTime; if (sleepTime < 0) sleepTime = 0; try { Thread.sleep(sleepTime); } catch (InterruptedException e) { } } return totWrite; }
/** Signals that a PooledThread has started. Creates the Thread's line and buffer. */ protected void threadStarted() { // wait for the SoundManager constructor to finish synchronized (this) { try { wait(); } catch (InterruptedException ex) { } } // use a short, 100ms (1/10th sec) buffer for filters that // change in real-time int bufferSize = playbackFormat.getFrameSize() * Math.round(playbackFormat.getSampleRate() / 10); // create, open, and start the line SourceDataLine line; DataLine.Info lineInfo = new DataLine.Info(SourceDataLine.class, playbackFormat); try { line = (SourceDataLine) AudioSystem.getLine(lineInfo); line.open(playbackFormat, bufferSize); } catch (LineUnavailableException ex) { // the line is unavailable - signal to end this thread Thread.currentThread().interrupt(); return; } line.start(); // create the buffer byte[] buffer = new byte[bufferSize]; // set this thread's locals localLine.set(line); localBuffer.set(buffer); }
private void leuchten(JLabel label, String farbe) { try { if (!leuchten) { Thread.sleep(200); label.setIcon(new ImageIcon(farbe + "_0.png")); Thread.sleep(200); label.setIcon(new ImageIcon(farbe + "_1.png")); Thread.sleep(200); label.setIcon(new ImageIcon(farbe + "_0.png")); Thread.sleep(200); label.setIcon(new ImageIcon(farbe + "_1.png")); leuchten = true; } } catch (Exception e) { } }
private void neueFarbe() { try { if ((runde % 2) == 0 && wait > 25) { wait = wait - 25; } klick = 0; durchgang++; zahl = zufall.nextInt(4); switch (zahl) { case (0): rot.setIcon(new ImageIcon("files/Minigames/Senso/red_1.png")); soundAbspielen(soundR); einfuegen(runde, 1); Thread.sleep(wait + 25); rot.setIcon(new ImageIcon("files/Minigames/Senso/red_0.png")); break; case (1): gruen.setIcon(new ImageIcon("files/Minigames/Senso/green_1.png")); soundAbspielen(soundG); einfuegen(runde, 2); Thread.sleep(wait + 25); gruen.setIcon(new ImageIcon("files/Minigames/Senso/green_0.png")); break; case (2): blau.setIcon(new ImageIcon("files/Minigames/Senso/blue_1.png")); soundAbspielen(soundB); einfuegen(runde, 3); Thread.sleep(wait + 25); blau.setIcon(new ImageIcon("files/Minigames/Senso/blue_0.png")); break; case (3): gelb.setIcon(new ImageIcon("files/Minigames/Senso/yellow_1.png")); soundAbspielen(soundY); einfuegen(runde, 4); Thread.sleep(wait + 25); gelb.setIcon(new ImageIcon("files/Minigames/Senso/yellow_0.png")); break; } durchgang = 0; runde++; neu = false; kopieren(); } catch (Exception ex) { } }
public void start(Game.Spiel senso) { this.setVisible(true); // new Senso(); s = senso; neustarten(true); läuft = true; spiel = new Thread(this); spiel.start(); }
public AudioPlayer(final JFrame frame) { clipListener = new LineListener() { public void update(LineEvent event) { if (event.getType() != STOP) return; clip.removeLineListener(clipListener); clip = null; synchronized (notificationMutex) { playing = false; notificationMutex.notifyAll(); } } }; worker = new Thread( new Runnable() { public void run() { while (true) { synchronized (notificationMutex) { try { notificationMutex.wait(1000); } catch (InterruptedException e) { // ignore this } if (!running) return; if (playing) continue; } File file = null; try { file = queue.poll(); if (file != null) playNext(file); } catch (Exception e) { log.severe( format("Cannot play audio file %s: %s", file, getLocalizedMessage(e))); showMessageDialog( frame, MessageFormat.format( Application.getInstance() .getContext() .getBundle() .getString("cannot-play-voice"), file, e), frame.getTitle(), ERROR_MESSAGE); } } } }, "AudioPlayer"); worker.start(); }
private static synchronized void ckpl() { if (enabled) { if (player == null) { player = new Player(); player.start(); } } else { ncl.clear(); } }
/** 结束音乐线程 */ public void stop() { if (m_midi == null) { musicThread.interrupt(); musicThread = null; m_stopped = true; } else { m_midi.stop(); m_midi.close(); } }
/** 开始音乐线程 */ public void start() { // midi和wav需要不同的方法 if (m_midi == null) { musicThread = new Thread(this); musicThread.start(); m_stopped = false; } else { m_midi.InitSequence(m_filename); m_midi.play(m_looped); } }
//////////////////////////////////////////////////////////////////////////// // needed for implement runnable public void run() { while (true) { animate(); repaint(); double seconds = 0.04; // time that 1 frame takes. int miliseconds = (int) (1000.0 * seconds); try { Thread.sleep(miliseconds); } catch (InterruptedException e) { } } }
/* * Maintenance to be done before playing; pause/mute management. */ private static void prePlay() { if (muted) { return; } while (paused) { try { Thread.sleep(100); } catch (InterruptedException ie) { // empty } } }
private void alteFarbe() { try { int alt = 0; for (int i = 0; i < farben.length; i++) { durchgang++; alt = farben[i]; Thread.sleep(wait); switch (alt) { case 1: rot.setIcon(new ImageIcon("files/Minigames/Senso/red_1.png")); soundAbspielen(soundR); Thread.sleep(wait + 25); rot.setIcon(new ImageIcon("files/Minigames/Senso/red_0.png")); break; case 2: gruen.setIcon(new ImageIcon("files/Minigames/Senso/green_1.png")); soundAbspielen(soundG); Thread.sleep(wait + 25); gruen.setIcon(new ImageIcon("files/Minigames/Senso/green_0.png")); break; case 3: blau.setIcon(new ImageIcon("files/Minigames/Senso/blue_1.png")); soundAbspielen(soundB); Thread.sleep(wait + 25); blau.setIcon(new ImageIcon("files/Minigames/Senso/blue_0.png")); break; case 4: gelb.setIcon(new ImageIcon("files/Minigames/Senso/yellow_1.png")); soundAbspielen(soundY); Thread.sleep(wait + 25); gelb.setIcon(new ImageIcon("files/Minigames/Senso/yellow_0.png")); break; } } wiedergeben = false; neu = true; } catch (Exception e) { } }
public static void main(String[] args) { try { byte[] wav = Base64Coder.base64ToBinary(getWaveLiteral().toCharArray(), 0, WAV_SIZE); InputStream is = new ByteArrayInputStream(wav); AudioFormat fmt = AudioSystem.getAudioFileFormat(is).getFormat(); AudioInputStream sound = AudioSystem.getAudioInputStream(is); DataLine.Info info = new DataLine.Info(Clip.class, fmt); Clip clip = (Clip) AudioSystem.getLine(info); clip.open(sound); clip.start(); Thread.sleep(3000); } catch (Exception ex) { ex.printStackTrace(); } }
/** * this will try to write all the indicated data whitout ever giving up... Either all the data is * written or it will loop for ever. * * @param data * @param ofs * @param len */ private void writeData(byte[] data, int ofs, int len) { int count = 0; int written = 0; while (written < len) { written += sdl.write(data, ofs + written, len - written); // take a litle break if we couldn't write everything in 100 // tries... count++; if (count >= 100) { try { Thread.sleep(10); } catch (InterruptedException e) { } count = 0; } } }
@Override public void run() { try { AudioFormat format = settings.getAudioFormat(); DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format); SourceDataLine speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo); speakers.open(format); speakers.start(); byte[] data = new byte[204800]; while (!Thread.interrupted()) { int numBytesRead = receiver.getInputStream().read(data, 0, 1024); log.debug("[Player] received {} bytes", numBytesRead); speakers.write(data, 0, numBytesRead); } } catch (LineUnavailableException | IOException e) { log.error("[Player] error occurred", e); } }
public void run() { try { while (läuft) { Thread.sleep(1); if (verloren) { verloren(); } else { if (intro) { rot.setIcon(new ImageIcon("files/Minigames/Senso/red_n.png")); gruen.setIcon(new ImageIcon("files/Minigames/Senso/green_n.png")); blau.setIcon(new ImageIcon("files/Minigames/Senso/blue_n.png")); gelb.setIcon(new ImageIcon("files/Minigames/Senso/yellow_n.png")); lblVerloren.setText(""); start.setText("3"); Thread.sleep(1000); start.setText("2"); Thread.sleep(1000); start.setText("1"); Thread.sleep(1000); start.setLocation(start.getX() - 20, start.getY()); start.setText("Los!"); Thread.sleep(250); start.setText(""); start.setLocation(start.getX() + 20, start.getY()); rot.setIcon(new ImageIcon("files/Minigames/Senso/red_0.png")); gruen.setIcon(new ImageIcon("files/Minigames/Senso/green_0.png")); blau.setIcon(new ImageIcon("files/Minigames/Senso/blue_0.png")); gelb.setIcon(new ImageIcon("files/Minigames/Senso/yellow_0.png")); intro = false; neu = true; } if (wiedergeben && !verloren) { Thread.sleep(500); alteFarbe(); } else if (neu && !verloren) neueFarbe(); if (!wiedergeben && !neu) { String s = "Noch "; vKlicks = runde - klick; if (vKlicks >= 1) s = s + String.valueOf(vKlicks); else s = ""; klicks.setText(s); } else klicks.setText(""); } } } catch (Exception e) { } }
private void fade(FloatControl control, double to) { //noinspection SynchronizationOnLocalVariableOrMethodParameter synchronized (control) { int steps = FADE_DURATION / FADE_STEP_DURATION; to = (to <= 0.0) ? 0.0001 : ((to > 1.0) ? 1.0 : to); float currDB = control.getValue(); float toDB = getDb(to); float diffStep = (toDB - currDB) / (float) steps; for (int i = 0; i < steps; i++) { currDB += diffStep; currDB = (Math.abs(currDB - toDB) < Math.abs(diffStep * 1.5)) ? toDB : currDB; control.setValue(currDB); try { Thread.sleep(FADE_STEP_DURATION); } catch (Exception ignored) { ignored.printStackTrace(); } } } }
public void run() { SourceDataLine line = null; try { try { line = (SourceDataLine) AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, fmt)); line.open(fmt, bufsize); line.start(); } catch (Exception e) { e.printStackTrace(); return; } byte[] buf = new byte[1024]; //noinspection InfiniteLoopStatement while (true) { if (Thread.interrupted()) throw (new InterruptedException()); synchronized (queuemon) { Collection<Runnable> queue = Audio.queue; Audio.queue = new LinkedList<Runnable>(); for (Runnable r : queue) r.run(); } synchronized (ncl) { for (CS cs : ncl) clips.add(cs); ncl.clear(); } fillbuf(buf, 0, 1024); //noinspection StatementWithEmptyBody for (int off = 0; off < buf.length; off += line.write(buf, off, buf.length - off)) ; } } catch (InterruptedException ignored) { } finally { synchronized (Audio.class) { player = null; } if (line != null) line.close(); } }
public static void sleep(int ms) { try { Thread.sleep(ms); } catch (Exception e) { } }
public void start() { if (relaxer == null) { relaxer = new Thread(this); relaxer.start(); } }
public void stop() { if (relaxer.isAlive()) { relaxer.stop(); } relaxer = null; }
sound(String _name) { soundFile = new File(_name); myThread = new Thread(this); myThread.start(); }