PHP implementation of the text-based Moss code generator _php Tutorial

Source: Internet
Author: User
Tags sin

PHP implementation of the text-based Moss code generator


Introduced

I recently encountered a requirement to generate Morse code audio files based on input text. After a few searches, I decided to write a generator myself.

Download source Codes –2.63 KB

Because I wanted to access my Morse code audio files through the web, I decided to use PHP as my main programming language. The above shows a webpage that starts to generate Moss code. In the downloaded zip file, contains the Web page for submitting the text and the PHP source file for generating and presenting the audio file. If you want to test the PHP code, you need to copy the Web page and the associated PHP file to a PHP-enabled server.

For many, the moss code, like some old movies, is a sequence of "dots" and "dashes", or a series of beeps. Obviously, if you want to use computer code to generate Moss code, this understanding is far from enough. This article will describe the elements that generate Moss code, how to generate audio files in wave format, and how to convert Moss code into audio files using PHP.

Moss Code

Moss Code is a text encoding method. It has the advantage of easy coding, and the ear can be easily decoded. In essence, by the opening and closing of the audio or radio frequency), resulting in a short or long audio pulse, commonly referred to as dot dot) and line dash), or in radio terminology called "Beep" and "click". In modern digital communications terminology, Moss Code is an amplitude keying amplitude shift keying, ASK).

In Moss code, character letters, numbers, punctuation marks, and special symbols are encoded into a sequence of "beep" and "tick". So in order to convert the text into Morse code, we first have to determine how to represent "tick" and "click". One obvious option is to use 0 for "beep", 1 for "click", or vice versa. Unfortunately, Moss code uses a variable-length encoding scheme. So we also have to use a variable-length sequence, or a way to package the data into a fixed-bit-width-bit-size that is common to computer memory. In addition, it is important to note that Moss code does not differentiate between alphabetic case and some special symbols that cannot be encoded. In our implementation, undefined characters and symbols are ignored.

In this project, memory footprint is not an issue that requires special consideration. So, we propose a simple coding scheme that uses "0" to denote each "tick", "1" for Each "tick", and put them in a string associative array. The PHP code that defines the Moss Code encoding table is like this:

 
 
  1. $CWCODE = Array (' A '= '=',' B '= '1000 ') ,' C '=' 1010',' D '=' 'E '=' 0',
  2. ' F ' => ' 0010 ' , Span class= "string", "G ' => , ' H ' => ' 0000 ' , ' I ' => ' xx ' , ' J ' => ' 0111 ' ,
  3. ' K ' => ' 101 ' , ' L ' => ' 0100 ' , => ' one ' , => , ' O ' => ' 111 ' ,
  4. ' P ' => ' 0110 ' , Span class= "string" > ' Q ' => , ' R ' => ' 010 ' , ' S ' => " , ' 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 bits. By adding a start bit to each code, you can form a bit pattern, and each character can be stored in one byte. At the same time, when parsing the final encoding, remove the bit to the left of the start bit) to obtain a true variable length encoding.

While many people do not realize that the "time interval" is the main factor in defining Moss Code, understanding this is key to generating Moss code. So the first thing we need to do is define the interval between the internal code of the Moss code, which is "tick" and "click". For convenience, we define a "beep" for the length of the sound as a unit of time DT, and the interval between "tick" and "tick" is also a time unit dt; the interval between the definition of a "click" of 3 DT, character letters) is also 3 DT The interval between defining a word words) is 7 dt. So, to sum up, our time interval table looks like this:

Project

Length of time

Beep

Dt

The interval between "beep"/"tick"

Dt

Clicks

3*dt

The interval between characters

3*dt

The interval between words

7*dt

In Moss code, the "playback speed" of encoded sounds is usually expressed in words/minutes (WPM). Since the English words have different lengths, and the characters also have different numbers of "tick" and "click", digital sampling from WPM to audio is not as simple as it seems. In a scheme adopted by international organizations, 5 characters are used as the average length of a word, while a number or punctuation mark is treated as 2 characters. In this way, an average of one word is 50 time unit dt. Thus, if you specify the WPM, then our total play time is the time unit/minute of the WPM, each "beep" that is a time unit dt) length equals 1.2/wpm seconds. Thus, given a "beep" of the length of time, the length of the other elements can easily be calculated.

You may have noticed that in the pages shown above, we used "Farnsworth spacing" for options below 15WPM. So what is this "Farnsworth spacing"?

When operator learns to decode Moss code with his ear, he realizes that the rhythm of the characters will change as the playback speed changes. When playback speed is less than 10WPM, he can easily identify "beep" and "click", and know which character to send. But when the playback speed exceeds 10WPM, the operator's recognition will be wrong, he will recognize more characters than the actual "beep" and "click". When a person who learns to be accustomed to low-speed moss code, when dealing with high-speed playback code, there will be problems. Because the rhythm has changed, his subconscious identification will go wrong.

To solve this problem, "Farnsworth spacing" was invented. Essentially, the speed at which letters and symbols are played is higher than 15WPM, and the overall playback speed is reduced by inserting more spaces between the characters. In this way, the operator is able to identify each character at a reasonable speed and tempo, and once all the words are nonalphanumeric, the speed can be increased, and the receiver needs to speed up the recognition of the characters. Essentially, the "Farnsworth spacing" technique solves the problem of rhythm change, enabling the receiver to learn quickly.

