{"size": 2249, "ext": "js", "max_stars_repo_path": "src/bufferloader.js", "max_stars_repo_name": "rithik25/KRM_IT_Club", "max_stars_repo_stars_event_min_datetime": "2017-07-17T11:03:27.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-10T10:37:57.000Z", "max_issues_repo_path": "src/bufferloader.js", "max_issues_repo_name": "rithik25/KRM_IT_Club", "max_issues_repo_issues_event_min_datetime": "2017-07-17T11:07:26.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-22T08:57:01.000Z", "max_forks_repo_path": "src/bufferloader.js", "max_forks_repo_name": "rithik25/KRM_IT_Club", "max_forks_repo_forks_event_min_datetime": "2017-07-17T20:05:20.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-18T13:56:18.000Z"}
{"max_stars_count": 143, "max_issues_count": 10, "max_forks_count": 45, "avg_line_length": 28.1125, "max_line_length": 81, "alphanum_fraction": 0.668741663}
/**
 * Buffer Loader.js
 *
 * A small abstraction around the HTML5 buffer loader interface
 * https://www.behance.net/dominikhofacker
 * Please consider supporting this project on behance:
 * https://www.behance.net/gallery/49260123/Web-Audio-Visualization
 *
 */
 var reader;

window.AudioContext = window.AudioContext || 
                      window.webkitAudioContext;

var BufferLoader = function(sources) {
    this.buffers = { };
    this.context = null;
    this.buffer = null;
    this.init();
};

BufferLoader.prototype.init = function() {
  try {
    window.AudioContext = window.AudioContext || window.webkitAudioContext;
    this.context = new AudioContext();
  }
  catch(_) {
    alert('Web Audio API is not supported in this browser');
  }
};

BufferLoader.prototype.onBufferLoadError = function(_) {
    console.error('Error loading buffer');
};

BufferLoader.prototype.onBufferLoad = function(bufferName, srcBuffer, callback) {
    this.context.decodeAudioData(srcBuffer, function onSuccess(buffer) {
        this.buffers[bufferName] = buffer;
        if (typeof callback === 'function') {			
			callback(); // Aufruf der Wiedergabefunktion
        }
    }.bind(this), this.onBufferError);
};

BufferLoader.prototype.load = function(bufferName, file, callback) {	
	reader = new FileReader();
	reader.onload = function(data) {
        if(data.target && data.target.result) {
            this.onBufferLoad(bufferName, data.target.result, callback);
        }else{
            console.dir(data);
        }
	}.bind(this);
	reader.readAsArrayBuffer(file);
};

BufferLoader.prototype._playBuffer = function(name, gain, time) {
    var source = this.context.createBufferSource();
    source.buffer = this.buffer;
    
    var analyser = this.context.createAnalyser();
	
	
	
    source.connect(analyser);
    source.connect(this.context.destination);
    source.start(time);
};

BufferLoader.prototype.play = function (name, gain, time) {
    // Default values for time and gain
    gain = typeof gain !== 'undefined' ? gain : 1;
    time = typeof time !== 'undefined' ? time : 0;

    this.buffer = this.buffers[name];
    if (this.buffer) { this._playBuffer(name, time, gain); }
    else { throw new Error("Buffer does not exist"); }
};

