How to call certain chord buttons instead of all the chord buttons from the chord library?

Viewed 44

I have been able to build a chord library that plays the chord and also says the chord name. I'm now trying to build upon the chord library. How can I have only a limited amount of chord buttons be displayed on the UI instead of all of the chords that are being populated?

The top portion of the code (that is below) is displaying the four buttons but not playing the chord sounds or saying the chord names.

The other code after is showing all the chord buttons and playing the chord sounds and chord names.

We're trying to combine the two portions of code from below so that they play the chord names and sounds for the four chords above.

 //In order to run in terminal insert the following code to activate localhost: npx parcel src/index.html
import { chordType, transpose, note } from '@tonaljs/tonal';
import { chord } from '@tonaljs/chord';
import { entries } from '@tonaljs/chord-dictionary';
import { Howler, howl } from 'howler';

const buttons = document.querySelector(".buttons");

const arrayOfChordNames = ["Major", "Minor", "Augmented", "Diminished"];

arrayOfChordNames.forEach((chord) => {
  let buttonElement = createElement("button", chord);
  buttons.appendChild(buttonElement);
});

function createElement(element, content) {
  let newElement = document.createElement(element);
  newElement.textContent = content;
  return newElement;
}


// code above is displaying the four buttons but not playing the chord sound and chord name
//code below is showing all the chord buttons and playing the chord sound and chord names
// we're trying combine the chord from below so that it plays the audio from the chord names and sounds for the four chords above 






const sound = new Howl({
    src: ['assets/pianosprite.mp3'],
    onload() {
        console.log('Sound file has been loaded. Do something here!');
        soundEngine.init();
    },
    onloaderror(e, msg) {
        console.log('Error', e, msg);
    }
});

const startNotes = ['C', 'C#', 'Db', 'D', 'D#', 'Eb', 'E', 'F', 'F#', 'Gb', 'G', 'G#', 'Ab', 'A', 'A#', 'Bb', 'B']; 

const startNoteSelector = document.querySelector('#start-note');
const octaveSelector = document.querySelector('#octave');
const buttons = document.querySelector('.buttons');
const intervalsInChord = document.querySelector('.intervals-in-chord');
const notesInChord = document.querySelector('.notes-in-chord');

let selectedStartNote = 'C';
let selectedOctave = '1';

const app = {
    init() {
        this.setupStartNotes();
        this.setupOctaves();
        this.setupButtons();
        this.setupEventListeners();
    },
    setupStartNotes() {
        startNotes.forEach(noteName => {
            let noteNameOption = this.createElement('option', noteName);
            startNoteSelector.appendChild(noteNameOption);
        });
    },
    setupOctaves() {
        for (let i = 1; i <= 6; i++) {
            let octaveNumber = this.createElement('option', i);
            octaveSelector.appendChild(octaveNumber);
        }
    },
    setupButtons() {
        const chordNames = entries().map(entry => {
            return entry.aliases[0];
        }); 
        chordNames.forEach(chordName => {
            let chordButton = this.createElement('button', chordName);
            buttons.appendChild(chordButton);
        });
        
    },
    setupEventListeners() {
        startNoteSelector.addEventListener('change', () => {
            selectedStartNote = startNoteSelector.value;
        });
        octaveSelector.addEventListener('change', () => {
            selectedOctave = octaveSelector.value;
        });
        buttons.addEventListener('click', (event) => {
            if (event.target.classList.contains('buttons')) {
                return;
            }
            selectedChord = event.target.innerText;
            this.displayAndPlayChord(selectedChord, event);
        });
    },
    displayAndPlayChord(selectedChord, event) {
        let chordIntervals = chord(selectedChord).intervals;
        intervalsInChord.innerText = chordIntervals.join(' - ');
        
        const startNoteWithOctave = selectedStartNote + selectedOctave;
        let chordNotes = chordIntervals.map(val => {
            return transpose(startNoteWithOctave, val);
        });
        notesInChord.innerText = chordNotes.join(' - ');
        soundEngine.play(chordNotes, event);
    },
    createElement(elementName, content) {
        let element = document.createElement(elementName);
        element.innerHTML = content;
        return element;
    }
}

