Skip to content

Commit

Permalink
AudioDriver : refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
exch-bms2 committed Mar 17, 2018
1 parent f5bb6da commit 6152668
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
24 changes: 11 additions & 13 deletions src/bms/player/beatoraja/audio/AbstractAudioDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

import java.nio.file.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.logging.Logger;

import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.*;

/**
* 抽象オーディオドライバー
Expand All @@ -23,7 +22,7 @@ public abstract class AbstractAudioDriver<T> implements AudioDriver {
/**
* 効果音マップ
*/
private Map<String, AudioElement<T>> soundmap = new HashMap<String, AudioElement<T>>();
private ObjectMap<String, AudioElement<T>> soundmap = new ObjectMap<String, AudioElement<T>>();
/**
* キー音マップ(音切りなし)
*/
Expand Down Expand Up @@ -196,9 +195,9 @@ public synchronized void setModel(BMSModel model) {
volume = model.getVolwav() / 100f;
}

List<SliceWav<T>>[] slicesound = new List[wavcount];
Array<SliceWav<T>>[] slicesound = new Array[wavcount];

Map<Integer, List<Note>> notemap = new HashMap<Integer, List<Note>>();
IntMap<List<Note>> notemap = new IntMap<List<Note>>();
final int lanes = model.getMode().key;
for (TimeLine tl : model.getAllTimeLines()) {
for (int i = 0; i < lanes; i++) {
Expand All @@ -218,16 +217,16 @@ public synchronized void setModel(BMSModel model) {
}
}

for (Entry<Integer, List<Note>> waventry : notemap.entrySet()) {
final int wavid = waventry.getKey();
for (IntMap.Entry<List<Note>> waventry : notemap) {
final int wavid = waventry.key;
if (progress >= 1) {
break;
}
if (wavid < 0) {
continue;
}
String name = model.getWavList()[wavid];
for (Note note : waventry.getValue()) {
for (Note note : waventry.value) {
if (note.getMicroStarttime() == 0 && note.getMicroDuration() == 0) {
// 音切りなしのケース
Path p = dpath.resolve(name);
Expand All @@ -239,7 +238,7 @@ public synchronized void setModel(BMSModel model) {
// 音切りありのケース
boolean b = true;
if (slicesound[note.getWav()] == null) {
slicesound[note.getWav()] = new ArrayList<SliceWav<T>>();
slicesound[note.getWav()] = new Array<SliceWav<T>>();
}
for (SliceWav<T> slice : slicesound[note.getWav()]) {
if (slice.starttime == note.getMicroStarttime() && slice.duration == note.getMicroDuration()) {
Expand All @@ -258,13 +257,13 @@ public synchronized void setModel(BMSModel model) {
}
}
}
progress += 1f / notemap.keySet().size();
progress += 1f / notemap.size;
}

Logger.getGlobal().info("音源ファイル読み込み完了。音源数:" + wavmap.length);
for (int i = 0; i < wavmap.length; i++) {
if (slicesound[i] != null) {
this.slicesound[i] = slicesound[i].toArray(new SliceWav[slicesound[i].size()]);
this.slicesound[i] = slicesound[i].toArray(SliceWav.class);
} else {
this.slicesound[i] = new SliceWav[0];
}
Expand All @@ -277,7 +276,7 @@ public synchronized void setModel(BMSModel model) {
progress = 1;
}

private void addNoteList(Map<Integer, List<Note>> notemap, Note n) {
private void addNoteList(IntMap<List<Note>> notemap, Note n) {
if (n.getWav() < 0) {
return;
}
Expand Down Expand Up @@ -454,7 +453,6 @@ protected T load(AudioKey key) {
} else {
if (wav == null) {
wav = PCM.load(key.path);
// TODO このタイミングでsampleRate変換を行い、slice時での再変換を抑止する
}

if (wav != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/bms/player/beatoraja/audio/GdxSoundDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void run() {
}
}

class SoundInstance {
private static class SoundInstance {
public Sound sound;
public long id = -1;
public int channel = -1;
Expand Down Expand Up @@ -338,7 +338,7 @@ public int read() {
result = ((s & 0xff00) >>> 8);
}
} else if(pcm instanceof ShortDirectPCM) {
result = ((ByteBuffer)pcm.sample).get(pos - 44 + pcm.start) & 0xff;
result = ((ByteBuffer)pcm.sample).get(pos - 44 + pcm.start * 2) & 0xff;
} else if(pcm instanceof FloatPCM) {
s = (short) (((float[])pcm.sample)[(pos - 44) / 2 + pcm.start] * Short.MAX_VALUE);
if (pos % 2 == 0) {
Expand Down
19 changes: 10 additions & 9 deletions src/bms/player/beatoraja/audio/PortAudioDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public class PortAudioDriver extends AbstractAudioDriver<PCM> implements Runnabl
private static DeviceInfo[] devices;

private BlockingStream stream;

private int sampleRate;

private int channels;
/**
* ミキサー入力
*/
Expand Down Expand Up @@ -57,12 +58,13 @@ public PortAudioDriver(Config config) {
}
DeviceInfo deviceInfo = devices[ deviceId ];
sampleRate = (int)deviceInfo.defaultSampleRate;
channels = 2;
// System.out.println( " deviceId = " + deviceId );
// System.out.println( " sampleRate = " + sampleRate );
// System.out.println( " device name = " + deviceInfo.name );

StreamParameters streamParameters = new StreamParameters();
streamParameters.channelCount = 2;
streamParameters.channelCount = channels;
streamParameters.device = deviceId;
int framesPerBuffer = config.getAudioDeviceBufferSize();
streamParameters.suggestedLatency = ((double)framesPerBuffer) / sampleRate;
Expand All @@ -71,13 +73,12 @@ public PortAudioDriver(Config config) {
int flags = 0;

// Open a stream for output.
stream = PortAudio.openStream( null, streamParameters,
(int) sampleRate, framesPerBuffer, flags );
stream = PortAudio.openStream( null, streamParameters, sampleRate, framesPerBuffer, flags );

stream.start();

mixer = new Thread(this);
buffer = new float[framesPerBuffer * 2];
buffer = new float[framesPerBuffer * channels];
inputs = new MixerInput[config.getAudioDeviceSimultaneousSources()];
for (int i = 0; i < inputs.length; i++) {
inputs[i] = new MixerInput();
Expand All @@ -92,8 +93,8 @@ protected PCM getKeySound(Path p) {
if (wav != null && wav.sampleRate != sampleRate) {
wav = wav.changeSampleRate(sampleRate);
}
if (wav != null && wav.channels != 2) {
wav = wav.changeChannels(2);
if (wav != null && wav.channels != channels) {
wav = wav.changeChannels(channels);
}

return wav;
Expand All @@ -104,8 +105,8 @@ protected PCM getKeySound(PCM pcm) {
if (pcm.sampleRate != sampleRate) {
pcm = pcm.changeSampleRate(sampleRate);
}
if (pcm.channels != 2) {
pcm = pcm.changeChannels(2);
if (pcm.channels != channels) {
pcm = pcm.changeChannels(channels);
}
return pcm;
}
Expand Down

0 comments on commit 6152668

Please sign in to comment.