jsfx生成一首简单的音乐效果代码

代码语言:html

所属分类:多媒体

代码描述:jsfx生成一首简单的音乐效果代码

代码标签: 简单 音乐 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum=1.0,minimum=1.0,user-scalable=0" />

</head>
<body>
    <div>
        听听声音
    </div>
    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jsfx.js"></script>
<script>
    (function(jsmusic){
	var scale = "c d ef g ha bC D EF G HA B ";

	jsmusic.Simple = Simple;
	function Simple(songtext, instrument, bpm, modules){
		var tokens = songtext.split("");
		var notes = [];
		for(var i = 0; i < tokens.length; i+=1){
			var note = scale.indexOf(tokens[i]) + 3;

			var more = true;
			while(more){
				switch(tokens[i+1]){
					case "+": i++; note +=  1; break;
					case "-": i++; note -=  1; break;
					case "^": i++; note += 12; break;
					case ".": i++; note -= 12; break;
					default:
						more = false;
				}
			}
			var freq = 220 * Math.pow(2, 1 + note/12);
			notes.push({Frequency: { Start: freq }});
		}
		return GenerateSong(notes, instrument, bpm, modules);
	};


	function GenerateSong(notes, instrument, bpm, modules){
		bpm = bpm || 120;
		var processor = new jsfx.Processor(instrument, modules);
		var sampleRate = processor.state.SampleRate;

		var beatSamples = sampleRate / (bpm / 60);
		var songSamples = notes.length * beatSamples;

		var beatBuffer = jsfx._createFloatArray(beatSamples);
		var playing = [];

		var songBuffer = jsfx._createFloatArray(songSamples + processor.getSamplesLeft());
		var currentStart = 0;

		// fill the beats
		for(var i = 0; i < notes.length; i += 1){
			var note = notes[i];
			var params = MergeParams(instrument, note);
			var proc = new jsfx.Processor(params, modules);
			playing.push(proc);

			playing.map(function(proc){
				proc.generate(beatBuffer);
			.........完整代码请登录后点击上方下载按钮下载查看

网友评论0