JSFiddle - React, Tailwind, and code Playground

by MaxVMH

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:900" />
</head>
<body>
    <div class="body">
        <div class="title">
            <h1>Frequency Trainer</h1>
        </div>
        <div class="controls">
            <br />
            <button type="button" id="start-button" class="control-button">Start</button>
            <button type="button" id="stop-button" class="control-button">Stop</button>
            <button type="button" id="next-button" class="control-button">Next</button><br />
            <br />
            Volume:<br />
            <input type="range" id="volume-control" class="volume-control" min="0" max="20" value="2" step="0.1" /><br />
            <br />
            <button type="button" id="difficulty-easy" class="difficulty-button" data-difficulty="easy">Easy</button>
            <button type="button" id="difficulty-normal" class="difficulty-button" data-difficulty="normal">Normal</button>
            <button type="button" id="difficulty-hard" class="difficulty-button" data-difficulty="hard">Hard</button>
            <button type="button" id="difficulty-pro" class="difficulty-button" data-difficulty="pro">Pro</button><br />
            <br />
        </div>
        <div class="grid">
        </div>
        <div class="footer">
            <a href="https://github.com/MaxVMH/frequency-trainer/tree/v.0.0.4-alpha">v.0.0.4</a>
        </div>
    </div>

    <script>
    (function () {
        'use strict';
        let difficultyMode = 'easy'; // default difficulty mode
        let frequencyTrainer = startFrequencyTrainer(difficultyMode);
        let frequency = frequencyTrainer.frequency;
        let frequencyContainers = null;

        // Control buttons
        let startButton = document.getElementById('start-button');
        startButton.onclick = function () {
            stopToneGenerator();
            startToneGenerator(frequency, volumeControl, 0, 3);
        };
...

CSS

body {
    font-family: 'Montserrat', sans-serif;
    text-align: center;
    padding-top: 10px;
}

h1 {
    margin: 0 auto;
    font-size: 30px;
    text-decoration: underline;
}

h2 {
    margin: 0;
    font-size: 25px;
}

a {
    color: #0000BB;
}

a:hover {
    color: #000000;
}

button {
    font-family: 'Montserrat', sans-serif;
    text-align: center;
    font-size: calc(10px + 1vw);
}

.body {
    max-width: 1500px;
    border: 1px solid black;
    width: 95%;
    margin: 0 auto;
}

.title {
    padding: 10px 0 0 0;
    margin: 0 auto;
    width: 95%;
}

.content {
    padding: 30px 0 0 0;
    margin: 0 auto;
    width: 95%;
}

.controls {
    padding: 0;
    margin: 0 auto;
    width: 95%;
}

.volume-control {
    padding: 0;
    margin: 0 auto;
    min-width: 200px;
    width: 80%;
}

.footer {
    padding: 20px 0 10px 0;
    margin: 0 auto;
    width: 95%;
}

.grid {
    margin: 0 auto;
    width: 95%;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
}

.frequency-container {
    margin: 2px;
    border: 1px solid black;
    padding: 0;

    min-width: 80px;
    min-height: 80px;
    max-width: 300px;
    max-height: 300px;

    display: flex;
    align-items: center;
    justify-content: center;

    font-size: calc(30px + 0.2vw);
    text-shadow: 0 0 25px white;
}

.frequency-container:before {
    content: '';
    padding-top: 100%;
    float: left;
}

[data-frequency="20"] {
    background: #CC2828;
}

[data-frequency="25"] {
    background: #CC3028;
}

[data-frequency="31.5"] {
    background: #CC3928;
}

[data-frequency="40"] {
    background: #CC4128;
}

[data-frequency="50"] {
    background: #CC4928;
}

[data-frequency="63"] {
    background: #CC5128;
}

[data-frequency="80"] {
    background: #CC5928;
}

[data-frequency="100"] {
    background: #CC6128;
}

[data-frequency="125"] {
    background: #CC6A28;
}

[data-frequency="160"] {
    background: #CC7228;
}

[data-frequency="200"] {
    background:...

JavaScript

let toneContext = null;
let toneGenerator = null;
let toneAmplifier = null;

function startFrequencyTrainer(difficultyMode, previousFrequency) {
    'use strict';
    let frequencies = null;
    let frequency = null;

    // Create objects
    toneContext = new(window.AudioContext || window.webkitAudioContext)();
    toneAmplifier = toneContext.createGain();

    // Pick a frequency
    frequencies = getFrequencies(difficultyMode);
    frequency = getNewFrequency(frequencies, previousFrequency);

    return {
        frequencies,
        frequency
    };
}

function stopFrequencyTrainer() {
    toneContext.close();
}

function startToneGenerator(frequency, volumeControl, startTimer, stopTimer) {
    'use strict';
    // Create and configure the oscillator
    toneGenerator = toneContext.createOscillator();
    toneGenerator.type = 'sine'; // could be sine, square, sawtooth or triangle
    toneGenerator.frequency.value = frequency;

    // Connect toneGenerator -> toneAmplifier -> output
    toneGenerator.connect(toneAmplifier);
    toneAmplifier.connect(toneContext.destination);

    // Set the gain volume
    toneAmplifier.gain.value = volumeControl.value / 100;

    // Fire up the toneGenerator
    toneGenerator.start(toneContext.currentTime + startTimer);
    toneGenerator.stop(toneContext.currentTime + startTimer + stopTimer);
}

function stopToneGenerator() {
    'use strict';
    if (toneGenerator) {
        toneGenerator.disconnect();
    }
}

function changeVolume(volumeControl) {
    'use strict';
    toneAmplifier.gain.value = volumeControl.value / 100;
}

function getFrequencies(difficultyMode) {
    'use strict';
    let frequencies = null;

    if (difficultyMode === 'easy') {
        frequencies = ["250", "800", "2500", "8000"];
    } else if (difficultyMode === 'normal') {
        frequencies = ["100", "200", "400", "800", "1600", "3150", "6300", "12500"];
    } else if (difficultyMode === 'hard') {
        frequencies = ["80", "125", "160", "250",...