尝试在 Java 中扭曲音频

发布于 2024-11-02 17:45:19 字数 4374 浏览 12 评论 0原文

我找到了一个名为 JVST 的库,它可以加载 VST 文件并相应地过滤音频。问题是,我不知道如何使用它。我所能做的就是从网站上复制并粘贴示例代码并根据我的需要进行调整,但它仍然不起作用。我想做的是扭曲用户选择模拟音箱的 .au 或 .wav 文件中的音频。我不确定多频段压缩是否是最好的选择,但我会尝试一下,如果失败,我可以使用大量自定义 VST 插件。无论如何,这是代码:

import sun.audio.*; //import the sun.audio package
import java.awt.*;
import java.io.*;
import org.boris.jvst.*;
@SuppressWarnings("serial")
public class SoundPlayer extends Frame implements FilenameFilter {
Button openButton = new Button("Open");  
Button playButton = new Button("Play");
Button loopButton = new Button("Loop");
Button stopButton = new Button("Stop");
Label filename = new Label("                   ");
File theFile = null;
@SuppressWarnings({ "restriction" })
AudioData theData = null;
InputStream nowPlaying = null;

@SuppressWarnings({ "deprecation" })
public SoundPlayer() {
    super("Boombox");
    resize(300, 200);
    Panel north = new Panel();
    north.setLayout(new FlowLayout(FlowLayout.LEFT));
    north.add(new Label("File: "));
    north.add("North", filename);
    add("North", north);
    Panel south = new Panel();
    south.add(openButton);
    south.add(playButton);
    south.add(loopButton);
    south.add(stopButton);
    add("South", south);
}

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    SoundPlayer sp = new SoundPlayer();
    sp.show();
}

@SuppressWarnings({ "deprecation", "restriction" })
public void open() {
    FileDialog fd = new FileDialog(this, "Please select a .au or .wav file:");
    fd.setFilenameFilter(this);
    fd.show();
    try {
        theFile = new File(fd.getDirectory() + "/" + fd.getFile());
        if (theFile != null) {
            filename.setText(theFile.getName());
            FileInputStream fis = new FileInputStream(theFile);
            AudioStream as = new AudioStream(fis);
            theData = as.getData();
        }
    }
    catch (IOException e) {
        System.err.println(e);
    }
}

@SuppressWarnings("restriction")
public void play() {
    stop();    
    if (theData == null) open();
    if (theData != null) {
        AudioDataStream ads = new AudioDataStream(theData);
        AudioPlayer.player.start(ads);
        nowPlaying = ads;
    }
}

@SuppressWarnings("restriction")
public void stop() {
    if (nowPlaying != null) {
        AudioPlayer.player.stop(nowPlaying);
        nowPlaying = null;
    }
}

@SuppressWarnings("restriction")
public void loop() {
    stop();
    if (theData == null) open();
    if (theData != null) {
        ContinuousAudioDataStream cads = new ContinuousAudioDataStream(theData);
        AudioPlayer.player.start(cads);
        nowPlaying = cads;
    }
}

public boolean action(Event e, Object what) {

    if (e.target == playButton) {
        play();
        return true;
    }
    else if (e.target == openButton) {
        open();
        return true;
    }
    else if (e.target == loopButton) {
        loop();
        return true;
    }
    else if (e.target == stopButton) {
        stop();
        return true;
    }

    return false;

}

public boolean accept(File dir, String name) {

    name = name.toLowerCase();
    if (name.endsWith(".au")) return true;
    if (name.endsWith(".wav")) return true;
    return false;
    public static void main(String[] args); throws Exception {
        AEffect a = VST.load("C:/Program Files (x86)/Audacity 1.3 Beta (Unicode)/Plug-Ins/mda MultiBand.dll");
        a.open();
        a.setSampleRate(44100.0f);
        a.setBlockSize(512);

        // attempt some processing
        int blocksize = 512;
        float[][] inputs = new float[a.numInputs][];
        for (int i = 0; i < a.numInputs; i++) {
            inputs[i] = new float[blocksize];
            for (int j = 0; j < blocksize; j++)
                inputs[i][j] = (float) Math
                .sin(j * Math.PI * 2 * 440 / 44100.0);
        }
        float[][] outputs = new float[a.numOutputs][];
        for (int i = 0; i < a.numOutputs; i++) {
            outputs[i] = new float[blocksize];
            for (int j = 0; j < blocksize; j++)
                outputs[i][j] = 0;
        }

        a.processReplacing(inputs, outputs, blocksize);

        VST.dispose(a);
    }
}
}

编辑 它给出的错误是:

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at SoundPlayer.main(SoundPlayer.java:35)

