The Asterisk File Transfer Protocol
By Corey Menscher on Day 2
Dial (212) 796-0729 ext. 160 to access the Asterisk File Transfer Protocol Server!
Today I created a prototype method for transferring files via an Asterisk VoIP PBX. The inspiration came from my early computing days using a an audio cassette recorder to store applications for my first personal computer, a TI-99/4a. Programs were stored on audio cassettes, and were literally transferred to RAM via an audio signal over a 1/8” jack. If you didn’t plug in the jack but instead listened to the files through the cassette player’s speaker, they had an eery digital quality (a lot like a fax machine). So, I decided to recreate this experience over the telephone. If you have an acoustic coupler attached to your computer, you could potentially download files from the system at a blistering 300 baud!
For demonstration purposes, the A-FTP server transmits this file…an 8Kb jpeg image (which takes 3 minutes and 29 seconds to play):
I decided to use the “Kanas City standard” (http://en.wikipedia.org/wiki/Kansas_City_standard) for audio frequency-shift keying of digital files. This standard was established in 1975 at a symposium in Kansas City to provide an alternative storage solution to expensive floppy drives. (I’m not sure whether the TI-99/4a used it, but it sounds pretty close.) To convert the file into an audio file that conforms to the Kansas city standard, Adam Parrish clued me into the CSound audio programming language. Csound is “a unit generator-based, user-programmable computer music system.” Its syntax is excruciating, but it is extremely powerful. I spent most of the day trying to learn the basics to create the fundamental components of a CSound composition…the Orchestra File and the Score file.
The Orchestra file describes all the “instruments” used to generate audio. In my case, I needed two instruments…one 1200hz sine wave oscillator for “0” bits, and one 2400hz sine wave oscillator for “1” bits. The score file is what describes the audio piece itself…it tells CSound what sounds to generate, and when and how long to play each instrument. (CSound gets vastly more complex than my rudimentary needs.) Once I figured out how to generate this file, I wrote a PHP script that reads my binary image file and outputs the appropriate lines in the score file. (A big thanks to Adam Parrish again for helping with the bit masking stuff.) Once the .wav file was generated, I converted it to the GSM audio format that Asterisk loves so dearly. I then created a short dial plan to first playback a greeting, and then the file itself.
You can access the server by dialing (212) 796-0729 ext. 160. Unfortunately, the low quality GSM codec probably precludes the audio waveform from actually being used for file transfer…but it’s still a fun idea. You can listen to a higher-fidelity version of the file here (2.4mb).
Here’s the CSound .orc (orchestra) file:
sr= 44100
ksmps= 20
nchnls= 1
instr 1
; bit '1'
asound oscil 15000, 2400, 1
out asound
endin
instr 2
; bit '0'
asound oscil 15000, 1200, 1
out asound
endin
Here’s the PHP code for generating the CSound .sco (score) file:
$scoreIntro = "f1 0 1024 10 1 \n";
$outFile = "aftp.sco"; //CSound score file to write
$inputFile = "isawaturtle.jpg";
//bit masking props to Adam Parrish
function bit($val, $bitno) {
return ($val & (1 << $bitno)) ? 1 : 0;
}
//generates and outputs the individual note events for CSound
function writeScore($contents) {
GLOBAL $handleWrite;
$lastEventTime = 0; //time last note was played
$currEventTime = 0; //time to play the current note until
$trueBitLen = .0033; //play a note for this length when a bit is true
$falseBitLen = .0016; //play a note for this length when a bit is false
$thisBitLen = 0;
$startBit = "";
$stopBit1 = "";
$stopBit2 = "";
$outStr = "";
for($i = 0; $i < strlen($contents); $i++) {
$thisByte = substr($contents, $i, 1);
$startBit = "i2 " . $lastEventTime . " " . $falseBitLen . "\n";
if($lastEventTime == 0) {
fwrite($handleWrite, "i2 0 .0016 \n");
$lastEventTime = .0016;
$currentEventTime = .0032;
}
for($b=0; $b<8; $b++) {
$thisBit = bit(ord($thisByte), $b);
if($thisBit) {
$currEventTime = $lastEventTime + $trueBitLen;
$thisInstrument = "i1";
$thisBitLen = $trueBitLen;
} else {
$currEventTime = $lastEventTime + $falseBitLen;
$thisInstrument = "i2";
$thisBitLen = $falseBitLen;
}
//output this bit
$outStr = $thisInstrument . " " . $lastEventTime . " " . $thisBitLen . "\n";
fwrite($handleWrite, $outStr);
$lastEventTime = $currEventTime;
}
$currEventTime = $lastEventTime + $trueBitLen;
fwrite($handleWrite, "i1 " . $lastEventTime . " " . $trueBitLen . "\n");
$currEventTime = $currEventTime + $trueBitLen;
fwrite($handleWrite, "i1 " . $currEventTime . " " . $trueBitLen . "\n");
if($i != strlen($contents)-1) {
$currEventTime = $currEventTime + $trueBitLen;
fwrite($handleWrite, "i2 " . $currEventTime . " " . $falseBitLen . "\n");
}
$lastEventTime = $currEventTime;
}
}
/*****************************************
********** BEGIN PROCESSSING *************
*****************************************/
//first read the input file
$handleRead = file_get_contents($inputFile);
//make sure the output file exists...if not create it
if (!file_exists($outFile)) touch($outFile);
$handleWrite = fopen($outFile, 'r+'); //open the score file
fwrite($handleWrite, $scoreIntro); //write the score intro
writeScore($handleRead); //loop through the input file and write the notes
fwrite($handleWrite, "e"); //end the score
fclose ($handleWrite); //close the score file
July 30th, 2008
5 Comments
-
-
-
-
-
1. SinoLogic » Transmi&hellip | July 30th, 2008 at 4:15 pm
[…] información: http://5-in-5.com Autor: Elio Rojano | Asterisk, Curiosidades, VoIP, programacion | Permalink | […]
2. roderickm | July 30th, 2008 at 11:34 pm
Neat hack!
According to the source code comments in ti99sim’s decode.cpp, the format of the recorded data on TI-99/4a cassettes is Miller Code, a simple Binary Line Code.
TI-99/Sim — http://www.mrousseau.org/programs/ti99sim/
I still have some TI-99/4a and cassettes in a box in my garage. Some things I just can’t throw away. It’s a sickness.
rm
3. TerminalDigit - Asterisk &hellip | July 31st, 2008 at 7:50 pm
[…] read more […]
4. Asterisk File Transfer Pr&hellip | August 1st, 2008 at 1:39 am
[…] The Asterisk File Transfer Protocol […]
5. appeape | October 7th, 2008 at 7:10 am
?????????? ?????????? ??? WordPress 2.6.2, ????? ??? ????? ?? ??? 5-in-5.com.
??????? ?????????)