-
Notifications
You must be signed in to change notification settings - Fork 6
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
1 parent
ecd60c2
commit 61aafa1
Showing
3 changed files
with
171 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Функция округляет значение до 4 знаков после запятой по-умолчанию | ||
// Принимает число и количество знаков после запятой | ||
// Отдаёт округлённое число | ||
export function toFixedNumber(number, digits = 4) { | ||
let pow = Math.pow(10, digits) | ||
return Math.round(number * pow) / pow | ||
} | ||
|
||
// Функция выполняет целочисленное деление | ||
// Принимает два числа, что на что делить | ||
// Отдаёт искомое число | ||
export function div(value, by) { | ||
return (value - (value % by)) / by | ||
} | ||
|
||
// Функция ищет ближайшее меньшее и большее число к заданному | ||
// Принимает: | ||
// - число, вокруг которого нужно найти ближайшие значения | ||
// - массив чисел, из которых выбираем ближайшие значения | ||
// Отдаёт массив из двух чисел: меньшее и большее | ||
export function getNearbyValues(number, array) { | ||
const nearbyLess = Math.max(...array.filter((value) => value < number)) | ||
const nearbyOver = Math.min(...array.filter((value) => value > number)) | ||
|
||
return [nearbyLess, nearbyOver] | ||
} |
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,55 @@ | ||
// Функция генерирует звукоряд | ||
|
||
// Принимает: | ||
// root - базовая частота, самая низкая частота | ||
// octaveAmount - количество октав | ||
// tonesInOctaveAmount - количество тонов (нот) в октаве | ||
|
||
// Отдаёт: | ||
// const tonesArray[] - массив тонов (звукоряд) | ||
|
||
import { div, getNearbyValues, toFixedNumber } from './helpers' | ||
|
||
export const notes = [] | ||
|
||
export function notesInit(root = 8.1757, octaveAmount = 12, tonesInOctaveAmount = 12) { | ||
let tonesAmount = octaveAmount * tonesInOctaveAmount // Количество тонов | ||
|
||
for (let i = 0; i < tonesAmount; i++) { | ||
notes[i] = toFixedNumber(root * 2 ** (i / tonesInOctaveAmount)) // Формируем равномерно темперированный звукоряд | ||
} | ||
} | ||
|
||
// Функция преобразует индекс массива notes[] в название ноты | ||
|
||
const notesNames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'] | ||
|
||
export function getNoteName(frequency) { | ||
// Находим индекс в массиве нот | ||
const notesIndex = notes.indexOf(frequency) | ||
|
||
// Если такого значения в массиве нет, значит мы звучим за диапазоном нот | ||
if (notesIndex < 0) { | ||
return `За диапазоном нот` | ||
} | ||
|
||
// Номер октавы | ||
const octave = div(notesIndex, 12) - 1 | ||
|
||
// Порядковый номер ноты в рамках октавы | ||
// Например, D == 3 (C - C# - D) | ||
const noteNumberOnOctave = notesIndex + 1 - 12 * (octave + 1) | ||
|
||
// Собираем название ноты вместе с номером октавы | ||
const noteName = notesNames[noteNumberOnOctave - 1] + String(octave) | ||
|
||
return noteName | ||
} | ||
|
||
// Функция приводит звучащий звук к ближайшей ноте | ||
// pitchCorrection | ||
export function pitchDetection(frequency) { | ||
const nearbyValues = getNearbyValues(frequency, notes) | ||
console.log(nearbyValues[0], nearbyValues[1]) | ||
console.log(getNoteName(nearbyValues[0]), getNoteName(nearbyValues[1])) | ||
} |