Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat equalizer #296

Merged
merged 16 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(sound): add equalizer using AudioContext api
  • Loading branch information
Charles Jacquin committed Mar 31, 2019
commit 6f5026df5471ddadb03922374a38c2d228c72cc0
2 changes: 1 addition & 1 deletion app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class App extends React.Component {
{this.renderNavLink('plugins', 'flask', 'Plugins', settings)}
{this.renderNavLink('search', 'search', 'Search Results', settings)}
{this.renderNavLink('settings', 'cogs', 'Settings', settings)}
{this.renderNavLink('equalizer', 'cogs', 'Equalizer', settings)}
{this.renderNavLink('equalizer', 'sliders', 'Equalizer', settings)}

<SidebarMenuCategoryHeader>
Collection
Expand Down
2 changes: 2 additions & 0 deletions app/components/Equalizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const Equalizer = ({ values, onChange }) => (
<div key={idx} className={styles.slider_container}>
<input
type='range'
min='-10'
max='10'
className={styles.slider}
value={value}
onChange={evt => {
Expand Down
65 changes: 62 additions & 3 deletions app/components/Sound/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'lodash';

import styles from './styles.scss';

const filterValues = [500, 1500, 2500, 4000, 6500, 8500, 12000, 16000];

const qValues = filterValues.map((freq, i, arr) => {
if (!i || i === arr.length - 1) {
return null;
} else {
return 2 * freq / Math.abs(arr[i + 1] - arr[i - 1]);
}
});

class Sound extends React.Component {
audio;
audioContext;
gainNode;
source;
filters = [];

constructor(props) {
super(props);
Expand All @@ -14,7 +29,37 @@ class Sound extends React.Component {
}

attachRef(element) {
this.audio = element;
if (element) {
this.audio = element;
this.audio.crossOrigin = 'anonymous';
}
}

createFilterNodes() {
let lastInChain = this.gainNode;

filterValues.forEach((freq, i, arr) => {
const biquadFilter = this.audioContext.createBiquadFilter();

biquadFilter.type = 'peaking';
biquadFilter.frequency.value = freq;
biquadFilter.gain.value = this.props.equalizer[i] || 0;
if (!i || i === arr.length - 1) {
biquadFilter.type = i ? 'highshelf' : 'lowshelf';
} else {
biquadFilter.Q.value = qValues[i];
}

if (lastInChain) {
lastInChain.connect(biquadFilter);
}

lastInChain = biquadFilter;

this.filters.push(biquadFilter);
});

return lastInChain;
}

handleTimeUpdate({ target }) {
Expand Down Expand Up @@ -46,7 +91,7 @@ class Sound extends React.Component {
}

setVolume() {
this.audio.volume = this.props.volume / 100;
this.gainNode.gain.value = this.props.volume / 100;
}

setPosition() {
Expand All @@ -65,9 +110,22 @@ class Sound extends React.Component {
if (this.props.playStatus !== prevProps.playStatus) {
this.setPlayerState();
}

if (!_.isEqual(this.props.equalizer, prevProps.equalizer)) {
this.props.equalizer.forEach((value, idx) => {
this.filters[idx].gain.value = value;
});
}
}

componentDidMount() {
this.audioContext = new AudioContext();
this.gainNode = this.audioContext.createGain();
this.source = this.audioContext.createMediaElementSource(this.audio);

this.source.connect(this.gainNode);
this.createFilterNodes().connect(this.audioContext.destination);

this.setVolume();
this.setPlayerState();
}
Expand Down Expand Up @@ -104,7 +162,8 @@ Sound.propTypes = {
onLoading: PropTypes.func,
onLoad: PropTypes.func,
position: PropTypes.number,
volume: PropTypes.number
volume: PropTypes.number,
equalizer: PropTypes.arrayOf(PropTypes.number)
};

export default Sound;
Expand Down
1 change: 1 addition & 0 deletions app/containers/SoundContainer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class SoundContainer extends React.Component {
onLoad={this.handleLoaded.bind(this)}
position={player.seek}
volume={player.muted ? 0 : player.volume}
equalizer={player.equalizer}
/>
);
}
Expand Down
2 changes: 1 addition & 1 deletion app/reducers/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const initialState = {
seek: 0,
volume: 100,
muted: false,
equalizer: [500, 1500, 2500, 4000, 6500, 8500, 12000, 16000]
equalizer: [0, 0, 0, 0, 0, 0, 0, 0]
};

export default function PlayerReducer(state=initialState, action) {
Expand Down