PHP implementation of text-based Moz code generator _ PHP Tutorial

Source: Internet
Author: User
PHP implements a text-based Moz code generator. PHP implements a text-based Moz code generator to introduce a demand for generating Moz code audio files based on input text. After several unsuccessful searches, I decided to write PHP to implement a text-based Moz code generator.

Introduction

I recently met a requirement to generate an audio file of the Moss code based on the input text. After several unsuccessful searches, I decided to write a generator myself.

Download source code-2.63 KB

I decided to use PHP as my main programming language because I wanted to access my Moss code audio files through the web. The above shows a web page that begins to generate Moss code. The downloaded zip file contains a webpage for submitting text and a PHP source file for generating and displaying audio files. If you want to test the PHP code, you need to rewrite the webpage and related PHP files to the server with PHP enabled.

For many people, Mosi code is like a sequence of "points" and "horizontal lines", or a series of beeps, as shown in some old movies. Obviously, this is far from enough understanding if you want to use computer code to generate Moz code. This article will introduce the elements of the Moz code generation, how to generate audio files in the WAVE format, and how to use PHP to convert Moz code into audio files.

Moz code

Moz code is a text encoding method. It has the advantage of convenient encoding and can be easily decoded by human ears. In essence, it is through the Enable and off of audio or radio frequency to form a short or long audio pulse, generally called dot) and line dash ), or, in radio terms, it is called "radio" and "tick ". In terms of modern digital communication, the Mosi code is an amplitude keying amplitude shift keying, ASK ).

In Moss code, character letters, numbers, punctuation marks, and special characters are encoded into a sequence of "tick" and "tick. Therefore, to convert the text into Moz code, we must first determine how to display the "cursor" and "tick ". An obvious choice is to use 0 to represent "tick", 1 to represent "tick", or vice versa. Unfortunately, the MOS code uses a variable-length encoding scheme. Therefore, we must also use a variable-length sequence, or use a method to package data into a general fixed-bit-width fixed bit-size format for computer memory. In addition, you must note that the MOS code is case-insensitive and cannot be encoded for some special characters. In our implementation, undefined characters and symbols will be ignored.

In this project, memory usage is not a special issue. Therefore, we propose a simple encoding scheme, that is, "0" is used to represent each "tick" and "1" is used to represent each "tick ", and put them in a string join array. The PHP code that defines the MOS code encoding table is as follows:

 
 
  1. $CWCODE = array ('A'=>'01','B'=>'1000','C'=>'1010','D'=>'100','E'=>'0',
  2. 'F'=>'0010','G'=>'110','H'=>'0000','I'=>'00','J'=>'0111',
  3. 'K'=>'101','L'=>'0100','M'=>'11','N'=>'10', 'O'=>'111',
  4. 'P'=>'0110','Q'=>'1101','R'=>'010','S'=>'000','T'=>'1',
  5. 'U'=>'001','V'=>'0001','W'=>'011','X'=>'1001','Y'=>'1011',
  6. 'Z'=>'1100', '0'=>'11111','1'=>'01111','2'=>'00111',
  7. '3'=>'00011','4'=>'00001','5'=>'00000','6'=>'10000',
  8. '7'=>'11000','8'=>'11100','9'=>'11110','.'=>'010101',
  9. ','=>'110011','/'=>'10010','-'=>'10001','~'=>'01010',
  10. '?'=>'001100','@'=>'00101');

Note that if you are particularly concerned about memory usage, the above code can be interpreted as bit ). Add a starting bit for each code to form a single-bit mode. each character can be stored in one byte. At the same time, when parsing the final encoding, you need to delete the bit on the left of the start bit) to get the real variable-length encoding.

Although many people do not realize that "time interval" is the main factor in defining moms code, understanding this is the key to generating moms code. Therefore, the first thing we need to do is to define the interval between the internal codes of the Moss code, that is, "tick" and "tick. For the sake of convenience, we define a "beep" sound length as a time unit dt, and the interval between "tick" and "tick" is also a time unit dt; define a "tick" with a length of 3 dt characters, and the interval between letters is also 3 dt; the interval between words is 7 dt. So, in summary, our time interval table is like the following:

Project

Time length

Bytes

Dt

"Interval"/"interval"

Dt

Click"

3 * dt

Interval between characters

3 * dt

Interval between words

7 * dt

