Skip to content

Commit

Permalink
Spectrograms with wavesurfer
Browse files Browse the repository at this point in the history
  • Loading branch information
kahst committed Dec 31, 2019
1 parent cd42b5b commit 1144702
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ This app also needs some additional packages that we have to install.
npm install audio-loader
npm install audio-resampler
npm install array-normalize
npm install wavesurfer.js
```
10 changes: 9 additions & 1 deletion css/style.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
html, body {
height: 100%;
}

.border-10 {
border-width:10px !important;
}
}

.h-33 {height: 33%;}
.h-40 {height: 40%;}
.h-85 {height: 85%;}
24 changes: 23 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<script>require('popper.js');</script>
<script>require('bootstrap');</script>

<!-- Additional JavaScript -->
<!-- Additional JavaScript & CSS -->
<link rel="stylesheet" href="css/style.css" />
<script src="js/birdnet.js"></script>
<script src="js/ui.js"></script>

Expand Down Expand Up @@ -44,4 +45,25 @@
</ul>
</div>
</nav>

<!-- Load audio file hint -->
<div class="d-flex h-85 justify-content-center align-items-center" id="loadFileHint">
<p class="lead justify-content-center" id="loadFileHintText">Load an audio file for analysis by clicking</br>on <i>File > Open audio file</i> in the top menu.</p>
<div class="d-none spinner-border text-secondary" role="status" id="loadFileHintSpinner"></div>
</div>

<!-- Waveform view -->
<div class="" id="waveformContainer"></div>

<!-- Spectrogram view -->
<div class="d-none h-33" id="specContainer"></div>

<!-- Controls view -->
<div class="container-fluid bg-dark text-white d-none" id="controlsWrapper">
<div class="row p-2">
<div class="col-3">
<button type="button" class="btn btn-primary" onclick="WAVESURFER.playPause();">Play/Pause</button>
</div>
</div>
</div>
</body>
62 changes: 61 additions & 1 deletion js/birdnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const tf = require('@tensorflow/tfjs');
const load = require('audio-loader')
const resampler = require('audio-resampler');
const normalize = require('array-normalize')
const WaveSurfer = require('wavesurfer.js');
const SpectrogramPlugin = require('wavesurfer.js/dist/plugin/wavesurfer.spectrogram.min.js');

const CONFIG = {

Expand All @@ -11,6 +13,8 @@ const CONFIG = {
}

var AUDIO_DATA = [];
var WAVESURFER = null;
var CURRENT_ADUIO_BUFFER = null;

///////////////////////// DO AFTER LOAD ////////////////////////////
window.onload = function () {
Expand All @@ -21,6 +25,12 @@ var AUDIO_DATA = [];

function loadAudioFile(filePath) {

// Hide load hint and show spinnner
hideAll();
showElement('loadFileHint');
showElement('loadFileHintSpinner');


// load one file
load(filePath).then(function (buffer) {

Expand All @@ -33,10 +43,60 @@ function loadAudioFile(filePath) {
// Normalize audio data
AUDIO_DATA = normalize(AUDIO_DATA)

console.log(AUDIO_DATA);
//console.log(AUDIO_DATA);

//Hide center div when done
hideElement('loadFileHint');

// Draw and show spectrogram
drawSpectrogram(buffer);

});

});

}

function drawSpectrogram(audioBuffer) {

// Set global buffer
CURRENT_ADUIO_BUFFER = audioBuffer;

// Show waveform container
showElement('waveformContainer');

// Setup waveform and spec views
var options = {
container: '#waveformContainer',
plugins: [
SpectrogramPlugin.create({
container: '#specContainer',
fftSamples: 1024,
pixelRatio: 1,
labels: false,
name: 'specCanvas'
})
]
};

// Create wavesurfer object
WAVESURFER = WaveSurfer.create(options);

// Load audio file
WAVESURFER.loadDecodedBuffer(CURRENT_ADUIO_BUFFER);

// Hide waveform view for now
hideElement('waveformContainer');
showElement('specContainer');

// Resize canvas of spec and labels
$('canvas').each(function() {
$( this ).height($('#specContainer').height());
});

// Show controls
showElement('controlsWrapper');

}


36 changes: 35 additions & 1 deletion js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,38 @@ function exitApplication() {

remote.getCurrentWindow().close()

}
}

function showElement(id) {

$('#' + id).removeClass('d-none');
$('#' + id).addClass('d-flex');

}

function hideElement(id) {

$('#' + id).removeClass('d-flex');
$('#' + id).addClass('d-none');

}

function hideAll() {

// File hint div
hideElement('loadFileHint');
hideElement('loadFileHintText');
hideElement('loadFileHintSpinner');

// Waveform and spec
hideElement('waveformContainer');
hideElement('specContainer');

// Controls
hideElement('controlsWrapper');

}


// Event listener
//window.addEventListener('resize', drawSpectrogram(CURRENT_ADUIO_BUFFER), false);
3 changes: 3 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ function createWindow () {
// Set icon
win.setIcon(__dirname + '/img/icon/icon.png');

// Always maximize
win.maximize()

// Hide nav bar
win.setMenuBarVisibility(false);

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"audio-resampler": "^1.0.1",
"bootstrap": "^4.4.1",
"jquery": "^3.4.1",
"popper.js": "^1.16.0"
"popper.js": "^1.16.0",
"wavesurfer.js": "^3.3.0"
}
}

0 comments on commit 1144702

Please sign in to comment.