/** Creates a new SoundManager with the specified maximum number of simultaneous sounds. */ public SoundManager(AudioFormat playbackFormat, int maxSimultaneousSounds) { super(Math.min(maxSimultaneousSounds, getMaxSimultaneousSounds(playbackFormat))); this.playbackFormat = playbackFormat; localLine = new ThreadLocal(); localBuffer = new ThreadLocal(); pausedLock = new Object(); // notify threads in pool it's ok to start synchronized (this) { notifyAll(); } }
/** バイト列に周波数、ベロシティ指定で波形書き込む */ public void writeWave(byte[] b, double frequency, int velocity) { double amplitude = sampleRate / frequency; // 波長 for (int i = 0; i < b.length; i++) { double r = i / amplitude; int v = (byte) ((Math.round(r) % 2 == 0) ? velocity : -velocity); v += b[i]; v = Math.min(Math.max(v, -128), 127); b[i] = (byte) v; } // 再生(バイト列をlineに書き込む) line.write(b, 0, b.length); }
@Override public int onAudioData(byte[] data) { if (audioLine != null && audioLine.isOpen()) { if (!audioLine.isRunning()) { audioLine.start(); } int toWrite = Math.min(audioLine.available(), data.length); if (toWrite == audioLine.available()) log.trace("full! toWrite: " + toWrite + " instead of: " + data.length); return audioLine.write(data, 0, toWrite); } else { return 0; } }