Skip to content

Commit

Permalink
add react and redux, with audio context
Browse files Browse the repository at this point in the history
  • Loading branch information
iagodahlem committed Sep 27, 2017
1 parent e2f222b commit 562fdcf
Show file tree
Hide file tree
Showing 140 changed files with 976 additions and 59 deletions.
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: node_js
node_js:
- 8

cache:
yarn: true
directories:
- node_modules

script:
- yarn test
- yarn build

deploy:
provider: pages
skip_cleanup: true
github_token: $GITHUB_TOKEN
local_dir: build
on:
branch: master
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# drumpad

[![Build Status](https://travis-ci.org/iagodahlem/drumpad.svg?branch=master)](https://travis-ci.org/iagodahlem/drumpad)

Playing with JavaScript and Web Audio API.

## License
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "drumpad",
"repository": "https://github.com/iagodahlem/drumpad.git",
"author": "Iago Dahlem Lorensini <iagodahlemlorensini@gmail.com>",
"homepage": "https://iagodahlem.github.io/drumpad",
"license": "MIT",
"scripts": {
"start": "react-scripts start",
Expand Down
Binary file added public/favicon.ico
Binary file not shown.
40 changes: 40 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>Drumpad</title>
</head>
<body>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
15 changes: 15 additions & 0 deletions public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "Drumpad",
"name": "Drumpad",
"icons": [
{
"src": "favicon.ico",
"sizes": "192x192",
"type": "image/png"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
Binary file added public/samples/clap-808.wav
Binary file not shown.
Binary file added public/samples/clap-analog.wav
Binary file not shown.
Binary file added public/samples/clap-crushed.wav
Binary file not shown.
Binary file added public/samples/clap-fat.wav
Binary file not shown.
Binary file added public/samples/clap-slapper.wav
Binary file not shown.
Binary file added public/samples/clap-tape.wav
Binary file not shown.
Binary file added public/samples/cowbell-808.wav
Binary file not shown.
Binary file added public/samples/crash-808.wav
Binary file not shown.
Binary file added public/samples/crash-acoustic.wav
Binary file not shown.
Binary file added public/samples/crash-noise.wav
Binary file not shown.
Binary file added public/samples/crash-tape.wav
Binary file not shown.
Binary file added public/samples/hihat-808.wav
Binary file not shown.
Binary file added public/samples/hihat-acoustic01.wav
Binary file not shown.
Binary file added public/samples/hihat-acoustic02.wav
Binary file not shown.
Binary file added public/samples/hihat-analog.wav
Binary file not shown.
Binary file added public/samples/hihat-digital.wav
Binary file not shown.
Binary file added public/samples/hihat-dist01.wav
Binary file not shown.
Binary file added public/samples/hihat-dist02.wav
Binary file not shown.
Binary file added public/samples/hihat-electro.wav
Binary file not shown.
Binary file added public/samples/hihat-plain.wav
Binary file not shown.
Binary file added public/samples/hihat-reso.wav
Binary file not shown.
Binary file added public/samples/hihat-ring.wav
Binary file not shown.
Binary file added public/samples/kick-808.wav
Binary file not shown.
Binary file added public/samples/kick-acoustic01.wav
Binary file not shown.
Binary file added public/samples/kick-acoustic02.wav
Binary file not shown.
Binary file added public/samples/kick-big.wav
Binary file not shown.
Binary file added public/samples/kick-classic.wav
Binary file not shown.
Binary file added public/samples/kick-cultivator.wav
Binary file not shown.
Binary file added public/samples/kick-deep.wav
Binary file not shown.
Binary file added public/samples/kick-dry.wav
Binary file not shown.
Binary file added public/samples/kick-electro01.wav
Binary file not shown.
Binary file added public/samples/kick-electro02.wav
Binary file not shown.
Binary file added public/samples/kick-floppy.wav
Binary file not shown.
Binary file added public/samples/kick-gritty.wav
Binary file not shown.
Binary file added public/samples/kick-heavy.wav
Binary file not shown.
Binary file added public/samples/kick-newwave.wav
Binary file not shown.
Binary file added public/samples/kick-oldschool.wav
Binary file not shown.
Binary file added public/samples/kick-plain.wav
Binary file not shown.
Binary file added public/samples/kick-slapback.wav
Binary file not shown.
Binary file added public/samples/kick-softy.wav
Binary file not shown.
Binary file added public/samples/kick-stomp.wav
Binary file not shown.
Binary file added public/samples/kick-tape.wav
Binary file not shown.
Binary file added public/samples/kick-thump.wav
Binary file not shown.
Binary file added public/samples/kick-tight.wav
Binary file not shown.
Binary file added public/samples/kick-tron.wav
Binary file not shown.
Binary file added public/samples/kick-vinyl01.wav
Binary file not shown.
Binary file added public/samples/kick-vinyl02.wav
Binary file not shown.
Binary file added public/samples/kick-zapper.wav
Binary file not shown.
Binary file added public/samples/openhat-808.wav
Binary file not shown.
Binary file added public/samples/openhat-acoustic01.wav
Binary file not shown.
Binary file added public/samples/openhat-analog.wav
Binary file not shown.
Binary file added public/samples/openhat-slick.wav
Binary file not shown.
Binary file added public/samples/openhat-tight.wav
Binary file not shown.
Binary file added public/samples/perc-808.wav
Binary file not shown.
Binary file added public/samples/perc-chirpy.wav
Binary file not shown.
Binary file added public/samples/perc-hollow.wav
Binary file not shown.
Binary file added public/samples/perc-laser.wav
Binary file not shown.
Binary file added public/samples/perc-metal.wav
Binary file not shown.
Binary file added public/samples/perc-nasty.wav
Binary file not shown.
Binary file added public/samples/perc-short.wav
Binary file not shown.
Binary file added public/samples/perc-tambo.wav
Binary file not shown.
Binary file added public/samples/perc-tribal.wav
Binary file not shown.
Binary file added public/samples/perc-weirdo.wav
Binary file not shown.
Binary file added public/samples/ride-acoustic01.wav
Binary file not shown.
Binary file added public/samples/ride-acoustic02.wav
Binary file not shown.
Binary file added public/samples/shaker-analog.wav
Binary file not shown.
Binary file added public/samples/shaker-shuffle.wav
Binary file not shown.
Binary file added public/samples/shaker-suckup.wav
Binary file not shown.
Binary file added public/samples/snare-808.wav
Binary file not shown.
Binary file added public/samples/snare-acoustic01.wav
Binary file not shown.
Binary file added public/samples/snare-acoustic02.wav
Binary file not shown.
Binary file added public/samples/snare-analog.wav
Binary file not shown.
Binary file added public/samples/snare-big.wav
Binary file not shown.
Binary file added public/samples/snare-block.wav
Binary file not shown.
Binary file added public/samples/snare-brute.wav
Binary file not shown.
Binary file added public/samples/snare-dist01.wav
Binary file not shown.
Binary file added public/samples/snare-dist02.wav
Binary file not shown.
Binary file added public/samples/snare-dist03.wav
Binary file not shown.
Binary file added public/samples/snare-electro.wav
Binary file not shown.
Binary file added public/samples/snare-lofi01.wav
Binary file not shown.
Binary file added public/samples/snare-lofi02.wav
Binary file not shown.
Binary file added public/samples/snare-modular.wav
Binary file not shown.
Binary file added public/samples/snare-noise.wav
Binary file not shown.
Binary file added public/samples/snare-pinch.wav
Binary file not shown.
Binary file added public/samples/snare-punch.wav
Binary file not shown.
Binary file added public/samples/snare-smasher.wav
Binary file not shown.
Binary file added public/samples/snare-sumo.wav
Binary file not shown.
Binary file added public/samples/snare-tape.wav
Binary file not shown.
Binary file added public/samples/snare-vinyl01.wav
Binary file not shown.
Binary file added public/samples/snare-vinyl02.wav
Binary file not shown.
Binary file added public/samples/tom-808.wav
Binary file not shown.
Binary file added public/samples/tom-acoustic01.wav
Binary file not shown.
Binary file added public/samples/tom-acoustic02.wav
Binary file not shown.
Binary file added public/samples/tom-analog.wav
Binary file not shown.
Binary file added public/samples/tom-chiptune.wav
Binary file not shown.
Binary file added public/samples/tom-fm.wav
Binary file not shown.
Binary file added public/samples/tom-lofi.wav
Binary file not shown.
Binary file added public/samples/tom-rototom.wav
Binary file not shown.
Binary file added public/samples/tom-short.wav
Binary file not shown.
71 changes: 71 additions & 0 deletions src/actions/sampleKitActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import audioBufferService from '../services/audioBufferService'
import * as fromAudioContext from '../reducers/audioContextReducer'
import * as fromSamples from '../reducers/samplesReducer'
import * as types from '../constants/actionTypes'

const fetchSampleKit = () => (dispatch, getState) => {
const fetchSampleKitRequest = () => ({
type: types.FETCH_SAMPLE_KIT_REQUEST,
})

const fetchSampleKitSuccess = (payload) => ({
type: types.FETCH_SAMPLE_KIT_SUCCESS,
payload,
})

const fetchSampleKitFailure = (error) => ({
type: types.FETCH_SAMPLE_KIT_FAILURE,
message: error.message || 'Something went wrong.',
})

const context = fromAudioContext.getAudioContext(getState())
const defaultSampleKit = fromSamples.getAll(getState())

dispatch(fetchSampleKitRequest())

const sampleKit = defaultSampleKit
.map(sample => {
return audioBufferService
.load(context, sample.url)
.then(buffer => ({
...sample,
buffer,
start: audioBufferService.findStartGapDuration(buffer),
}))
})

return Promise.all(sampleKit)
.then(payload => dispatch(fetchSampleKitSuccess(payload)))
.catch(error => dispatch(fetchSampleKitFailure(error)))
}

const playSample = (id) => (dispatch, getState) => {
const playSampleStart = (id) => ({
type: types.PLAY_SAMPLE_START,
id,
})

const playSampleStop = (id) => ({
type: types.PLAY_SAMPLE_STOP,
id,
})

const context = fromAudioContext.getAudioContext(getState())
const sample = fromSamples.getSample(getState(), id)

dispatch(playSampleStart(id))

return audioBufferService.play(context, sample, () => {
dispatch(playSampleStop(id))
})
}

export default {
fetchSampleKit,
playSample,
}

export {
fetchSampleKit,
playSample,
}
61 changes: 61 additions & 0 deletions src/actions/sampleKitActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import axios from 'axios'
import httpAdapter from 'axios/lib/adapters/http'
import nock from 'nock'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as sampleKitActions from './sampleKitActions'
import * as types from '../constants/actionTypes'
import audioBufferService from '../services/audioBufferService'

const host = 'http://localhost'

const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)

const sample = { id: 'sample', name: 'sample', url: '/sample-url', start: 0 }
const state = {
allIds: ['sample'],
byId: { sample },
}

const audioBuffer = {
duration: 0.48036281179138324,
length: 21184,
numberOfChannels: 1,
sampleRate: 44100,
}

// TODO: set this in a jest global setup
axios.defaults.host = host
axios.defaults.adapter = httpAdapter

describe('sampleKitActions', () => {
beforeEach(() => {
audioBufferService.findStartGapDuration = jest.fn()
.mockReturnValue(0)
})

afterEach(nock.cleanAll)

describe('fetchSamples', () => {
it('success', async () => {
audioBufferService.load = jest.fn()
.mockReturnValue(Promise.resolve(audioBuffer))

nock(host)
.get('/sample-url')
.reply(200, { buffer: [] })

const payload = [{ ...sample, buffer: audioBuffer }]
const store = mockStore({ samples: state })
const expectedActions = [
{ type: types.FETCH_SAMPLE_KIT_REQUEST },
{ type: types.FETCH_SAMPLE_KIT_SUCCESS, payload },
]

await store.dispatch(sampleKitActions.fetchSampleKit())

expect(store.getActions()).toEqual(expectedActions)
})
})
})
3 changes: 3 additions & 0 deletions src/components/App/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.App {
height: 100%;
}
12 changes: 12 additions & 0 deletions src/components/App/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react'
import './App.css'

import Drumpad from '../../containers/Drumpad'

const App = () => (
<div className='App'>
<Drumpad />
</div>
)

export default App
18 changes: 18 additions & 0 deletions src/components/App/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react'
import { shallow } from 'enzyme'

import App from './App'

describe('App', () => {
let component
let props

beforeEach(() => {
props = {}
component = shallow(<App {...props} />)
})

it('renders', () => {
// expect(component).toMatchSnapshot()
})
})
3 changes: 3 additions & 0 deletions src/components/App/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import App from './App'

export default App
20 changes: 20 additions & 0 deletions src/components/Key/Key.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.Key {
width: calc(33.3% - 6px);
height: calc(33.3% - 6px);
margin: 3px;
border-radius: 5px;
background-color: rgba(255,255,255,.5);
transition: background-color .2s ease;
}

.Key::after {
content: '';
display: block;
width: 100%;
height: 100%;
background-image: radial-gradient(rgba(243,243,243,.8) 0%, transparent 100%);
}

.Key.isPlaying {
background-color: #ff0;
}
17 changes: 17 additions & 0 deletions src/components/Key/Key.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'
import PropTypes from 'prop-types'
import './Key.css'

const toggleClass = (isPlaying) => isPlaying ? 'Key isPlaying' : 'Key'

const Key = ({ isPlaying, letter, sample, onClick }) => (
<div className={toggleClass(isPlaying)} onClick={(e) => onClick(e, sample)}>
</div>
)

Key.propTypes = {
sample: PropTypes.string.isRequired,
onClick: PropTypes.func.isRequired,
}

export default Key
17 changes: 17 additions & 0 deletions src/components/Key/Key.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react'
import { shallow } from 'enzyme'

import Key from './Key'

describe('Key', () => {
let component, props

beforeEach(() => {
props = {}
component = shallow(<Key {...props} />)
})

it('should', () => {
// expect(component).toMatchSnapshot()
})
})
3 changes: 3 additions & 0 deletions src/components/Key/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Key from './Key.js'

export default Key
12 changes: 12 additions & 0 deletions src/components/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import App from './App'
import Key from './Key'

export default {
App,
Key,
}

export {
App,
Key,
}
10 changes: 10 additions & 0 deletions src/constants/actionTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const FETCH_SAMPLE_KIT_REQUEST = 'FETCH_SAMPLE_KIT_REQUEST'
export const FETCH_SAMPLE_KIT_SUCCESS = 'FETCH_SAMPLE_KIT_SUCCESS'
export const FETCH_SAMPLE_KIT_FAILURE = 'FETCH_SAMPLE_KIT_FAILURE'

export const FETCH_SAMPLE_REQUEST = 'FETCH_SAMPLE_REQUEST'
export const FETCH_SAMPLE_SUCCESS = 'FETCH_SAMPLE_SUCCESS'
export const FETCH_SAMPLE_FAILURE = 'FETCH_SAMPLE_FAILURE'

export const PLAY_SAMPLE_START = 'PLAY_SAMPLE_START'
export const PLAY_SAMPLE_STOP = 'PLAY_SAMPLE_STOP'
14 changes: 14 additions & 0 deletions src/constants/defaultSampleKit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const byId = (kit) => kit
.reduce((samples, sample) => {
samples[sample.id] = sample
return samples
},{})

const allIds = (kit) => kit.map(sample => sample.id)

const defaultSampleKit = (kit) => ({
byId: byId(kit),
allIds: allIds(kit),
})

export default defaultSampleKit
13 changes: 13 additions & 0 deletions src/constants/keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const keys = [
{ letter: 't', sample: 'clap-808' },
{ letter: 'y', sample: 'cowbell-808' },
{ letter: 'u', sample: 'crash-808' },
{ letter: 'g', sample: 'hihat-808' },
{ letter: 'h', sample: 'kick-808' },
{ letter: 'j', sample: 'openhat-808' },
{ letter: 'b', sample: 'perc-808' },
{ letter: 'n', sample: 'snare-808' },
{ letter: 'm', sample: 'tom-808' },
]

export default keys
13 changes: 13 additions & 0 deletions src/constants/sampleKit808.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const kit808 = [
{ id: 'clap-808', name: 'Clap 808', url: '/clap-808.wav', kit: '808' },
{ id: 'cowbell-808', name: 'Cowbell 808', url: '/cowbell-808.wav', kit: '808' },
{ id: 'crash-808', name: 'Crash 808', url: '/crash-808.wav', kit: '808' },
{ id: 'hihat-808', name: 'Hihat 808', url: '/hihat-808.wav', kit: '808' },
{ id: 'kick-808', name: 'Kick 808', url: '/kick-808.wav', kit: '808' },
{ id: 'openhat-808', name: 'Openhat 808', url: '/openhat-808.wav', kit: '808' },
{ id: 'perc-808', name: 'Perc 808', url: '/perc-808.wav', kit: '808' },
{ id: 'snare-808', name: 'Snare 808', url: '/snare-808.wav', kit: '808' },
{ id: 'tom-808', name: 'Tom 808', url: '/tom-808.wav', kit: '808' },
]

export default kit808
Loading

0 comments on commit 562fdcf

Please sign in to comment.