Spaces:
Sleeping
Sleeping
Mina Emadi commited on
Commit ·
46430b2
1
Parent(s): 3ef0232
increasing the speed of uploading to the server
Browse files
backend/routers/stems.py
CHANGED
|
@@ -91,7 +91,7 @@ async def get_stem(
|
|
| 91 |
detail=f"Stem '{stem_name}' not found"
|
| 92 |
)
|
| 93 |
|
| 94 |
-
# Serve raw
|
| 95 |
if format == "pcm":
|
| 96 |
audio = stem.audio
|
| 97 |
if audio.ndim == 2:
|
|
@@ -99,8 +99,9 @@ async def get_stem(
|
|
| 99 |
else:
|
| 100 |
audio = np.ascontiguousarray(audio, dtype=np.float32)
|
| 101 |
num_frames = len(audio)
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
return Response(
|
| 105 |
content=pcm_bytes,
|
| 106 |
media_type="application/octet-stream",
|
|
@@ -110,7 +111,8 @@ async def get_stem(
|
|
| 110 |
"X-Sample-Rate": str(stem.sample_rate),
|
| 111 |
"X-Channels": "1",
|
| 112 |
"X-Frames": str(num_frames),
|
| 113 |
-
"
|
|
|
|
| 114 |
}
|
| 115 |
)
|
| 116 |
|
|
|
|
| 91 |
detail=f"Stem '{stem_name}' not found"
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# Serve raw int16 PCM — half the wire size of float32, no encode/decode libraries
|
| 95 |
if format == "pcm":
|
| 96 |
audio = stem.audio
|
| 97 |
if audio.ndim == 2:
|
|
|
|
| 99 |
else:
|
| 100 |
audio = np.ascontiguousarray(audio, dtype=np.float32)
|
| 101 |
num_frames = len(audio)
|
| 102 |
+
audio_int16 = (audio * 32767).astype(np.int16)
|
| 103 |
+
pcm_bytes = audio_int16.tobytes()
|
| 104 |
+
print(f"[{stem_name}] Serving {len(pcm_bytes)/1024/1024:.1f}MB int16 PCM ({num_frames} frames @ {stem.sample_rate}Hz)")
|
| 105 |
return Response(
|
| 106 |
content=pcm_bytes,
|
| 107 |
media_type="application/octet-stream",
|
|
|
|
| 111 |
"X-Sample-Rate": str(stem.sample_rate),
|
| 112 |
"X-Channels": "1",
|
| 113 |
"X-Frames": str(num_frames),
|
| 114 |
+
"X-Bit-Depth": "16",
|
| 115 |
+
"Access-Control-Expose-Headers": "X-Sample-Rate, X-Channels, X-Frames, X-Bit-Depth",
|
| 116 |
}
|
| 117 |
)
|
| 118 |
|
frontend/src/hooks/useAudioEngine.js
CHANGED
|
@@ -148,7 +148,11 @@ export function useAudioEngine() {
|
|
| 148 |
console.log(`[${stem}] ArrayBuffer: ${sizeMB}MB in ${(bufferEnd - bufferStart).toFixed(0)}ms`)
|
| 149 |
|
| 150 |
const constructStart = performance.now()
|
| 151 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
const audioBuffer = ctx.createBuffer(numChannels, numFrames, sampleRate)
|
| 153 |
audioBuffer.copyToChannel(float32, 0)
|
| 154 |
const constructEnd = performance.now()
|
|
|
|
| 148 |
console.log(`[${stem}] ArrayBuffer: ${sizeMB}MB in ${(bufferEnd - bufferStart).toFixed(0)}ms`)
|
| 149 |
|
| 150 |
const constructStart = performance.now()
|
| 151 |
+
const int16 = new Int16Array(arrayBuffer)
|
| 152 |
+
const float32 = new Float32Array(int16.length)
|
| 153 |
+
for (let i = 0; i < int16.length; i++) {
|
| 154 |
+
float32[i] = int16[i] / 32767
|
| 155 |
+
}
|
| 156 |
const audioBuffer = ctx.createBuffer(numChannels, numFrames, sampleRate)
|
| 157 |
audioBuffer.copyToChannel(float32, 0)
|
| 158 |
const constructEnd = performance.now()
|