听音训练的JS和Python代码

(希望有其他人建设出来供大家免费试用的听音训练网站)
通过两种语言实现了,第一种是Python。第二种是JavaScript,可以兼容很多设备。代码中不会包含使用到的音频文件,仅供参考。

1、Python 代码块

import random,time
import winsound
notewrite=["Dol","Re","Mi","Fa","Sol","La","Ti","Dol#"]
while 1:
    winsound.PlaySound("beginning.wav",winsound.SND_FILENAME)
    note=random.randrange(1,8)
    winsound.PlaySound(str(note) + ".wav",winsound.SND_FILENAME)
    key=input("键入你最后听到的音(数字1-8),回车确认")
    print(str(note) ," ", notewrite[note-1] ," ",key==str(note))
    winsound.PlaySound(str(note) + ".wav",winsound.SND_FILENAME)
    time.sleep(2)

2、JavaScript 代码块

<!DOCTYPE html>
<html>
<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>听音训练</title> 
</head>
<body>
<button type="button" onclick="clk_start()" style = "height:100px;width:80%;font-size:20px;" id = "btn_start" > 开始测试 </button><br>
<button type="button" onclick="choose1()" disable=true style="height:40px;left:10%;width:80%;font-size:20px;" id="1 Dol" > 1 Dol </button><br>
<button type="button" onclick="choose2()" disable=true style="height:40px;left:10%;width:80%;font-size:20px;" id="2 Re" > 2 Re </button><br>
<button type="button" onclick="choose3()" disable=true style="height:40px;left:10%;width:80%;font-size:20px;" id="3 Mi" > 3 Mi </button><br>
<button type="button" onclick="choose4()" disable=true style="height:40px;left:10%;width:80%;font-size:20px;" id="4 Fa" > 4 Fa </button><br>
<button type="button" onclick="choose5()" disable=true style="height:40px;left:10%;width:80%;font-size:20px;" id="5 Sol" > 5 Sol </button><br>
<button type="button" onclick="choose6()" disable=true style="height:40px;left:10%;width:80%;font-size:20px;" id="6 La" > 6 La </button><br>
<button type="button" onclick="choose7()" disable=true style="height:40px;left:10%;width:80%;font-size:20px;" id="7 Ti" > 7 Ti </button><br>
<button type="button" onclick="choose8()" disable=true style="height:40px;left:10%;width:80%;font-size:20px;" id="8 Dol" > 8 Dol </button><br>


<audio src="beginning.wav" onended="sing_note()" id=start_player>
<audio id=note_player>
<script>
	var stper=document.getElementById("start_player");
	var ntper=document.getElementById("note_player");
	var note=0;
	function clk_start(){ document.getElementById("btn_start").innerHTML="音频播放中...";stper.play();note=Math.trunc((Math.random()*100)%8+1);ntper.src=note+".wav";}
	
	function sing_note(){
		ntper.play();
		document.getElementById("btn_start").innerHTML="请选择..."
	}
	
