加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP实现基于文本的莫斯电码生成器

发布时间:2016-10-17 09:33:17 所属栏目:PHP教程 来源:站长网
导读:副标题#e# 介绍 我最近遇到一个基于输入文本生成摩斯代码音频文件的需求。几番搜索无果之后,我决定自己编写一个生成器。 下载源代码 2.63 KB 498)this.width=498;' onmousewheel = 'javascript:return big(this)' height="417" width="404" alt="codegen_s

同时,在生成声音方面我们还要考虑另外一个问题。一般来讲,我们是通过正玄波的开关来生成莫斯代码。但是你直接这样来做的话,就会发现你生成的信号会占用非常大的带宽。所以,通常无线电设备会对其加以修正,以减少带宽占用。

在我们的项目中,也会做这样的修正,只不过是用数字的方式。既然我们已经知道了一个最小声音样本“嘀”的时间长度,那么,可以证明,最小带宽的声幅 发生在长度等于“嘀”的正玄波半周期。事实上,我们使用低通滤波器(low pass filter)来过滤音频信号也能达到同样的效果。不过,既然我们已经知道所有的信号字符,我们直接简单的过滤一下每一个字符信号就可以了。

生成“嘀”、“嗒”和空白信号的PHP代码就像下面这样:

  1. while ($dt < $DitTime) { 
  2.   $x = Osc(); 
  3.   if ($dt < (0.5*$DitTime)) { 
  4.     // Generate the rising part of a dit and dah up to half the dit-time 
  5.     $x = $x*sin((M_PI/2.0)*$dt/(0.5*$DitTime)); 
  6.     $ditstr .= chr(floor(120*$x+128)); 
  7.     $dahstr .= chr(floor(120*$x+128)); 
  8.     } 
  9.   else if ($dt > (0.5*$DitTime)) { 
  10.     // For a dah, the second part of the dit-time is constant amplitude 
  11.     $dahstr .= chr(floor(120*$x+128)); 
  12.     // For a dit, the second half decays with a sine shape 
  13.     $x = $x*sin((M_PI/2.0)*($DitTime-$dt)/(0.5*$DitTime)); 
  14.     $ditstr .= chr(floor(120*$x+128)); 
  15.     } 
  16.   else { 
  17.     $ditstr .= chr(floor(120*$x+128)); 
  18.     $dahstr .= chr(floor(120*$x+128)); 
  19.     } 
  20.   // a space has an amplitude of 0 shifted to 128 
  21.   $spcstr .= chr(128); 
  22.   $dt += $sampleDT; 
  23.   } 
  24. // At this point the dit sound has been generated 
  25. // For another dit-time unit the dah sound has a constant amplitude 
  26. $dt = 0; 
  27. while ($dt < $DitTime) { 
  28.   $x = Osc(); 
  29.   $dahstr .= chr(floor(120*$x+128)); 
  30.   $dt += $sampleDT; 
  31.   } 
  32. // Finally during the 3rd dit-time, the dah sound must be completed 
  33. // and decay during the final half dit-time 
  34. $dt = 0; 
  35. while ($dt < $DitTime) { 
  36.   $x = Osc(); 
  37.   if ($dt > (0.5*$DitTime)) { 
  38.     $x = $x*sin((M_PI/2.0)*($DitTime-$dt)/(0.5*$DitTime)); 
  39.     $dahstr .= chr(floor(120*$x+128)); 
  40.     } 
  41.   else { 
  42.     $dahstr .= chr(floor(120*$x+128)); 
  43.     } 
  44.   $dt += $sampleDT; 
  45.   } 

WAVE格式的文件

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读