PdOffline.java 文件源码

java
阅读 19 收藏 0 点赞 0 评论 0

项目:gdx-pd 作者:
/**
 * Bake a path to a wav file
 * @param patch the patch to bake
 * @param wav the wav file to write
 * @param channels how many channels (1 for mono, 2 for stereo, can be more than 2 channels)
 * @param sampleRate sample rate used by Pd
 * @param time baking duration in seconds
 * @throws IOException
 */
public static void bake(File patch, File wav, int channels, int sampleRate, float time) throws IOException {

    // disable Pd : does nothing if Pd alreay initialized.
    PdConfiguration.disabled = true;

    // Pause audio.
    // Does nothing in headless mode but required to 
    // have Pd static code executed (load library)
    Pd.audio.pause();

    int handle = PdBase.openPatch(patch);
    PdBase.openAudio(0, channels, sampleRate);
    PdBase.computeAudio(true);

    int frames = (int)(time * sampleRate);
    int samples = frames * channels;
    short [] data = new short[samples];
    int ticks = frames / PdBase.blockSize();
    PdBase.process(ticks, new short[]{}, data);

    PdBase.closePatch(handle);

    // save
    byte [] buf = new byte[data.length * 2];
    for(int i=0 ; i<data.length ; i++){
        buf[i*2+0] = (byte)(data[i] & 0xFF);
        buf[i*2+1] = (byte)((data[i] >> 8) & 0xFF);
    }

    ByteArrayInputStream stream = new ByteArrayInputStream(buf);
    AudioFormat format = new AudioFormat(sampleRate, 16, channels, true, false);
    AudioInputStream audioStream = new AudioInputStream(stream, format, data.length);
    AudioSystem.write(audioStream, Type.WAVE, wav);

    // resume audio
    Pd.audio.resume();
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号