-
-
Notifications
You must be signed in to change notification settings - Fork 31
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
1 parent
d6c6115
commit 542dc22
Showing
5 changed files
with
58 additions
and
0 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
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,16 @@ | ||
import { withHook } from '@newcar/core' | ||
import type { AudioPlayer } from '../../widgets/audio-player' | ||
|
||
export function play(audio: ArrayBuffer) { | ||
let source: AudioBufferSourceNode | ||
|
||
return withHook<AudioPlayer, { | ||
speed?: number | ||
}>({ | ||
before({ widget }) { | ||
source = widget.context.createBufferSource() | ||
source.buffer = audio | ||
}, | ||
animate() {}, | ||
}) | ||
} |
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,14 @@ | ||
import type { WidgetOptions, WidgetStyle } from '@newcar/core' | ||
import { Widget } from '@newcar/core' | ||
|
||
export interface AudioPlayerOptions extends WidgetOptions { | ||
style?: WidgetStyle | ||
} | ||
|
||
export class AudioPlayer extends Widget { | ||
context: AudioContext = new AudioContext() | ||
|
||
constructor(options?: AudioPlayerOptions) { | ||
super(options) | ||
} | ||
} |
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,26 @@ | ||
import { $source } from '../global' | ||
|
||
/** | ||
* Preloading a font file. | ||
* @param src The font file's path. | ||
* @returns The Font with `ArrayBuffer` type. | ||
*/ | ||
export async function useAudio(src: string) { | ||
if (typeof window !== 'undefined') { | ||
const response = await fetch(src) | ||
const array = await response.arrayBuffer() | ||
$source.fonts.push(array) | ||
return array | ||
} | ||
else { | ||
const fs = await import('node:fs') | ||
const path = await import('node:path') | ||
const buffer = fs.readFileSync(path.resolve(src)) | ||
const array = buffer.buffer.slice( | ||
buffer.byteOffset, | ||
buffer.byteOffset + buffer.byteLength, | ||
) | ||
$source.fonts.push(array) | ||
return array | ||
} | ||
} |
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