The Csound Blog by Jacob Joaquin email jacobjoaquin@gmail.com web www.thumbuki.com/csound/blog (C)2008 Jacob Joaquin Licensed under Creative Commons (see below) 2008.01.02 The Infamous mcseq I want to learn morse code.[1] I don't know why, I just do. So a few days ago, I made it my new year's resolution. The first action I took to help me accomplish this goal of mine was writing the Csound instrument Morse Code Sequencer Event Generator, or mcseq for short. Instrument mcseq parses a string of morse code and generates i-events. And that's it. A synthesis engine does not come included, and must be provided by the user for mcseq to do anything significant. Instrument mcseq is purposely written in a very generic manner, so people can tailor their sound engines to their own personal needs. If you take a look at the header of mcseq in the orchestra, you will notice four generic parameters: ithru1, ithru2, ithru3 and ithru4. These four parameters are passed through to the instrument defined in p-field 4 when the events are generated. The actual purpose these four parameters serve are defined in the synthesis engine. To demonstrate how the four generic parameters are used, I created the synthesis engine instrument ditadahVonBeep.[2] Here, the four ithru parameters are mapped to amplitude, frequency, pan position and wave table. There are a few mcseq quirks I should point out. P-field 3, which is usually used for duration, must be set to 1. Why? It's a hack that passes the current score tempo to an instrument without having to do so explicitly. This works because whatever value is placed in p-field 3 is translated into time-in-seconds. If the tempo is set to 60 bpm, mcseq will receive the value 1 for p-field 3. If the tempo is 120 bpm, mcseq will receive the value 0.5 for p-field 3. As for the other p-fields: P-field 4 is used to declare which instrument mcseq will generate events for. P-field 5 is the resolution of the dit. For example, a value of [1/4] will mean that a dit equals one quarter note. P-fields 6, 7, 8 and 9 are the generic pass-thru parameters. P-field 10 is a string of morse code. Creating a compatible synthesis engine should be fairly straight forward. I've included the instrument mcseqTemplate to help get you started. Here's the run down: P-field 4 receives either a value of 1 or 3, indicating whether or not it's a dit or dot. Instrument ditdahVonBeep does not use this parameter, but I coded this feature into mcseq because I can conceive of smart engines where the behavior differs based on being a dit or dah. For example, a dit might be a kick while a dah could be a snare. P-fields 5, 6, 7 and 8 receive the values from the generic ithru values. Morse Code Primer[3] Dits are represented by dots, dahs are represented by dashes. A space represents the division between characters. A forward slash indicates the the division between between words. .... . .-.. .-.. --- / .-- --- .-. .-.. -.. Figure 1. "Hello World" in morse code. The fundamental time unit of morse code is the dit. Characters, spaces and word separators all take their cue from the dit. A single dot equals one dit. A dash, also known as a dah, equals three dits. The space between dits/dahs within a character equal one dit. The space between characters equals three dits. The space between words equals seven dits. dit = 1 dah = 3 space within characters = 1 space between characters = 3 space between words = 7 Figure 2. The time units of morse code. As for converting strings of text to morse code, I use Qbit's Online Morse Code Translator.[4] The mcseq instrument is designed to parse morse code based on Qbit's morse format. The website for Qbit's Morse Code Translator is: http://www.qbit.it/lab/morse.php Users beware! While some ascii-to-morse code translators will work, others will not. Until next time, Jake P.S. Thanks and congratulations to Dr. B. and Cesare Marilungo for the successful cSounds.com face lift.[5] Permalink http://www.thumbuki.com/20080102/the-infamous-mcseq.html References [0] The title "The Infamous mcseq" is a pop-culture reference to the film, The Three Amigos, where a telegraph regarding a villian is reduced to "The Infamous El Guapo." http://www.imdb.com/title/tt0301934/ [1,3] Morse Code @ Wikipedia.org http://en.wikipedia.org/wiki/Morse_code [2] "ditdahVonBeep" is a pop-culture reference to the pin-up model and burlesque dancer Dita Von Tease. http://en.wikipedia.org/wiki/Dita_Von_Teese [4] Qbit Morse Code Translator http://www.qbit.it/lab/morse.php [5] cSounds.com http://www.csounds.com License (cc) Creative Commons Attribution-Share Alike 3.0 Unported You are free: * to Share -- to copy, distribute and transmit the work * to Remix -- to adapt the work Under the following conditions: * Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work). * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. * For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page. * Any of the above conditions can be waived if you get permission from the copyright holder. * Nothing in this license impairs or restricts the author's moral rights. http://creativecommons.org/licenses/by-sa/3.0/ sr = 44100 kr = 4410 ksmps = 10 nchnls = 2 ; ---- Macros ---- # define mcseq # 1 # ; Morse Code Sequencer Event Generator # define ditdahVonBeep # 2 # ; Synthesis Engine # define mcseqTemplate # 3 # ; mcseq synthesis engine template # define tWave # 1 # ; Table storing a wave shape instr $mcseq itempo = p3 ; You should set p3 to "1" in the score iinstr = p4 ; The instrument to generate events for iresolution = p5 * 4 ; Resolution of note ithru1 = p6 ; Generic parameter passed to iinstr ithru2 = p7 ; Generic parameter passed to iinstr ithru3 = p8 ; Generic parameter passed to iinstr ithru4 = p9 ; Generic parameter passed to iinstr Spattern strget p10 ; Morse code to parse ilength strlen Spattern ; Length of the pattern. iSindex = 0 ; Index of current position in string iditTime = 0 ; Point in time in dits idit = 1 ; Time unit of dit idah = 3 ; Time unit of dah imessage = 1 ; If 1, print message to terminal window begin: Schar strsub Spattern, iSindex, iSindex + 1 icompare strcmp Schar, "." if( icompare == 0 ) igoto dit icompare strcmp Schar, "-" if( icompare == 0 ) igoto dah icompare strcmp Schar, " " if( icompare == 0 ) igoto characterspace icompare strcmp Schar, "/" if( icompare == 0 ) igoto wordspace ; No match igoto advanceSindex dit: schedule iinstr, iditTime * itempo * iresolution, idit * iresolution * itempo, idit, ithru1, ithru2, ithru3, ithru4 iditTime = iditTime + idit + idit igoto advanceSindex dah: schedule iinstr, iditTime * itempo * iresolution, idah * iresolution * itempo, idah, ithru1, ithru2, ithru3, ithru4 iditTime = iditTime + idah + idit igoto advanceSindex characterspace: wordspace: iditTime = iditTime + 2 * idit igoto advanceSindex advanceSindex: iSindex = iSindex + 1 if( iSindex < ilength ) igoto begin if( imessage = 0 ) igoto end printf_i "\nMorse Code Info: dits = %d\n", 1, iditTime printf_i " beats = %f\n", 1, iditTime * iresolution printf_i " time = %f\n\n", 1, iditTime * iresolution * itempo end: endin instr $ditdahVonBeep idur = p3 ; Duration inull = p4 ; Does nothing iamp = p5 * 0dbfs ; Amp scaling ifreq = p6 ; Frequency of beep ipan = p7 ; Pan position iwave = p8 ; Index of oscillator wave table ; Envelope aline linseg iamp, idur * 0.95, iamp, idur * 0.05, 0 ; Oscillator aosc oscil aline, ifreq, iwave ; Output to dac of file out aosc * sqrt( 1 - ipan ), aosc * sqrt( ipan ) endin instr $mcseqTemplate idur = p3 ; Duration iditdah = p4 ; Is this dit (1) or dah (3) ithru1 = p5 ; Generic pass-thru paramter 1 ithru2 = p6 ; Generic pass-thru paramter 2 ithru3 = p7 ; Generic pass-thru paramter 3 ithru4 = p8 ; Generic pass-thru paramter 4 ; Insert Synthesis Engine Here endin ; ---- Macros ---- # define mcseq # 1 # ; Morse Code Sequencer Event Generator # define ditdahVonBeep # 2 # ; Synthesis Engine # define mcseqTemplate # 3 # ; mcseq synthesis engine template # define tWave # 1 # ; Table storing a wave shape ; ---- Tables ---- f $tWave 0 [2^16+1] 10 1 1 1 1 1 1 1 1 1 1 1 1 ; ---- Tempo ---- t 0 120 ; ---- i-events ---- i $mcseq 0 1 $ditdahVonBeep [1/32] 1.0 440 0.7 $tWave ".- ... / ..-. .- .-. / .- ... / -.. .. --. .. - .. .- .-.. / ... -.-- -. - .... . ... .. --.. . .-. ... / .- .-. ." i $mcseq 33.75 1 $ditdahVonBeep [1/32] 1.0 435 0.3 $tWave " / -.-. --- -. -.-. . .-. -. . -.. --..-- / -.-. ... --- ..- -. -.. / .. ... / .- ... / ...- .. -. - .- --. . / .- ... / - .... . -.-- / -.-. --- -- . .-.-.-" e 85