-
Notifications
You must be signed in to change notification settings - Fork 1
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
13 changed files
with
230 additions
and
39 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
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
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,68 @@ | ||
.sample-browser { | ||
background: #000; | ||
height: 107px; | ||
width: 160px; | ||
border-left: 1px solid #555; | ||
border-right: 1px solid #555; | ||
border-top: 2px solid #666; | ||
border-radius: 4px; | ||
padding: 4px; | ||
overflow: hidden; | ||
} | ||
|
||
.file-input { | ||
display: none; | ||
} | ||
|
||
.samples { | ||
max-height: 80px; | ||
overflow-y: scroll; | ||
display: flex; | ||
flex-direction: column; | ||
border: 1px solid var(--primary); | ||
margin-bottom: 4px; | ||
} | ||
|
||
.samples::-webkit-scrollbar { | ||
width: 8px; | ||
} | ||
|
||
.samples::-webkit-scrollbar-track { | ||
background: #000; | ||
border-left: 1px solid var(--primary); | ||
} | ||
|
||
.samples::-webkit-scrollbar-thumb { | ||
background: var(--primary); | ||
} | ||
|
||
.samples button { | ||
font-size: 12px; | ||
text-decoration: none; | ||
text-transform: uppercase; | ||
display: block; | ||
text-align: left; | ||
color: var(--primary); | ||
border-bottom: 1px solid var(--primary); | ||
padding: 0 2px; | ||
} | ||
|
||
.samples button.selected { | ||
background-color: var(--primary); | ||
color: #000; | ||
} | ||
.controls { | ||
display: flex; | ||
justify-content: end; | ||
} | ||
|
||
.controls button { | ||
font-size: 12px; | ||
text-decoration: none; | ||
text-transform: uppercase; | ||
display: block; | ||
text-align: left; | ||
color: var(--primary); | ||
border: 1px solid var(--primary); | ||
padding: 0 2px; | ||
} |
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,81 @@ | ||
import { h, useEffect, useRef, useState } from 'kaiku' | ||
import * as api from '../../api' | ||
import * as engine from '../../engine' | ||
import css from './SampleBrowser.css' | ||
|
||
type Props = { | ||
onSelect: (id: string) => void | ||
selected: null | string | ||
} | ||
|
||
const SampleBrowser = ({ onSelect, selected }: Props) => { | ||
const fileInputRef = useRef<HTMLInputElement>() | ||
const browserState = useState<{ | ||
samples: { | ||
id: string | ||
name: string | ||
ownerId: string | ||
}[] | ||
}>({ | ||
samples: [], | ||
}) | ||
|
||
const fetchSamples = () => { | ||
api.getSamples().then((samples) => { | ||
browserState.samples = samples | ||
}) | ||
} | ||
|
||
useEffect(fetchSamples) | ||
|
||
const onFileChange = async (evt: InputEvent) => { | ||
const fileElement = evt.target! as HTMLInputElement | ||
const file = fileElement.files?.[0] | ||
if (file) { | ||
const buffer = ( | ||
await engine.getAudioContext().decodeAudioData(await file.arrayBuffer()) | ||
).getChannelData(0) | ||
|
||
if (buffer.length > 44100 * 20) { | ||
// TODO: Tell user that the sample is too long | ||
return | ||
} | ||
|
||
const { id } = await api.saveSample(file.name, buffer) | ||
onSelect(id) | ||
fetchSamples() | ||
} | ||
} | ||
|
||
const addSample = () => { | ||
if (fileInputRef.current) { | ||
fileInputRef.current.click() | ||
} | ||
} | ||
|
||
return ( | ||
<div className={css('sample-browser')}> | ||
<input | ||
type="file" | ||
className={css('file-input')} | ||
ref={fileInputRef} | ||
onChange={onFileChange} | ||
/> | ||
<div className={css('samples')}> | ||
{browserState.samples.map((sample) => ( | ||
<button | ||
onClick={() => onSelect(sample.id)} | ||
className={css({ selected: selected === sample.id })} | ||
> | ||
{sample.name} | ||
</button> | ||
))} | ||
</div> | ||
<div className={css('controls')}> | ||
<button onClick={addSample}>add</button> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default SampleBrowser |
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
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
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