mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-24 23:51:20 -08:00
refactor(asr): [PKG-ASR-03] own typed audio capture and playback lifetimes
This commit is contained in:
1 parent
53c295819d
commit
0cf2af4840
33 files changed
+3492
-809
No files matched your search
@@ -0,0 +1,59 @@
|
||||
interface SampleBlock {
|
||||
samples: Float32Array
|
||||
next?: SampleBlock
|
||||
}
|
||||
|
||||
export class SampleQueue {
|
||||
private head?: SampleBlock
|
||||
private tail?: SampleBlock
|
||||
private offset = 0
|
||||
private size = 0
|
||||
|
||||
get length(): number {
|
||||
return this.size
|
||||
}
|
||||
|
||||
push(input: Float32Array): void {
|
||||
if (input.length === 0)
|
||||
return
|
||||
const block: SampleBlock = { samples: new Float32Array(input) }
|
||||
if (this.tail)
|
||||
this.tail.next = block
|
||||
else
|
||||
this.head = block
|
||||
this.tail = block
|
||||
this.size += input.length
|
||||
}
|
||||
|
||||
take(count: number): Float32Array | null {
|
||||
if (!Number.isSafeInteger(count) || count <= 0)
|
||||
throw new RangeError('Sample count must be a positive safe integer')
|
||||
if (count > this.size)
|
||||
return null
|
||||
|
||||
const output = new Float32Array(count)
|
||||
let written = 0
|
||||
while (this.head && written < count) {
|
||||
const samples = this.head.samples
|
||||
const amount = Math.min(samples.length - this.offset, count - written)
|
||||
output.set(samples.subarray(this.offset, this.offset + amount), written)
|
||||
written += amount
|
||||
this.offset += amount
|
||||
if (this.offset === samples.length) {
|
||||
this.head = this.head.next
|
||||
this.offset = 0
|
||||
}
|
||||
}
|
||||
if (!this.head)
|
||||
this.tail = undefined
|
||||
this.size -= count
|
||||
return output
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.head = undefined
|
||||
this.tail = undefined
|
||||
this.offset = 0
|
||||
this.size = 0
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user