Libgdx,在音乐改变音高 [英] Libgdx,change pitch in Music

查看:263
本文介绍了Libgdx,在音乐改变音高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个凉爽的效果,当有爆炸的音乐得到了一会儿。

I want to implement a cool effect, when there is an explosion the music gets slightly slower for a moment.

音乐类libgdx不允许改变声音的音高,我尝试使用声音类而不是音乐来播放我的音乐,但它在加载文件的速度很慢(5秒,5 MB的文件,在桌面上!)。

The Music class of libgdx doesn't allow changing the pitch of the sound, I tried using the Sound class instead of the Music to play my music but its very slow at loading the files ( 5 sec for a 5 mb file, on Desktop! ).

所以,在问题如果有一种方法可以解决此,或者如果有外部库,该作品无论在台式机和Android可以工作与libgdx。

So, the question is if there is a way to workaround this, or if there is an external library, that works both on desktop and android and can work along with libgdx.

推荐答案

经过一番搜索,我发现了一种通过编辑libgdx源$ C ​​$ C。

After some searching I found a way by editing the libgdx source code.

您需要使用 AudioDevice 目的是通过样本来播放音乐的样品,要做到这一点,你需要包括音频扩展libgdx的。

You need to use the AudioDevice object to play your music sample by sample ,to do that you need to include the audio-extension of libgdx.

我们该怎么编辑libgdx源$ C ​​$ C,所以你需要下载它,并用正确的libgdx项目取代gdx.jar,GDX-后端的android.jar和GDX-后端lwjgl.jar(他们有未经罐子扩展相同的名称)

We gonna edit the libgdx source code so you need to download it and replace the gdx.jar , gdx-backend-android.jar and gdx-backend-lwjgl.jar with the correct libgdx projects(they have the same name without the jar extension)

1)编辑AudioDevice.java com.badlogic.gdx.audio包GDX项目里面

添加以下code接口里面

add the following code inside the interface

public void setSpeed(float val);

2)编辑AndroidAudioDevice.java com.badlogic.gdx.backends.android包GDX-后端的Andr​​oid项目内

在AudioDevice类的安卓端依靠AudioTrack类Android SDK的,这个类有一个 setPlaybackRate(..)方法。

The Android side of the AudioDevice class relies on the AudioTrack class of the Android sdk ,this class has a setPlaybackRate(..) method.

添加以下code类里面

add the following code inside the class

@Override
public void setSpeed (float speed) {
    track.setPlaybackRate((int)(track.getSampleRate()*speed));
}

3)编辑OpenALAudioDevice.java com.badlogic.gdx.backends.lwjgl.audio包GDX-后端LWJGL项目里面

在AudioDevice的桌面端依靠的OpenAL(音频的OpenGL的),它有一个方便的一组变桨法

The Desktop side of the AudioDevice relies on OpenAL (the opengl of audio) which has a handy set pitch method

添加以下里面的类

@Override
public void setSpeed (float speed) {
    alSourcef(sourceID, AL_PITCH, speed);
}

4)播放音频

下面是code加载和播放声音文件

Here is the code for loading and playing the sound file

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.AudioDevice;
import com.badlogic.gdx.audio.io.Mpg123Decoder;
import com.badlogic.gdx.files.FileHandle;

public class MusicBeat {

static short[] samples = new short[2048];
Mpg123Decoder decoder;
AudioDevice device;
static FileHandle externalFile;
public boolean playing=true;
public MusicBeat(String name )
{
        FileHandle file=Gdx.files.internal(name);
        FileHandle external=Gdx.files.external("myappname/"+name);
        if(!external.exists())file.copyTo(external); //copy the file to the external storage only if it doesnt exists yet
        decoder = new Mpg123Decoder(external);
        device = Gdx.audio.newAudioDevice(decoder.getRate(),decoder.getChannels() == 1 ? true : false);
        playing=false;
        externalFile=file;
}

void play()
{
    playing=true;
    Thread playbackThread = new Thread(new Runnable() {
        @Override
        public synchronized  void run() {
            int readSamples = 0;
            while ( playing) {
                if(decoder!=null){
                    if((readSamples = decoder.readSamples(samples, 0,samples.length))<=0){
                        decoder.dispose();
                        decoder = new Mpg123Decoder(externalFile);
                        playing=false;
                    }
                    device.writeSamples(samples, 0, readSamples);
                }
            }
        }
    });
    playbackThread.setDaemon(true);
    playbackThread.start();
}

public void stop(){
    playing=false;
    decoder.dispose();
}
public void setVolume(float vol){
    device.setVolume(vol);
}
public void setSpeed(float speed){
    device.setSpeed(speed);
}

}

从哪里获得的音频扩展?(所需AudioDevice)

Where to get the audio extension?(required for the AudioDevice)

音频扩展似乎已经去precated和罐子不能被轻易发现,我上传了他们的此处,它的一个老版本,但应该只是罚款。

The audio extension seems to have been deprecated and the jars cant be found easily, I have uploaded them here, its an old version but should work just fine.

更简单的方法?

如果你的游戏是唯一的意图在桌面上运行,音乐类libgdx桌面再次依靠OpenAL的赋予与球场上发挥威力,所以你只需要编辑音乐接口(OpenALMusic在桌面上)而不是AudioDevice,并得到由样品的事情全剧样品的方程,遗憾的是因为dawez说在Android上的音乐类依赖的MediaPlayer这不是给我们的音高变化的选择。

If your game is only intent to run on desktop ,the Music class of libgdx on desktop relies again on OpenAL which gives as the power to play with the pitch ,so you just need to edit the Music interface(OpenALMusic on desktop) instead of the AudioDevice and get out the whole play sample by sample thing out of the equation,unfortunately as dawez said the Music class on android relies on MediaPlayer which is not giving us the pitch change option.

结论:这个方法似乎没有对我很好,如果你的游戏真正需要的间距的事情,它不会是有道理的,没有它,然后去用它,否则它的这种实在太多精力一个小细节。

Conclusion :This method doesnt seem nice to me ,If your game really needs the pitch thing and it doesn't makes sense without it then go with it ,otherwise its just too much effort for such a small detail .

这篇关于Libgdx,在音乐改变音高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