In the MOS code, the "playback speed" of the encoding sound is usually expressed by the number of words/minute (WPM. Because English words have different lengths and different numbers of characters, the conversion from WPM to audio is not as easy as it looks. In an international group, five characters are used as the average length of a word, and a number or punctuation character is used as two characters. In this way, an average word is 50 time units (dt. In this way, if you specify WPM, the total playback time is 50 * WPM time unit/minute, and each "period" is a time unit dt) the length is equal to 1.2/WPM seconds. In this way, the length of time for a "period" is given, and the length of time for other elements can be easily calculated.

You may have noticed that in the page shown above, we use "Farnsworth spacing" for options lower than 15WPM ". So what is this "Farnsworth spacing?

When the reporter learns to use his ears to decode moms, he will realize that when the playback speed changes, the rhythm of the characters will also change. When the playback speed is lower than 10WPM, he can easily identify "tick" and "tick" and know which character to send. However, when the playback speed exceeds 10 WPM, the operator's identification will fail, and the number of characters recognized by the operator will be greater than the actual number of "clicks" and "clicks ". When a person who is used to low-speed moms code is learning, problems may occur when handling high-speed playback code. Due to the changing pace, his subconscious recognition will go wrong.

To solve this problem, Farnsworth spacing was invented. In essence, the playback speed of letters and symbols is still higher than that of 15WPM. at the same time, more spaces are inserted between characters to reduce the overall playback speed. In this way, the reporter can recognize each character at a reasonable speed and rhythm. once all the characters are learned, the speed can be increased, the receiver only needs to speed up character recognition. In essence, the Farnsworth spacing technique solves the rhythm change problem and enables the recipient to learn quickly.

Therefore, for lower playback speeds, the system generates a 15WPM. Correspondingly, a period is 0.08 seconds, but the interval between characters and words is no longer three dit or seven dit, instead, it is adjusted to adapt to the overall speed.

Generate sound

In PHP code, a character is the index of the preceding array. it represents a group of MOS sounds consisting of "lag", "tick", and blank spaces. We use digital sampling to form an audio sequence, write it into a file, and add appropriate header information to define it as a WAVE format.

The code for generating sound is actually quite simple. you can find them in the PHP file of the project. I found it quite convenient to define a "digital oscillator. Every time an osc () is called, it returns a scheduled sample generated from the Xuan-Bo. Using sound sampling and sound frequency specifications, it is sufficient to generate audio in the WAVE format. The-1 to + 1 in the produced positive and Xuan waves are moved and adjusted, so that the byte data of the sound can be expressed as 0 to 255, and 128 represents the zero amplitude.

At the same time, we need to consider another issue in generating sound. In general, we generate moms code through the switch of Zheng Xuanbo. However, if you do this directly, you will find that the signal you generate will occupy a very large bandwidth. Therefore, radio devices usually modify the bandwidth to reduce bandwidth usage.

In our project, we will also make such corrections, but only by using numbers. Now that we know the time length of the minimum sound sample "duration", it can be proved that the minimum bandwidth occurs in the half-cycle of the positive and negative waves whose length is equal to the "duration. In fact, we use the low pass filter (low pass filter) to filter audio signals. However, since we already know all the signal characters, we can simply filter each character signal.

The PHP code that generates "timer", "tick", and blank signals is like the following:

 
 
  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. }

Files in WAVE Format

WAVE is a common audio format. In the simplest form, a WAVE file contains an integer sequence in the header to indicate the audio amplitude at the specified sampling rate. For more information about the WAVE File, see Audio File Format Specifications website. For Moz code generation, we do not need to use all the parameter options in the WAVE Format. we only need an 8-bit single channel. so easy. It should be noted that multi-byte data must adopt the low-priority little-endian byte order. A wave file uses a RIFF format consisting of chunks.

A wave file starts with an ASCII identifier RIFF, followed by a 4-byte "block", followed by a header containing the ASCII character WAVE, and finally defines the format of data and sound data.

In our program, the first "block" contains a format specifier, which consists of the ASCII character fmt and a four-byte "block ". Here, because I use the plain vanilla PCM format for normal pulse encoding, each "block" is 16 bytes. Then, we also need the data: Number of channels, sound sampling/second, average byte/second, a block) alignment indicator, bit)/sound sampling. In addition, because we do not need high quality stereo sound, we only use single channel. We use the sampling rate of 11050 sampling/second standard CD quality audio, which is 44200 sampling/second, and saved in 8 bits.

Finally, the real audio data is stored in the next "block. It contains the ASCII character data, a 4-byte "block", and finally consists of the byte sequence because we use real audio data consisting of 8-bit (bit)/sampling.

In the program, the sound consisting of an 8-bit audio amplitude sequence is stored in the variable $ soundstr. Once the audio data is generated, all the "block" sizes can be calculated, and then they can be combined and written into the disk file. The following code shows how to generate the header information and the audio "block ". Note that $ riffstr indicates the RIFF header, $ fmtstr indicates the block format, and $ soundstr indicates the audio data block ".

 
 
  1. $riffstr = 'RIFF'.$NSizeStr.'WAVE';
  2. $x = SAMPLERATE;
  3. $SampRateStr = '';
  4. for ($i=0; $i<4; $i++) {
  5. $SampRateStr .= chr($x % 256);
  6. $x = floor($x/256);
  7. }
  8. $fmtstr = 'fmt '.chr(16).chr(0).chr(0).chr(0).chr(1).chr(0).chr(1).chr(0)
  9. .$SampRateStr.$SampRateStr.chr(1).chr(0).chr(8).chr(0);
  10. $x = $n;
  11. $NSampStr = '';
  12. for ($i=0; $i<4; $i++) {
  13. $NSampStr .= chr($x % 256);
  14. $x = floor($x/256);
  15. }
  16. $soundstr = 'data'.$NSampStr.$soundstr;

Summary and comments

Our text Moz code generator looks good now. Of course, we can also make many modifications and improvements to it, such as using other character sets, directly reading text from files, and generating compressed audio. Because our project aims to make it easy to use on the network, our simple solution has already achieved our goal.

Of course, as always, I hope you will give suggestions on these simple and crude code. Although someone has been teaching me over the years, I still lack background knowledge about Moz code. Therefore, it is my fault to make any mistakes or omissions.

Http://www.codeceo.com/article/php-morse-code-generation.html.
Original article: Morse Code Generation from Text

Introduction: I recently encountered a requirement to generate an audio file of the Moss code based on the input text. After several unsuccessful searches, I decided to write it myself...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.