Therefore, in the entire system, for the lower playback speed, are unified into 15WPM. Correspondingly, the length of a "beep" is 0.08 seconds, but the interval between characters and words is no longer 3 dit or 7 dit, but is adjusted to fit the overall speed.

Generate sound

In PHP code, a character, the index of the preceding array, represents a group of Moss sounds made up of "tick", "tick", and blank spacing. We use digital sampling to compose the audio sequence and write it to a file, with the appropriate header information defined as the wave format.

The code that generates the sound is actually quite simple, and you can find them in the PHP file in your project. I find it quite convenient to define a "digital oscillator". Each time the OSC () is called, it returns a timed sample generated from a positive wave. Using sound sampling and audio specifications, it is sufficient to generate the wave format. In the generated positive and dark waves between 1 to + 1 is moved and adjusted, so that the sound of the byte data can be expressed in 0 to 255来, while 128 represents the 0 amplitude.

At the same time, we need to consider another issue in generating sound. In general, we generate Moss code through the switch of the positive and the dark waves. But if you do this directly, you will find that the signals you generate take up a very large amount of bandwidth. As a result, the radio is usually modified to reduce the bandwidth consumption.

In our project, we will also make such corrections, just in the form of numbers. Now that we know the length of a minimum sound sample "beep", it can be proved that the minimum bandwidth of the sound amplitude occurs at length equal to "beep" of the half period of the positive and the wave. In fact, the same effect can be achieved by filtering the audio signal using the low pass filter, lower pass filters. However, since we already know all the signal characters, we can simply filter each character signal.

The PHP code that generates "beep", "tick", and blank signals is like this:

 
 
  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 (+ $x +));
  7. $dahstr. = Chr (Floor (+ $x +));
  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 (+ $x +));
  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 (+ $x +));
  15. }
  16. Else {
  17. $ditstr. = Chr (Floor (+ $x +));
  18. $dahstr. = Chr (Floor (+ $x +));
  19. }
  20. //A space has an amplitude of 0 shifted to
  21. $spcstr. = Chr (+);
  22. $dt + = $sampleDT;
  23. }
  24. //At the 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 (+ $x +));
  30. $dt + = $sampleDT;
  31. }
  32. //Finally during the 3rd dit-time, the Dah sound must is 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 (+ $x +));
  40. }
  41. Else {
  42. $dahstr. = Chr (Floor (+ $x +));
  43. }
  44. $dt + = $sampleDT;
  45. }

Wave format Files

Wave is a general-purpose audio format. In its simplest form, wave files represent the audio amplitude of a given sample rate by including an integer sequence in the head. For more information about wave files, please see here Audio file Format specifications website. For the production of Moss Code, we do not need to use all the parameters of the wave format options, just need a 8-bit mono on it, so easy. It is important to note that multibyte data requires a byte order with low priority Little-endian. Wave files use a riff format that consists of records called "Block chunks".

The wave file starts with an ASCII identifier riff, followed by a 4-byte "block", followed by a header message containing the ASCII character wave, and finally the data and sound data that defines the format.

In our program, the first "block" contains a format specifier, which consists of the ASCII character FMT and a 4 times-byte "block". In this case, since I am using the normal pulse code modulation plain vanilla PCM) format, each "block" is 16 bytes. We will then need this data: Channel count, sound sample/second, average byte/second, block block) alignment indicator, bit bit)/sound sampling. Also, since we do not need high-quality stereo, we only use mono, we use the sample rate of 11050 samples/second standard CD quality audio for 44200 samples/sec) to generate the sound and save it with 8 bit bits.

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

In the program, a sound composed of 8-bit audio amplitude sequences is stored in the variable $SOUNDSTR. Once the audio data has been generated, you can calculate all the "block" sizes, and then you can merge them together to write to the disk file. The following code shows how to generate header information and audio "blocks". Note that the $RIFFSTR represents the riff header, $fmtstr represents the "block" format, $SOUNDSTR represents 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% );
  6. $x = Floor ($x/N);
  7. }
  8. $fmtstr = ' fmt '.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% );
  14. $x = Floor ($x/N);
  15. }
  16. $soundstr = ' data '. $NSampStr. $soundstr;

Summary and Comment

Our text Moss code generator looks pretty good at the moment. Of course, we can also make a lot of modifications and improvements, such as the use of other character sets, directly from the file to read text, generate compressed audio and so on. Because the purpose of our project is to make it easy to use on the Web, so our simple solution has reached our goal.

Of course, as always, I hope you will make suggestions for these simple and crude code. Although I have been taught for years, I still lack the background knowledge of Moss code, so it is my fault if any errors or omissions occur.

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

http://www.bkjia.com/PHPjc/1089381.html www.bkjia.com true http://www.bkjia.com/PHPjc/1089381.html techarticle implementation of the text-based Moss code generator in PHP I recently encountered a requirement based on the input text to generate Morse codes for audio files. After a few searches, I decided to write my own ...

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