Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tonygoldcrest committed Feb 13, 2024
1 parent 80646d5 commit 8ac16dd
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 13 deletions.
7 changes: 6 additions & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ipcMain,
protocol,
screen,
net,
powerSaveBlocker,
} from 'electron';
import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
Expand All @@ -39,6 +39,8 @@ let mainWindow: BrowserWindow | null = null;

const store = new Store();

let powerSaveBlockerId: number = -1;

ipcMain.on('load-song', async (event, id) => {
const songData = (store.get('songs') as StorageSchema['songs'])[id];

Expand Down Expand Up @@ -126,6 +128,8 @@ const createWindow = async () => {
return display.bounds.x !== 0 || display.bounds.y !== 0;
});

powerSaveBlockerId = powerSaveBlocker.start('prevent-display-sleep');

mainWindow = new BrowserWindow({
show: false,
x: externalDisplay ? externalDisplay.bounds.x + 50 : 0,
Expand Down Expand Up @@ -176,6 +180,7 @@ app.on('window-all-closed', () => {
// after all windows have been closed
if (process.platform !== 'darwin') {
app.quit();
powerSaveBlocker.stop(powerSaveBlockerId);
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/main/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function parseAndSaveSongs(

store.delete('songs');

glob(`${result.filePaths[0]}/**/*.mid`, {}, (err, files) => {
glob(`${result.filePaths[0]}/**/notes.mid`, {}, (err, files) => {
const songList = files
.map((file) => path.join(path.dirname(file), 'song.ini'))
.filter((file) => fs.existsSync(file))
Expand Down
10 changes: 3 additions & 7 deletions src/midi-parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export class MidiParser {
const currentModifierNotes = this.modifierNotes.filter(
(modifier) =>
currentTick >= modifier.note.ticks &&
currentTick <= modifier.note.ticks + modifier.note.durationTicks,
currentTick < modifier.note.ticks + modifier.note.durationTicks,
);

if (tickNotes) {
Expand Down Expand Up @@ -379,10 +379,6 @@ export class MidiParser {
return note;
}

if (!note.isRest) {
return this.getClosestDuration(availableDurations, note);
}

const atomicDurations = this.getSubsets(
availableDurations,
note.durationTicks ?? 0,
Expand All @@ -399,15 +395,15 @@ export class MidiParser {
const { duration, dotted, isTriplet } =
this.durationMap[durationTicks];

const isRest = note.isRest || index === 0;
const isRest = note.isRest || index !== 0;
const newNote: Note = {
isTriplet: isTriplet ?? false,
dotted: dotted ?? false,
durationTicks,
isRest,
tick: 0,
duration: `${duration}${isRest ? 'r' : ''}`,
notes: note.isRest ? ['b/4'] : note.notes,
notes: isRest ? ['b/4'] : note.notes,
};

return newNote;
Expand Down
10 changes: 10 additions & 0 deletions src/renderer/services/audio-player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class AudioPlayer {

const audioTrack = new AudioTrack(audioBuffers, name, this.context);

audioTrack.endedListener = this.trackEndedListener;

this.audioTracks.push(audioTrack);

return audioTrack;
Expand Down Expand Up @@ -89,4 +91,12 @@ export class AudioPlayer {

this.context.close();
}

trackEndedListener = () => {
if (this.audioTracks.filter((track) => !track.ended).length !== 0) {
return;
}

this.stop();
};
}
38 changes: 34 additions & 4 deletions src/renderer/services/audio-player/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export class AudioTrack {

duration: number;

ended: boolean = false;

endedListener: (() => void) | null = null;

constructor(
public buffers: AudioBuffer[],
public name: string,
Expand Down Expand Up @@ -34,32 +38,58 @@ export class AudioTrack {
}

start(at: number, offset: number) {
if (this.sources.length > 0) {
this.stop();
}

this.ended = false;

this.sources = this.buffers.map((buffer, index) => {
const source = this.context.createBufferSource();
source.buffer = buffer;
source.start(at, offset);

source.connect(this.gainNodes[index]);

source.addEventListener('ended', this.endedEventListener);

return source;
});
}

stop() {
this.sources.forEach((source) => {
source.stop(0);
source.disconnect();
});
this.sources.forEach((source) => this.stopSource(source));

this.sources = [];
}

endedEventListener = (event: Event) => {
const source = event.currentTarget as AudioBufferSourceNode;

this.stopSource(source);
this.sources.splice(this.sources.indexOf(source), 1);

if (this.sources.length === 0) {
this.ended = true;

this.endedListener?.();
}
};

stopSource(source: AudioBufferSourceNode) {
source.stop();
source.removeEventListener('ended', this.endedEventListener);
source.disconnect();
}

destroy() {
this.stop();

this.gainNodes.forEach((node) => {
node.disconnect();
});
this.gainNodes = [];

this.endedListener = null;
}
}
4 changes: 4 additions & 0 deletions src/renderer/views/SelectSongView/SelectSongView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Header,
ScanSongsButton,
SongListContainer,
SongNumber,
SongViewOverlay,
Wrapper,
} from './styles';
Expand Down Expand Up @@ -54,6 +55,9 @@ export function SelectSongView() {
setNameFilter(value);
}}
/>
<SongNumber>
{filteredSongList.length} / {songList.length}
</SongNumber>
</Header>
<SongListContainer>
<SongList
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/views/SelectSongView/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export const Header = styled.div`
padding: 20px;
z-index: 10;
box-shadow: ${theme.boxShadow.soft};
display: flex;
flex-flow: column;
`;

export const SongNumber = styled.div`
margin-left: auto;
color: ${theme.color.foreground};
font-size: 12px;
`;

export const ScanSongsButton = styled(FloatButton)`
Expand Down

0 comments on commit 8ac16dd

Please sign in to comment.