Skip to content

Commit

Permalink
Merge pull request #16 from novikov-alexander/onset
Browse files Browse the repository at this point in the history
Onset implementation
  • Loading branch information
qiuxiang committed Sep 21, 2022
2 parents 7760dd8 + d3fdd56 commit 9db17d0
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.6)
project(aubiojs)

set(source_files
${PROJECT_SOURCE_DIR}/aubio/src/onset/onset.c
${PROJECT_SOURCE_DIR}/aubio/src/onset/peakpicker.c
${PROJECT_SOURCE_DIR}/aubio/src/pitch/pitch.c
${PROJECT_SOURCE_DIR}/aubio/src/pitch/pitchfcomb.c
Expand All @@ -11,6 +12,7 @@ set(source_files
${PROJECT_SOURCE_DIR}/aubio/src/pitch/pitchyin.c
${PROJECT_SOURCE_DIR}/aubio/src/pitch/pitchyinfast.c
${PROJECT_SOURCE_DIR}/aubio/src/pitch/pitchyinfft.c
${PROJECT_SOURCE_DIR}/aubio/src/spectral/awhitening.c
${PROJECT_SOURCE_DIR}/aubio/src/spectral/fft.c
${PROJECT_SOURCE_DIR}/aubio/src/spectral/ooura_fft8g.c
${PROJECT_SOURCE_DIR}/aubio/src/spectral/phasevoc.c
Expand All @@ -29,7 +31,8 @@ set(source_files
${PROJECT_SOURCE_DIR}/aubio/src/lvec.c
${PROJECT_SOURCE_DIR}/aubio/src/mathutils.c
${PROJECT_SOURCE_DIR}/src/tempo.cc
${PROJECT_SOURCE_DIR}/src/pitch.cc)
${PROJECT_SOURCE_DIR}/src/pitch.cc
${PROJECT_SOURCE_DIR}/src/onset.cc)

include_directories(src)
include_directories(aubio/src)
Expand Down
3 changes: 3 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
<li>
<a href="tempo.html">Beat tracking</a>
</li>
<li>
<a href="onset.html">Onset detection</a>
</li>
</ul>
58 changes: 58 additions & 0 deletions examples/onset.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>Aubio Onset</title>
</head>
<body>
<div class="attacks">
<div id="last-attack">0</div>
</div>
<audio
src="http://mr1.doubanio.com/e586b3b26b25c3b6b1590061a462f2e0/0/fm/song/p291930_128k.mp4"
crossorigin="anonymous"
controls
></audio>
<script type='module' src="../build/aubio.js"></script>
<script type='module'>
import aubio from '../build/aubio.js';

let audioContext, scriptProcessor, audioSource;
const audio = document.querySelector("audio");
audio.addEventListener("play", run);

function run() {
if (audioContext) return;

audioContext = new (AudioContext || webkitAudioContext)();
scriptProcessor = audioContext.createScriptProcessor(512, 1, 1);
scriptProcessor.connect(audioContext.destination);
aubio().then((e) => {
const onset = new e.Onset(
"default",
scriptProcessor.bufferSize,
scriptProcessor.bufferSize / 8,
audioContext.sampleRate
);
scriptProcessor.addEventListener("audioprocess", function (event) {
if (onset.do(event.inputBuffer.getChannelData(0))) {
console.log("threshold", onset.getThreshold());
attack(onset.getLast());
}
});
});

audioSource =
audioSource || audioContext.createMediaElementSource(audio);
audioSource.connect(scriptProcessor);
audioSource.connect(audioContext.destination);

const $attack = document.querySelector("#last-attack");

function attack(time) {
$attack.innerHTML = parseFloat(time);
}
}
</script>
</body>
</html>
138 changes: 138 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,133 @@ declare class Tempo {
getConfidence(): number;
}

declare class Onset {
/**
* execute onset detection
*/
do(buffer: InputBuffer): number;

/**
* get the time of the latest onset detected, in samples
*/
getLast(): number;

/**
* get the time of the latest onset detected, in seconds
*/
getLastS(): number;

/**
* get the time of the latest onset detected, in milliseconds
*/
getLastMs(): number;

/**
* set onset detection adaptive whitening
*/

setAwhitening(enable: boolean):number;
/**
* get onset detection adaptive whitening
*/
getAwhitening():number;

/**
* set or disable log compression
*/
setCompression(enable: boolean):number;

/**
* get onset detection log compression
*/
getCompression(): number;

/**
* set onset detection silence threshold
*/
setSilence(silence: number):number;

/**
* get onset detection silence threshold
*/
getSilence(): number;

/**
* get onset detection function
*/
getDescriptor(): number;

/**
* get thresholded onset detection function
*/
getThresholdedDescriptor(): number;

/**
* set onset detection peak picking threshold
*/
setThreshold(threshold: number):number;

/**
* get onset peak picking threshold
*/
getThreshold(): number;

/**
* set minimum inter onset interval in samples
*/
getMinioi(): number;

/**
* set minimum inter onset interval in seconds
*/
getMinioiS(): number;

/**
* set minimum inter onset interval in milliseconds
*/
getMinioiMs(): number;

/**
* set delay in samples
*/
setDelay(delay: number): number;

/**
* set delay in seconds
*/
setDelayS(delay: number): number;

/**
* set delay in milliseconds
*/
setDelayMs(delay: number): number;

/**
* get delay in samples
*/
getDelay(): number;

/**
* get delay in seconds
*/
getDelayS(): number;

/**
* get delay in milliseconds
*/
getDelayMs(): number;

/**
* set default parameters
*/
setDefaultParameters(): number;

/**
* reset onset detection
*/
reset(): void;
}

declare type Aubio = {
Pitch: {
/**
Expand Down Expand Up @@ -124,6 +251,17 @@ declare type Aubio = {
*/
new (bufferSize: number, hopSize: number, sampleRate: number): Tempo;
};
Onset: {
/**
* Onset detection
*
* @param method - pitch detection algorithm
* @param bufferSize - length of FFT
* @param hopSize - number of frames between two consecutive runs
* @param sampleRate - sampling rate of the signal to analyze
*/
new (bufferSize: number, hopSize: number, sampleRate: number): Onset;
};
};

/**
Expand Down
Loading

0 comments on commit 9db17d0

Please sign in to comment.