2016-11-23 12 views
9

wavesurfer.jsは、オーディオファイルから波形をレンダリングするのに最適ですが、生成される波形/スペクトログラフの振幅/周波数にCSSプロパティをアニメーション化することが可能かどうかは疑問ですによってwaveurfer.js?別のパラメータに割り当てることのできる変数の種類がありますか(たとえば、不透明度が<img>)?wavesurfer.jsを使用してCSSプロパティをアニメーション化する

答えて

7

wavesurfer.jsを見るとAnalyserNodewavesurfer.backend.analyserで取得できます。

注:analyserAnalyserNodeのインスタンスであることを確認する必要があります。ブラウザがWeb Audio APIをサポートしている場合のみです。

AnalyserNodeからAnalyserNode.frequencyBinCountプロパティを取得すると、creating a visualisation/animationを開始できます。

自分のサイトのwavesurfer.js examplesに簡単な例(codepen)を作成しました。

var wavesurfer = WaveSurfer.create({ 
 
    container: '#waveform', 
 
    waveColor: '#5B88C8', 
 
    progressColor: '#264E73' 
 
}); 
 
wavesurfer.load('https://ia902606.us.archive.org/35/items/shortpoetry_047_librivox/song_cjrg_teasdale_64kb.mp3'); 
 

 
//get the AnalyserNode from wavesurfer 
 
//@see https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode 
 
var analyser = wavesurfer.backend.analyser, 
 
    //array to store the frequency data 
 
    //@see https://developer.mozilla.org/en-US/docs/Web/API/AnalyserNode/getByteFrequencyData 
 
    frequencyData = new Uint8Array(analyser.frequencyBinCount), 
 

 
    //div to animate and play/pause button 
 
    box = document.getElementById('box'), 
 
    play = document.getElementById('play'); 
 

 
//play button - play pause audio 
 
play.addEventListener('click', function() { 
 
    wavesurfer.playPause(); 
 
}); 
 

 
//wavesurfer 'audioprocess' event Fires continuously as the audio plays @see events on wave surfer http://wavesurfer-js.org/docs/events.html 
 
wavesurfer.on('audioprocess', function(e) { 
 

 
    analyser.getByteFrequencyData(frequencyData); 
 
    //console.log(frequencyData); 
 

 
    //simple example - get the first value in the array and set the width of the box 
 
    var w = frequencyData[0] * 0.05; 
 
    //apply a scale transform; 
 
    box.style.transform = 'scale(' + w + ',1)'; 
 

 
});
/* add some transition */ 
 

 
.animation { 
 
    margin: 50px auto; 
 
    width: 50px; 
 
    height: 50px; 
 
    background-color: #71C2D0; 
 
    transition: transform 0.1s ease-out; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/wavesurfer.js/1.2.3/wavesurfer.min.js"></script> 
 
<div id="waveform"></div> 
 
<div id="box" class="animation"></div> 
 
<button id="play">Play</button>

+0

本当にありがとうございました!私はまだ詳細を理解していませんが、画像の不透明度をアニメーション化するためにコードを修正しましたが、CSS3のフィルタプロパティをアニメーション化する際に問題があります。 – Roland

+0

他のCSSプロパティと同様に、フィルタをアニメーション化することができます。遷移:フィルタ0.5s線形;もしあなたがjavascriptを使ってそれをやりたいのなら、これは役に立つかもしれません。http://stackoverflow.com/questions/20082283/animate-css-blur-filter-in-jquery – r8n5n

+0

誰かが見ていると、詩はサラの「歌」ですTeasdale](http://www.bartleby.com/273/50.html)、[この特定の録音はLibriVoxのものです](https://librivox.org/short-poetry-collection-047/)を参照してください。 – approxiblue

関連する問題