const soundEngine = {
    init() {
        const lengthOfNote = 2400;
        let timeIndex = 0;
        for (let i = 24; i <= 96; i++) {
            sound['_sprite'][i] = [timeIndex, lengthOfNote];
            timeIndex += lengthOfNote;
        }
        
    },

    play(soundSequence, event) {
        const buttons =
        document.querySelector(".buttons");

        const chordNameTable = {
        "M": "Major",
        "m": "Minor",
        "dim": "diminished",
        "aug": "Augmented"
    
          }

          buttons.addEventListener("click", (event) => {
            
          })
          function textToSpeech(message, chord) {
            const speech = new SpeechSynthesisUtterance();
            speech.lang = "en-US";
            speech.text = message;
            speech.volume = 1;
            speech.rate = 1;
            speech.pitch = 1;
            window.speechSynthesis.speak(speech);
            // When speaking has finished
            speech.onend = function() {
              playChord(chord);
            }
          }
          
          function playChord(chord) {
            // Wait a second (1000 miliseconds) before playing the chord
            setTimeout(() => {
                const chordMidiNumbers = soundSequence.map(noteName => {
                    return note(noteName).midi;
                });
                sound.volume(0.05);
                chordMidiNumbers.forEach(noteMidiNumber => {
                    
                    sound.play(noteMidiNumber.toString());
                });
              console.log("Chord to be played", chord);
            }, 500);
          }
          const sayThis = chordNameTable[event.target.textContent];
            textToSpeech(sayThis, event.target.textContent);
    }
}

app.init();

const allChordNames = entries()
chordEntries.map(entry => {
    return entry.aliases[0];
})
console.log(Array.isArray (allChordNames));
body {
    font-family: Lato, Sans-serif;
    color: #fff;
    background: #000000
}


.controls{
    display: flex;
    justify-content: center;
}
.controls select {
    margin: 0 10px;
}
.chord-notes {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 1.5em;
}
.chord-notes p {
    margin: 10px 0;
}
/*width makes the page more condensed towards the left*/
.buttons {
    display: flex;
    flex-wrap: wrap;
    width: 60%;
}
/*min-width divides columns into 3 instead of 4, max-width creates a set size for the button*/
.buttons button{
    flex-grow: 1;
    min-width: 20%;
    max-width: 20%;
    height: 50px;
    font-size: 1em;
    background: #7200CC;
    border: none;
    color: #fff;
    margin: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chord Dictionary</title>
    <link rel="stylesheet" href="styles.css">
    <head>
        <style>
            ul {
              list-style-type: none;
              margin: 0;
              padding: 0;
              overflow: hidden;
              background-color: #333;
            }

            li {
              float: left;
            }

            li a {
              display: block;
              color: white;
              text-align: center;
              padding: 14px 16px;
              text-decoration: none;
            }

            li a:hover {
              background-color: #A400DD;
            }
        </style>
    </head>
</head>
    <ul>
        <li><a class="active" href="/home">Home</a></li>
        <li><a href="/lessons">Lessons</a></li>
        <li><a href="/practice">Practice</a></li>
        <li><a href="/test">Test</a></li>
        <li><a href="/demo">Demo</a></li>
        <li><a href="/src/triads.html">Triads</a></li>
        <li><a href="/sign up">Sign Up</a></li>
        <li><a href="/login">Login</a></li>
    </ul>
</head>

<body>
    <h1>This is the Triads page!</h1>
    <div class="controls">
        <label for="start-note">Start note:</label>
        <select name="start note" id="start-note">
           
        </select>

        <label for="octave">Octave: </label>
        <select name="Octave" id="octave">
            
        </select>

        <label for="Loops">Loops: </label>
        <select id="Loops">
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="loop">loop</option>
        </select>
        
    </div>
    <div class="chord-notes">
        <p class="notes-in-chord">C3 - D3 - E3 - F#3 - A3</p>
        <p class="intervals-in-chord">P1 - M3 - P5 - M7</p>
    </div>
    <div class="buttons">
        
    </div>
    <script type="module" src="triads.js"></script>
</body>
</html>

0 Answers
Related