ホーム > カテゴリ > HTML5・JavaScript >

Waveファイルの音声の一部を削除する [WAVE.js]

WAVE.jsを使用して「Waveファイル」(*.wav)の音声の一部を「削除」します。WAVE.jsはオープンソースなのでどなたでもご利用可能です。

WAVE.js

https://github.com/TakeshiOkamoto/WAVE.js

DEMO (Japanese)

https://www.petitmonte.com/labo/wave-delete/

ソースコード

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src ="WAVE.js"></script>
<script>

var wav;

function onDragOver(event){ 
  event.preventDefault(); 
} 
  
function onDrop(event){
  onAddFile(event);
  event.preventDefault(); 
}  

function run() {
 
  // HTML5 Validity Check
  if (!document.getElementById("num_st").checkValidity()){
    return true;
  }   

  if (!document.getElementById("num_end").checkValidity()){
    return true;
  }   

  if(!wav) return false;
    
  var st = parseInt(document.getElementById("num_st").value,10);
  var en = parseInt(document.getElementById("num_end").value,10);
  
  if(st == en){
    alert("The same time can not be set.")  ;
    return false;    
  } 
  
  if(st > en){
    alert("Please make start time shorter than end time.")  ;
    return false;    
  }   
  
console.time("run");     
      
  try{
    // Raw
    var data = wav.getData();
    
    // Sample per 1 ms (Sample/ms)
    var ms = Math.round(wav.Analyst.WaveFomat.nSamplesPerSec/1000);
    
    var tmpL = [], tmpR = [];      
    
    var ss = ms * st;
    var ee = ms * en;
    
    // L
    for(var i=0;i<data.L.length;i++){
      if(i>=ss && i <= ee){
        // delete
      }else{
        tmpL.push(data.L[i]); 
      }
    }
    
    // R
    if(data.R.length != 0){ 
      for(var i=0;i<data.R.length;i++){
        if(i>=ss && i <= ee){
          // delete
        }else{
          tmpR.push(data.R[i]); 
        }
      }
    }

    data.L = tmpL;
    data.R = tmpR;
    
    // Save
    var F = wav.WriteStream(wav.Analyst.WaveFomat.wBitsPerSample, data,
                            wav.Analyst.WaveFomat.nSamplesPerSec);
    F.SaveToFile("test.wav","audio/wav");                
    
     
  }catch(e){
    alert("Could not acquire waveform data. (unsupported format)");
    console.error(e);         
  }
  
console.timeEnd("run");
  
  return false;
}

function onAddFile(event) {
  var files;
  var reader = new FileReader();
  
  if(event.target.files){
    files = event.target.files;
  }else{ 
    files = event.dataTransfer.files;   
  }    
   
  reader.onload = function (event) {

    try{
      
      wav = new TWaveFormat(new Uint8Array(reader.result));
      
      var str = "File loaded : " + wav.Analyst.WaveFomat.wBitsPerSample + "bit ";
      str +=  wav.Analyst.WaveFomat.nSamplesPerSec + "Hz ";
     
      if(wav.Analyst.WaveFomat.nChannels == 1){
        str += " Mono ";
      }else{
        str += " Stereo ";
      }
      
      str += Math.round(wav.Analyst.time)/1000 + "s(" +
             wav.Analyst.time + "ms)";  
      
      // Max
      document.getElementById("num_st").max = Math.floor(wav.Analyst.time);
      document.getElementById("num_end").max = Math.floor(wav.Analyst.time);     
            
      document.getElementById("msg").innerHTML =str;           
     
    }catch(e){
      alert(e);
      console.error(e); 
    }    
  };
  
  if (files[0]){    
    reader.readAsArrayBuffer(files[0]); 
    document.getElementById("inputfile").value = "";
  }
}      
</script>
</head>
<body ondrop="onDrop(event);" ondragover="onDragOver(event);">  
<h1>Delete</h1>
<p></p>
<form> 
 <table>
   <tr><th>Wave File</th><td>&nbsp;<input type="file" id="inputfile" accept="audio/wav" onchange="onAddFile(event);"></td></tr>
 </table>    
 <p></p> 
 <table>
  <tr><th>&nbsp;Range&nbsp;</th>
      <td>&nbsp;From <input type="number" id="num_st" style="width:85px;text-align:right;" min="0" max="9999999" value ="" required> to 
                  <input type="number" id="num_end" style="width:85px;text-align:right;" min="0" max="9999999" value ="" required> ms<br>&nbsp;@1s = 1000ms</td></tr>      
 </table>   
 <p id="msg" style="font-size:12px;color:green;"> </p> 
 <input type="submit" onclick="return run();" value="R U N">
</form>
</body>
</html> 





関連記事



公開日:2019年03月05日
記事NO:02738