I found a library called JVST that can load VST files and filter the audio accordingly. The problem is, I have no clue how to use it. All I could do is copy and paste the example code from the website and adapt it to my needs, yet it still doesn't work. What I'm trying to do is distort the audio from a .au or .wav file that the user selects to simulate a boombox. I'm not sure if multi-band compression is the best thing to use, but I'm going to try it, and if it fails, I have a load of custom VST plugins at my disposal. Anyways, here's the code:

import sun.audio.*; //import the sun.audio package
import java.awt.*;
import java.io.*;
import org.boris.jvst.*;
@SuppressWarnings("serial")
public class SoundPlayer extends Frame implements FilenameFilter {
Button openButton = new Button("Open");  
Button playButton = new Button("Play");
Button loopButton = new Button("Loop");
Button stopButton = new Button("Stop");
Label filename = new Label("                   ");
File theFile = null;
@SuppressWarnings({ "restriction" })
AudioData theData = null;
InputStream nowPlaying = null;

@SuppressWarnings({ "deprecation" })
public SoundPlayer() {
    super("Boombox");
    resize(300, 200);
    Panel north = new Panel();
    north.setLayout(new FlowLayout(FlowLayout.LEFT));
    north.add(new Label("File: "));
    north.add("North", filename);
    add("North", north);
    Panel south = new Panel();
    south.add(openButton);
    south.add(playButton);
    south.add(loopButton);
    south.add(stopButton);
    add("South", south);
}

@SuppressWarnings("deprecation")
public static void main(String[] args) {
    SoundPlayer sp = new SoundPlayer();
    sp.show();
}

@SuppressWarnings({ "deprecation", "restriction" })
public void open() {
    FileDialog fd = new FileDialog(this, "Please select a .au or .wav file:");
    fd.setFilenameFilter(this);
    fd.show();
    try {
        theFile = new File(fd.getDirectory() + "/" + fd.getFile());
        if (theFile != null) {
            filename.setText(theFile.getName());
            FileInputStream fis = new FileInputStream(theFile);
            AudioStream as = new AudioStream(fis);
            theData = as.getData();
        }
    }
    catch (IOException e) {
        System.err.println(e);
    }
}

@SuppressWarnings("restriction")
public void play() {
    stop();    
    if (theData == null) open();
    if (theData != null) {
        AudioDataStream ads = new AudioDataStream(theData);
        AudioPlayer.player.start(ads);
        nowPlaying = ads;
    }
}

@SuppressWarnings("restriction")
public void stop() {
    if (nowPlaying != null) {
        AudioPlayer.player.stop(nowPlaying);
        nowPlaying = null;
    }
}

@SuppressWarnings("restriction")
public void loop() {
    stop();
    if (theData == null) open();
    if (theData != null) {
        ContinuousAudioDataStream cads = new ContinuousAudioDataStream(theData);
        AudioPlayer.player.start(cads);
        nowPlaying = cads;
    }
}

public boolean action(Event e, Object what) {

    if (e.target == playButton) {
        play();
        return true;
    }
    else if (e.target == openButton) {
        open();
        return true;
    }
    else if (e.target == loopButton) {
        loop();
        return true;
    }
    else if (e.target == stopButton) {
        stop();
        return true;
    }

    return false;

}

public boolean accept(File dir, String name) {

    name = name.toLowerCase();
    if (name.endsWith(".au")) return true;
    if (name.endsWith(".wav")) return true;
    return false;
    public static void main(String[] args); throws Exception {
        AEffect a = VST.load("C:/Program Files (x86)/Audacity 1.3 Beta (Unicode)/Plug-Ins/mda MultiBand.dll");
        a.open();
        a.setSampleRate(44100.0f);
        a.setBlockSize(512);

        // attempt some processing
        int blocksize = 512;
        float[][] inputs = new float[a.numInputs][];
        for (int i = 0; i < a.numInputs; i++) {
            inputs[i] = new float[blocksize];
            for (int j = 0; j < blocksize; j++)
                inputs[i][j] = (float) Math
                .sin(j * Math.PI * 2 * 440 / 44100.0);
        }
        float[][] outputs = new float[a.numOutputs][];
        for (int i = 0; i < a.numOutputs; i++) {
            outputs[i] = new float[blocksize];
            for (int j = 0; j < blocksize; j++)
                outputs[i][j] = 0;
        }

        a.processReplacing(inputs, outputs, blocksize);

        VST.dispose(a);
    }
}
}

EDIT
The error it gives is:

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

    at SoundPlayer.main(SoundPlayer.java:35)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖竹 2024-11-09 17:45:19

我将 JVST JAR 导入到我的构建路径中,瞧!导入org.boris.jvst.*;工作过

I imported the JVST JAR into my build path and voila! The import org.boris.jvst.*; worked

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文