Skip to content

Commit

Permalink
Various bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
SpencerPark committed Apr 6, 2018
1 parent e1705f0 commit 847777e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 32 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ gradle-app.setting
gen/

# Intellij files
.idea/
.idea/
out/

docs/
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
//The manifest attribute makes the jar executable.
jar {
manifest {
attributes 'Main-Class': 'org.mellowd.compiler.Compiler'
attributes 'Main-Class': 'org.mellowd.io.Compiler'
}
//Include all shaded dependencies in the jar
from configurations.shade
Expand Down Expand Up @@ -103,8 +103,8 @@ test {
}

generateGrammarSource {
outputDirectory = file("$outputDirectory/org/mellowd/parser")
arguments += ['-package', 'org.mellowd.parser']
outputDirectory = file("$outputDirectory/org/mellowd/compiler")
arguments += ['-package', 'org.mellowd.compiler']
arguments += ['-visitor']
arguments += ['-no-listener']
}
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/org/mellowd/compiler/MellowD.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,18 @@ public synchronized Sequence execute() throws Exception {
executor.start();
}

while (!executors.isEmpty()) {
CodeExecutor executor = executors.poll();
executor.join(3000L); //Wait for the thread to finish
if (executor.errorWhileExecuting()) {
throw executor.getExecutionError();
try {
while (!executors.isEmpty()) {
CodeExecutor executor = executors.poll();
executor.join(3000L); //Wait for the thread to finish
if (executor.errorWhileExecuting()) {
throw executor.getExecutionError();
}
}
} finally {
this.blocks.values().forEach(MellowDBlock::clearCode);
}

this.blocks.values().forEach(MellowDBlock::clearCode);

return master;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ static int calcIndexOverflow(int index, int size) {
static void forEachInRange(int lower, int upper, IntConsumer apply) {
if (upper == lower) {
apply.accept(lower);
} else if (upper > lower) {
} else if (lower < upper) {
for (int i = lower; i <= upper; i++)
apply.accept(i);
} else {
for (int i = upper; i >= lower; i--)
for (int i = lower; i >= upper; i--)
apply.accept(i);
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/org/mellowd/io/WavIODelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.function.Function;

public class WavIODelegate implements SequenceIODelegate {
private static final float SAMPLE_RATE = 96000; //Hz
private static final float SAMPLE_RATE = 44100; //Hz
private static final int SAMPLE_SIZE = 24; //bits per sample
private static final int CHANNELS_MONO = 1;
private static final int CHANNELS_STEREO = 2;
Expand All @@ -26,15 +26,31 @@ public class WavIODelegate implements SequenceIODelegate {

private final Function<Synthesizer, Synthesizer> soundfontLoader;

private int channels = CHANNELS_STEREO;
private float sampleRate = SAMPLE_RATE;
private int sampleSize = SAMPLE_SIZE;

public WavIODelegate(Function<Synthesizer, Synthesizer> soundfontLoader) {
this.soundfontLoader = soundfontLoader;
}

public void setChannels(int channels) {
this.channels = channels;
}

public void setSampleRate(float sampleRate) {
this.sampleRate = sampleRate;
}

public void setSampleSize(int sampleSize) {
this.sampleSize = sampleSize;
}

@Override
public void save(Sequence sequence, OutputStream out) throws IOException {
VirtualMIDIPlayer player = new VirtualMIDIPlayer(sequence);

AudioFormat format = new AudioFormat(SAMPLE_RATE, SAMPLE_SIZE, CHANNELS_STEREO, true, false);
AudioFormat format = new AudioFormat(this.sampleRate, this.sampleSize, this.channels, true, false);
AudioSynthesizer synth;
AudioInputStream stream;
try {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/mellowd/io/repl/MellowDKernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,12 @@ public Sequence eval(String code) throws ParseException, CompilationException {
if (errorListener.encounteredError())
throw new ParseException(errorListener.getErrors());

MellowDCompiler walker = new MellowDCompiler(mellowD);

if (!parseTree.importStatement().isEmpty()) {
//Compile the dependencies
parseTree.importStatement().forEach(walker::visitImportStatement);
parseTree.importStatement().forEach(this.compiler::visitImportStatement);
}

walker.visitSong(parseTree);
this.compiler.visitSong(parseTree);

try {
return mellowD.execute();
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/org/mellowd/midi/MIDIChannel.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public String toString() {
public static final int DEFAULT_OFF_VELOCITY = 96;

private Track midiTrack;
private final boolean isPercussion;
private final boolean percussion;
private final int channelNum;
private final TimingEnvironment timingEnvironment;
private SortedMap<Long, ScheduledAction> scheduledActions;
Expand All @@ -98,9 +98,9 @@ public String toString() {

private boolean slurred = false;

public MIDIChannel(Track midiTrack, boolean isPercussion, int channelNum, TimingEnvironment timingEnvironment) {
public MIDIChannel(Track midiTrack, boolean percussion, int channelNum, TimingEnvironment timingEnvironment) {
this.midiTrack = midiTrack;
this.isPercussion = isPercussion;
this.percussion = percussion;
this.channelNum = channelNum;
this.timingEnvironment = timingEnvironment;
this.scheduledActions = new TreeMap<>(Long::compare);
Expand Down Expand Up @@ -137,22 +137,26 @@ public Track replaceTrack(Track newTrack) {
}
});

// Forcefully set the instrument. Set to -1 to disable the optimization that skips
// setting the instrument when it is the same as the channel's state
int soundBank = this.soundBank;
this.soundBank = -1;
int instrument = this.instrument;
this.instrument = -1;
this.setSoundBank(soundBank);
this.setInstrument(instrument);
// If not a percussion block make sure to reapply the last instrument change
if (!this.percussion) {
// Forcefully set the instrument. Set to -1 to disable the optimization that skips
// setting the instrument when it is the same as the channel's state
int soundBank = this.soundBank;
this.soundBank = -1;
int instrument = this.instrument;
this.instrument = -1;
this.setSoundBank(soundBank);
this.setInstrument(instrument);
}

if (this.pitchBend != GeneralMidiConstants.NO_PITCH_BEND) {
int pitchBend = this.pitchBend;
this.pitchBend = -1;
this.setPitchBend(pitchBend);
}

this.controllers.values().forEach(MIDIController::reapply);
//TODO this results in some weird effects, try to only reapply the ones that actually need it
//this.controllers.values().forEach(MIDIController::reapply);

if (this.muted) {
this.muted = false;
Expand All @@ -163,7 +167,7 @@ public Track replaceTrack(Track newTrack) {
}

public boolean isPercussion() {
return isPercussion;
return percussion;
}

protected int getChannelNum() {
Expand Down

0 comments on commit 847777e

Please sign in to comment.