-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
core/src/main/java/me/sunstorm/showmanager/util/WaveformRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package me.sunstorm.showmanager.util; | ||
|
||
import com.google.common.hash.Hashing; | ||
import me.sunstorm.showmanager.Constants; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class WaveformRunner { | ||
private static final File CACHE = new File(Constants.BASE_DIRECTORY, "wavecache"); | ||
private final Path executablePath; | ||
|
||
public WaveformRunner(@NotNull Path executablePath) { | ||
this.executablePath = executablePath; | ||
} | ||
|
||
public byte[] sample(@NotNull Path wav) throws IOException { | ||
var cached = tryCaching(wav); | ||
if (cached != null) return cached; | ||
|
||
var process = new ProcessBuilder() | ||
.command(executablePath.toString(), "-i", wav.toString(), "--output-format", "dat", "-q", "-b", "8") | ||
.redirectOutput(ProcessBuilder.Redirect.PIPE) | ||
.start(); | ||
var buf = new ByteArrayOutputStream(); | ||
process.getInputStream().transferTo(buf); | ||
try { | ||
var result = process.waitFor(); | ||
if (result == 0) { | ||
var data = buf.toByteArray(); | ||
saveCache(data, wav.toString()); | ||
return data; | ||
} else { | ||
throw new IOException("waveform process exited with non zero code"); | ||
} | ||
} catch (InterruptedException e) { | ||
Exceptions.sneaky(e); | ||
return null; | ||
} | ||
} | ||
|
||
@Nullable | ||
private byte[] tryCaching(@NotNull Path wav) throws IOException { | ||
var name = Hashing.sha256().hashString(wav.toString(), StandardCharsets.UTF_8).toString(); | ||
var cached = CACHE.toPath().resolve(name); | ||
if (Files.exists(cached)) { | ||
return Files.readAllBytes(cached); | ||
} | ||
return null; | ||
} | ||
|
||
private void saveCache(byte[] data, String file) throws IOException { | ||
if (!CACHE.isDirectory()) | ||
CACHE.mkdirs(); | ||
var name = Hashing.sha256().hashString(file, StandardCharsets.UTF_8).toString(); | ||
Files.write(CACHE.toPath().resolve(name), data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<script lang="ts"> | ||
import Peaks, { type PeaksOptions } from 'peaks.js' | ||
import { loadedAudio } from '$lib/data/audio' | ||
import LoaderCircle from 'lucide-svelte/icons/loader-circle' | ||
import { player } from '@/audio/waveform' | ||
let zoomview: HTMLElement | null = null | ||
let overview: HTMLElement | null = null | ||
const loadWaveData = async (): Promise<void> => { | ||
const res = await fetch('http://localhost:7000/audio/samples') | ||
if (res.status != 200) throw new Error('Request failed') | ||
const data = await res.arrayBuffer() | ||
const peaksOptions: PeaksOptions = { | ||
zoomview: { | ||
container: zoomview | ||
}, | ||
overview: { | ||
container: overview | ||
}, | ||
player: player, | ||
waveformData: { | ||
arraybuffer: data | ||
} | ||
} | ||
Peaks.init(peaksOptions, (e) => console.log('Peaks init failed:', e)) | ||
} | ||
</script> | ||
|
||
{#if $loadedAudio != ''} | ||
<div class="space-y-2"> | ||
<div bind:this={zoomview} class="h-[100px] w-[1000px] border shadow-sm"></div> | ||
<div bind:this={overview} class="h-[75px] w-[1000px] border shadow-sm"></div> | ||
</div> | ||
{#await loadWaveData()} | ||
<div class="space-y-3"> | ||
<LoaderCircle class="h-10 w-10 animate-spin" /> | ||
<p>Loading...</p> | ||
</div> | ||
{:catch} | ||
<p>Failed to load waveform render.</p> | ||
{/await} | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { EventEmitterForPlayerEvents, PlayerAdapter } from 'peaks.js' | ||
|
||
let emitter: EventEmitterForPlayerEvents | null = null | ||
|
||
export const player: PlayerAdapter = { | ||
init: async (eventEmitter: EventEmitterForPlayerEvents) => { | ||
emitter = eventEmitter | ||
}, | ||
destroy: () => {}, | ||
play: async () => {}, | ||
pause: () => {}, | ||
seek: (time: number) => {}, | ||
isPlaying: () => { | ||
return false | ||
}, | ||
isSeeking: () => { | ||
return false | ||
}, | ||
getCurrentTime: () => { | ||
return 0 | ||
}, | ||
getDuration: () => { | ||
return 0 | ||
} | ||
} |