function choose1(){if(note==1){ntper.play();document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    True                 AGAIN?";}else{document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    False                  AGAIN?"}};
function choose2(){if(note==2){ntper.play();document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    True                 AGAIN?";}else{document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    False                  AGAIN?"}};
function choose3(){if(note==3){ntper.play();document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    True                 AGAIN?";}else{document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    False                  AGAIN?"}};
function choose4(){if(note==4){ntper.play();document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    True                 AGAIN?";}else{document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    False                  AGAIN?"}};
function choose5(){if(note==5){ntper.play();document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    True                 AGAIN?";}else{document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    False                  AGAIN?"}};
function choose6(){if(note==6){ntper.play();document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    True                 AGAIN?";}else{document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    False                  AGAIN?"}};
function choose7(){if(note==7){ntper.play();document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    True                 AGAIN?";}else{document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    False                  AGAIN?"}};
function choose8(){if(note==8){ntper.play();document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    True                 AGAIN?";}else{document.getElementById("btn_start").innerHTML=note+"   "+singname(note)+"    False                  AGAIN?"}};

	
	function singname(i){
	switch(i)
{
    case 1:
        return("Dol");
        break;
    case 2:
        return("Re");
        break;
    case 3:
        return("Mi");
        break;
    case 4:
        return("Fa");
        break;
    case 5:
        return("Sol");
        break;
    case 6:
        return("La");
        break;
    case 7:
        return("Ti");
        break;
    case 8:
        return("Dol");
        break;
}
}
</script>
</body>
</html>

3、跨三度的大小调混合听音训练JavaScript代码块

<!DOCTYPE html>
<html>
<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<title>听音训练(三度12音)</title> 
</head>
<body>

    <button type="button" onclick="clk_start()" style="height:100px;width:80%;font-size:20px;" id="btn_start"> 开始测试 </button><br><br>
    <p>=========================================================================================================</p><br />
<p> C3   
<button type="button" onclick="choose301()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 1 Dol </button>
 <button type="button" onclick="choose302()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 1# Dol# </button>
<button type="button" onclick="choose303()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 2 Re </button>
<button type="button" onclick="choose304()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 2# Re# </button>
<button type="button" onclick="choose305()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 3 Mi </button>
<button type="button" onclick="choose306()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 4 Fa </button>
<button type="button" onclick="choose307()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 4# Fa# </button>
<button type="button" onclick="choose308()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 5 Sol </button>
<button type="button" onclick="choose309()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 5# Sol# </button>
<button type="button" onclick="choose310()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 6 La </button>
<button type="button" onclick="choose311()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 6# La# </button>
<button type="button" onclick="choose312()" disable=true style="height:40px;left:10%;right:30%;font-size:20px;"> 7 Ti </button><br><br>
</p>
<p>
C4
<button type="button" onclick="choose401()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;"> 1 Dol </button>
<button type="button" onclick="choose402()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;"> 1# Dol# </button>
<button type="button" onclick="choose403()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;"> 2 Re </button>
<button type="button" onclick="choose404()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;"> 2# Re# </button>
<button type="button" onclick="choose405()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;" id="3 Mi"> 3 Mi </button>
<button type="button" onclick="choose406()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;" id="4 Fa"> 4 Fa </button>
<button type="button" onclick="choose407()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;" id="4# Fa#"> 4# Fa# </button>
<button type="button" onclick="choose408()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;" id="5 Sol"> 5 Sol </button>
<button type="button" onclick="choose409()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;" id="5# Sol#"> 5# Sol# </button>
<button type="button" onclick="choose410()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;" id="6 La"> 6 La </button>
<button type="button" onclick="choose411()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;" id="6# La#"> 6# La# </button>
<button type="button" onclick="choose412()" disable=true style="height:40px;left:31%;right:61%;font-size:20px;" id="7 Ti"> 7 Ti </button><br><br>
</p>
<p>
C5
<button type="button" onclick="choose501()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;" id="1 Dol"> 1 Dol </button>
<button type="button" onclick="choose502()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;" id="1# Dol#"> 1# Dol# </button>
<button type="button" onclick="choose503()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;" id="2 Re"> 2 Re </button>
<button type="button" onclick="choose504()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;" id="2# Re#"> 2# Re# </button>
<button type="button" onclick="choose505()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;"> 3 Mi </button>
<button type="button" onclick="choose506()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;"> 4 Fa </button>
<button type="button" onclick="choose507()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;"> 4# Fa# </button>
<button type="button" onclick="choose508()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;"> 5 Sol </button>
<button type="button" onclick="choose509()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;"> 5# Sol# </button>
<button type="button" onclick="choose510()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;"> 6 La </button>
<button type="button" onclick="choose511()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;"> 6# La# </button>
<button type="button" onclick="choose512()" disable=true style="height:40px;left:62%;right:90%;font-size:20px;"> 7 Ti </button>
</p>

    <audio src="beginning.wav" onended="sing_note()" id=start_player />
    <audio id=note_player />
    <script>
        var stper = document.getElementById("start_player");
        var ntper = document.getElementById("note_player");
        var note = 0;
        var i = 0;
        function clk_start() {
            document.getElementById("btn_start").innerHTML = "音频播放中...";
            stper.play();
            i = Math.trunc((Math.random() * 100) % 12 + 1);
            note = 100 * Math.trunc((Math.random() * 100) % 3 + 3) + i;
            ntper.src = note + ".wav";
        }

        function sing_note() {
            ntper.play();
            document.getElementById("btn_start").innerHTML = "请选择..."
        }
        function choose301() { ntper.play(); if (note == 301) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose302() { ntper.play(); if (note == 302) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose303() { ntper.play(); if (note == 303) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose304() { ntper.play(); if (note == 304) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose305() { ntper.play(); if (note == 305) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose306() { ntper.play(); if (note == 306) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose307() { ntper.play(); if (note == 307) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose308() { ntper.play(); if (note == 308) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose309() { ntper.play(); if (note == 309) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose310() { ntper.play(); if (note == 310) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose311() { ntper.play(); if (note == 311) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose312() { ntper.play(); if (note == 312) { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C3-" + i + "   " + singname(i) + "                            False                               Again?" } };

        function choose401() { ntper.play(); if (note == 401) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose402() { ntper.play(); if (note == 402) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose403() { ntper.play(); if (note == 403) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose404() { ntper.play(); if (note == 404) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose405() { ntper.play(); if (note == 405) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose406() { ntper.play(); if (note == 406) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose407() { ntper.play(); if (note == 407) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose408() { ntper.play(); if (note == 408) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose409() { ntper.play(); if (note == 409) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose410() { ntper.play(); if (note == 410) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose411() { ntper.play(); if (note == 411) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose412() { ntper.play(); if (note == 412) { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C4-" + i + "   " + singname(i) + "                            False                               Again?" } };

        function choose501() { ntper.play(); if (note == 501) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose502() { ntper.play(); if (note == 502) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose503() { ntper.play(); if (note == 503) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose504() { ntper.play(); if (note == 504) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose505() { ntper.play(); if (note == 505) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose506() { ntper.play(); if (note == 506) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose507() { ntper.play(); if (note == 507) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose508() { ntper.play(); if (note == 508) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose509() { ntper.play(); if (note == 509) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose510() { ntper.play(); if (note == 510) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose511() { ntper.play(); if (note == 511) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };
        function choose512() { ntper.play(); if (note == 512) { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                             True                               Again?"; } else { document.getElementById("btn_start").innerHTML = "C5-" + i + "   " + singname(i) + "                            False                               Again?" } };

        function singname(i) {
            switch (i) {
                case 1:
                    return ("Dol");
                    break;
                case 2:
                    return ("Dol#");
                    break;
                case 3:
                    return ("Re");
                    break;
                case 4:
                    return ("Re#");
                    break;
                case 5:
                    return ("Mi");
                    break;
                case 6:
                    return ("Fa");
                    break;
                case 7:
                    return ("Fa#");
                    break;
                case 8:
                    return ("Sol");
                    break;
                case 9:
                    return ("Sol#");
                    break;
                case 10:
                    return ("La");
                    break;
                case 11:
                    return ("La#");
                    break;
                case 12:
                    return ("Ti");
                    break;
            }
        }


    </script>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值