Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
package alphaTab.collections

internal class ArrayListWithRemoveRange<T> : ArrayList<T> {

constructor() : super()
constructor(c: Collection<T>) : super(c)

public override fun removeRange(startIndex:Int, endIndex:Int) {
super.removeRange(startIndex, endIndex);
}
}

public class List<T> : Iterable<T> {
internal val _data: MutableList<T>
internal val _data: ArrayListWithRemoveRange<T>

val length: Double
get() = _data.size.toDouble()

public constructor() {
_data = ArrayList()
_data = ArrayListWithRemoveRange()
}

public constructor(vararg items: T) {
_data = items.toMutableList()
_data = items.toCollection(ArrayListWithRemoveRange())
}

@Suppress("UNCHECKED_CAST")
public constructor(size: Int) {
_data = ArrayList()
_data = ArrayListWithRemoveRange()
var remaining = size
while (remaining-- > 0) {
_data.add(null as T)
}
}

public constructor(items: Iterable<T>) {
_data = items.toMutableList()
_data = items.toCollection(ArrayListWithRemoveRange())
}

internal constructor(items: MutableList<T>) {
internal constructor(items: ArrayListWithRemoveRange<T>) {
_data = items
}

Expand Down Expand Up @@ -72,7 +82,7 @@ public class List<T> : Iterable<T> {
}

public fun pop(): T {
return _data.removeLast()
return _data.removeAt(_data.lastIndex)
}

public fun unshift(item: T) {
Expand Down Expand Up @@ -111,11 +121,11 @@ public class List<T> : Iterable<T> {
}

public fun slice(): List<T> {
return List(ArrayList(_data))
return List(ArrayListWithRemoveRange(_data))
}

public fun slice(start: Double): List<T> {
return List(_data.subList(start.toInt(), _data.size))
return List(ArrayListWithRemoveRange(_data.subList(start.toInt(), _data.size)))
}

public fun shift(): T {
Expand All @@ -128,7 +138,7 @@ public class List<T> : Iterable<T> {
actualStart += _data.size
}

_data.subList(start.toInt(), (start + deleteCount).toInt()).clear()
_data.removeRange(start.toInt(), (start + deleteCount).toInt())
_data.addAll(start.toInt(), newElements.toList())
}

Expand Down
13 changes: 7 additions & 6 deletions src/synth/AlphaSynth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,14 +557,15 @@ export class AlphaSynthBase implements IAlphaSynth {
if (isSeek) {
this._playedEventsQueue.clear();
} else {
const playedEvents = new Queue<MidiEvent>();
while (!this._playedEventsQueue.isEmpty && this._playedEventsQueue.peek().time < currentTime) {
const synthEvent = this._playedEventsQueue.dequeue();
playedEvents.enqueue(synthEvent.event);
const playedEvents: MidiEvent[] = [];
while (!this._playedEventsQueue.isEmpty && this._playedEventsQueue.peek()!.time < currentTime) {
const synthEvent = this._playedEventsQueue.dequeue()!;
playedEvents.push(synthEvent.event);
}
if (!playedEvents.isEmpty) {
if (playedEvents.length > 0) {
playedEvents.reverse();
(this.midiEventsPlayed as EventEmitterOfT<MidiEventsPlayedEventArgs>).trigger(
new MidiEventsPlayedEventArgs(playedEvents.toArray())
new MidiEventsPlayedEventArgs(playedEvents)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/synth/BackingTrackPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class BackingTrackAudioSynthesizer implements IAudioSampleSynthesizer {
public fakeSynthesize(): SynthEvent[] {
const processedEvents: SynthEvent[] = [];
while (!this._midiEventQueue.isEmpty) {
const m: SynthEvent = this._midiEventQueue.dequeue();
const m: SynthEvent = this._midiEventQueue.dequeue()!;
if (m.isMetronome && this.metronomeVolume > 0) {
// ignore metronome
} else if (m.event) {
Expand Down
66 changes: 43 additions & 23 deletions src/synth/ds/Queue.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
class QueueItem<T> {
public value: T;
public next?: QueueItem<T>;

public constructor(value: T) {
this.value = value;
}
}

export class Queue<T> {
private _items: T[] = [];
private _position: number = 0;
private _head?: QueueItem<T>;
private _tail?: QueueItem<T>;

public isEmpty: boolean = true;
public get isEmpty() {
return this._head === undefined;
}

public clear() {
this._items = [];
this._position = 0;
this.isEmpty = true;
this._head = undefined;
this._tail = undefined;
}

public enqueue(item: T) {
this.isEmpty = false;
this._items.push(item);
const queueItem = new QueueItem<T>(item);
if (this._tail) {
// not empty -> add after tail
this._tail!.next = queueItem;
this._tail = queueItem;
} else {
// empty -> new item takes head and tail
this._head = queueItem;
this._tail = queueItem;
}
}

public peek(): T {
return this._items[this._position];
public peek(): T | undefined {
const head = this._head;
if (!head) {
return undefined;
}
return head.value;
}

public dequeue(): T {
const item = this._items[this._position];
this._position++;
if (this._position >= this._items.length / 2) {
this._items = this._items.slice(this._position);
this._position = 0;
public dequeue(): T | undefined {
const head = this._head;
if (!head) {
return undefined;
}
this.isEmpty = this._items.length === 0;
return item;
}

public toArray(): T[] {
const items = this._items.slice(this._position);
items.reverse();
return items;
const newHead:QueueItem<T>|undefined = head.next;
this._head = newHead;
// last item removed?
if (!newHead) {
this._tail = undefined;
}
return head.value;
}
}
2 changes: 1 addition & 1 deletion src/synth/synthesis/TinySoundFont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class TinySoundFont implements IAudioSampleSynthesizer {
// process in micro-buffers
// process events for first microbuffer
while (!this._midiEventQueue.isEmpty) {
const m: SynthEvent = this._midiEventQueue.dequeue();
const m: SynthEvent = this._midiEventQueue.dequeue()!;
if (m.isMetronome && this.metronomeVolume > 0) {
this.channelNoteOff(SynthConstants.MetronomeChannel, SynthConstants.MetronomeKey);
this.channelNoteOn(SynthConstants.MetronomeChannel, SynthConstants.MetronomeKey, 95 / 127);
Expand Down