File size: 9,130 Bytes
a0fcd39 25d51c9 a0fcd39 b384007 a0fcd39 b384007 a0fcd39 25d51c9 a0fcd39 b384007 a0fcd39 b384007 a0fcd39 b384007 a0fcd39 08bdfee 25d51c9 08bdfee 25d51c9 3ef0232 59dcfdf a0fcd39 25d51c9 a0fcd39 3ef0232 59dcfdf 25d51c9 a0fcd39 08bdfee a0fcd39 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | import { useState, useCallback } from 'react'
export function useSession() {
const [sessionId, setSessionId] = useState(null)
const [stems, setStems] = useState([])
const [detection, setDetection] = useState(null)
const [chordsData, setChordsData] = useState(null)
const [loading, setLoading] = useState(false)
const [error, setError] = useState(null)
const clearError = useCallback(() => {
setError(null)
}, [])
// Upload files and automatically run detection
const upload = useCallback(async (formData) => {
setLoading(true)
setError(null)
try {
// Step 1: Upload files (formData already prepared by FileUpload component)
const uploadResponse = await fetch('/api/upload', {
method: 'POST',
body: formData
})
if (!uploadResponse.ok) {
const data = await uploadResponse.json().catch(() => ({}))
throw new Error(data.detail || `Upload failed: ${uploadResponse.status}`)
}
const uploadData = await uploadResponse.json()
console.log('Upload response:', uploadData)
setSessionId(uploadData.session_id)
setStems(uploadData.stems)
// Step 2: Automatically run detection with the session_id we just got
const detectResponse = await fetch(`/api/detect/${uploadData.session_id}`, {
method: 'POST'
})
if (!detectResponse.ok) {
const data = await detectResponse.json().catch(() => ({}))
throw new Error(data.detail || `Detection failed: ${detectResponse.status}`)
}
const detectData = await detectResponse.json()
console.log('Detection response:', detectData)
setDetection(detectData)
// Auto-fetch chord progression and scale suggestions
fetch(`/api/chords/${uploadData.session_id}`)
.then(r => r.ok ? r.json() : null)
.then(data => { if (data) setChordsData(data) })
.catch(() => {})
return { upload: uploadData, detection: detectData }
} catch (err) {
console.error('Error:', err)
setError(err.message)
return null
} finally {
setLoading(false)
}
}, [])
// Manual detect (if needed separately)
const detect = useCallback(async (sid = null) => {
const id = sid || sessionId
if (!id) {
setError('No session to detect')
return null
}
setLoading(true)
setError(null)
try {
const response = await fetch(`/api/detect/${id}`, {
method: 'POST'
})
if (!response.ok) {
const data = await response.json().catch(() => ({}))
throw new Error(data.detail || `Detection failed: ${response.status}`)
}
const data = await response.json()
setDetection(data)
return data
} catch (err) {
setError(err.message)
return null
} finally {
setLoading(false)
}
}, [sessionId])
const process = useCallback(async (semitones, targetBpm = null, regionStart = null, regionEnd = null) => {
if (!sessionId) {
setError('No session to process')
return null
}
setLoading(true)
setError(null)
try {
const body = { semitones, target_bpm: targetBpm }
if (regionStart !== null && regionEnd !== null) {
body.region_start = regionStart
body.region_end = regionEnd
}
const response = await fetch(`/api/process/${sessionId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
if (!response.ok) {
const data = await response.json().catch(() => ({}))
throw new Error(data.detail || `Processing failed: ${response.status}`)
}
const data = await response.json()
return data
} catch (err) {
setError(err.message)
return null
} finally {
setLoading(false)
}
}, [sessionId])
const getStems = useCallback(async () => {
if (!sessionId) return null
try {
const response = await fetch(`/api/stems/${sessionId}`)
if (!response.ok) {
throw new Error('Failed to get stems')
}
const data = await response.json()
return data
} catch (err) {
setError(err.message)
return null
}
}, [sessionId])
const loadPreset = useCallback(async (presetName) => {
setLoading(true)
setError(null)
try {
const uploadResponse = await fetch(`/api/preset/${presetName}`, { method: 'POST' })
if (!uploadResponse.ok) {
const data = await uploadResponse.json().catch(() => ({}))
throw new Error(data.detail || `Failed to load preset: ${uploadResponse.status}`)
}
const uploadData = await uploadResponse.json()
setSessionId(uploadData.session_id)
setStems(uploadData.stems)
const detectResponse = await fetch(`/api/detect/${uploadData.session_id}`, { method: 'POST' })
if (!detectResponse.ok) {
const data = await detectResponse.json().catch(() => ({}))
throw new Error(data.detail || `Detection failed: ${detectResponse.status}`)
}
const detectData = await detectResponse.json()
setDetection(detectData)
// Auto-fetch chord progression and scale suggestions
fetch(`/api/chords/${uploadData.session_id}`)
.then(r => r.ok ? r.json() : null)
.then(data => { if (data) setChordsData(data) })
.catch(() => {})
return { upload: uploadData, detection: detectData }
} catch (err) {
setError(err.message)
return null
} finally {
setLoading(false)
}
}, [])
const fetchChords = useCallback(async (sid = null) => {
const id = sid || sessionId
if (!id) return null
try {
const response = await fetch(`/api/chords/${id}`)
if (response.ok) {
const data = await response.json()
setChordsData(data)
return data
}
} catch (err) {
console.warn('Chord detection failed:', err.message)
}
return null
}, [sessionId])
const generate = useCallback(async (regionStart, regionEnd, duration = 15.0, prompt = null) => {
if (!sessionId) {
setError('No session to generate')
return null
}
setLoading(true)
setError(null)
try {
const body = { region_start: regionStart, region_end: regionEnd, duration }
if (prompt) {
body.prompt = prompt
}
const response = await fetch(`/api/generate/${sessionId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
if (!response.ok) {
const data = await response.json().catch(() => ({}))
throw new Error(data.detail || `Generation failed: ${response.status}`)
}
const data = await response.json()
return data
} catch (err) {
setError(err.message)
return null
} finally {
setLoading(false)
}
}, [sessionId])
// Extract and analyze a seed from a selected region
const extractSeed = useCallback(async (regionStart, regionEnd) => {
if (!sessionId) {
setError('No session')
return null
}
try {
const response = await fetch('/api/seed/extract', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session_id: sessionId,
start_time: regionStart,
end_time: regionEnd,
}),
})
if (!response.ok) {
const data = await response.json().catch(() => ({}))
throw new Error(data.detail || `Seed extraction failed: ${response.status}`)
}
return await response.json()
} catch (err) {
setError(err.message)
return null
}
}, [sessionId])
// Generate with ACE-Step
const generateAceStep = useCallback(async (params) => {
if (!sessionId) {
setError('No session')
return null
}
setLoading(true)
setError(null)
try {
const response = await fetch('/api/generate/acestep', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ session_id: sessionId, ...params }),
})
if (!response.ok) {
const data = await response.json().catch(() => ({}))
throw new Error(data.detail || `ACE-Step generation failed: ${response.status}`)
}
return await response.json()
} catch (err) {
setError(err.message)
return null
} finally {
setLoading(false)
}
}, [sessionId])
// Check available models
const checkModels = useCallback(async () => {
try {
const response = await fetch('/api/models')
if (response.ok) return await response.json()
} catch (err) {
console.warn('Model check failed:', err.message)
}
return null
}, [])
return {
sessionId,
stems,
detection,
chordsData,
loading,
error,
upload,
detect,
process,
generate,
extractSeed,
generateAceStep,
checkModels,
fetchChords,
getStems,
loadPreset,
clearError
}
}
|