Step 4: Decode the Audio

So, now we've got a bunch of audio on our device. How do we decode it? I based my code on an Android tutorial which shows how to record data and then play it back. In my case, I made sure to save that audio as 16bit PCM encoded. I sampled at 44100hz. On Android (and elsewhere, I suppose) 16bit PCM data means that each sample is a signed 16bit value. Since we only care about the frequency, we only need to care about how much time there is between "zero-crossings". A zero-crossing is when the signal goes from postive to negative or vice-versa. A 0 bit will be represented by the space between 2 crossings, and a 1 will have an extra crossing in approximately the same time period.
Card data in each track starts off with some (variable) number of 0s, to establish the base frequency. What I did was listen for the first sample above a certain "quiet" threshold, then count the number of samples between zero-crossings. That number becomes the base value for a 0. Since these cards are hand-swiped, the actual frequencies will change somewhat from the start of the scan to the end. So, I made a simple method which determines if the number of samples since the last zero-crossing is closer to the base frequency or twice the base frequency (half the base number of samples). It then adjusts the expected base frequency accordingly. This works well, so long as the changes between any two logical bits are fairly small. And they almost certainly will be.

To detect a zero-crossing, we need to look at the sign of each sample and compare it to the sign of the previous sample.  If they differ (one positive, one negative) then the signal crossed 0 between those samples.

The basic algorithm is to iterate through the byte array, extracting samples.  Count the number of samples between zero-crossings, and compare the count to the expected count for a 0 or 1.

Okay, after some hand-waving, we now have a binary sequence of data, which we want to turn back into ASCII. The most common encoding (and the only one I wrote a handler for) encodes each character as some number of bits, plus one parity bit. In the case of track 2, that's 4 bits for the character, and 1 for parity, making 5 bit groups. The bits are read from least significant to most, with the parity bit last. The parity bit is set to make the number of 1s in the group odd. In my implementation, I just disregard the parity bit, but it would help determine whether the read was good or not. In track 1, it's 6 bits for the character, plus the parity.
The character set of the tracks differ too, but both are subsets of ASCII with some offset. In the case of track 2, which only encodes some symbols and digits, the character set starts at 48, which is the ASCII code for "0". So if we get 0,0,0,0,1 as our character, we turn that into 0, add 48, and get 48. Similarly, 1,0,0,0,0 is 1. 1+48 = 49 = ASCII "1".
For track 1, the character set starts with " " (space) which is ASCII 32. So we add 32 to the decoded numeric value and get our ASCII character. After that, we have the data, so all that remains is hooking up the UI glue.
<p>i am from venezuela ...we like use the square reader to our university...your lib work??? we buy the square dongle but in the rhombus app dont see anything...there is a sdk files or something we can use to read the cards with the hardware</p>
No, Square readers are all encrypted now, which means 3rd party libraries like Rhombus cannot make use of them.<br>You can get unencrypted readers from alibaba, but I am no longer supporting Rhombus. Feel free to use RhombusLib on github as a base for your own code.
<p>Is it possible to use the square reader as part of a library check-out system? We are a not-for-profit literacy foundation working in Haiti. Built a new library and need a cheap reader system to read a library card and book card.</p>
yes scott very cheap reader I use simple and small spy camera this is a sample at vid.me search soundwavesmp4
<p>I use <a href="http://www.booksource.com/for" rel="nofollow">http://www.booksource.com/for </a> my class library.</p><p>I just scan the ISBN code with my iphone/ipad. Most books are imported correctly. I believe it also works with barcode readers on the computer. I hope this helps</p>
<p>http://www.booksource.com/ </p>
I already made this software.. u mean a soundwaves I try also a small spycamera I take off the small mic and then I change it to magnetic reader then I swaaap it and spycamera record it then I convert it to software I see my complete track2...
<p>contact me bro i have a software for that audio reader michaelsandiego59@yahoo.com</p>
<p>You suggested one unencrypted reader 3 months ago (http://www.aliexpress.com/item/free-shipping-Mobile-Credit-Debit-Card-Reader-for-Apple-Android-iOS-3-5mm-Black-and-white/1071970592.html) but that one is no longer available. Is there another we can use?</p>
<p>Is there a way to implement this to read identification cards. Specifically to get the age of the ID holder.</p>
<p>hi, im professional decoder, i can decode any sound file to track2!</p><p>if you need a good decoder contactme<br>this is im ICQ: 655803449<br>jabber : decode.wav@jabbim.cz</p>
<p>Do you know if the paypal reader is encrypted? Why did they start encrypting these readers? I can't see a valid security reason, so I'm guessing it is specifically to stop rival companies' apps from using their free hardware?</p>
<p>Hi! do you have an example of how to implement the lib? Thanks in advance!</p>
<p>I was looking for a general magnetic stripe reader and thought that this might be a solution. You mention that this won't work with a newer square device - how would if my square reader is the newer model. Trying to run this on the galaxy S5 and it doesn't seem to be reading...</p>
<p>It's been years since Square has produced unencrypted devices. If your device isn't working, that is probably the cause. Sorry. There are unencrypted readers you can order from China. They should only be a few dollars from alibaba or dx.com.</p><p>If you really want to see whether yours is encrypted, you could open it up and look for a battery and PCB/chip. The unencrypted readers were just a read head and some wires to a headphone jack.</p>
<p>Do you know if similar devices like one from beanstream would work?</p>
<p>I doubt it. I'd never heard of them before, but a quick google search says that they encrypt data, and it seems they also support chip and pin, which is much more involved.</p><p>Here's a link to the unencrypted chinese readers I've bought and used successfully: http://www.aliexpress.com/item/free-shipping-Mobile-Credit-Debit-Card-Reader-for-Apple-Android-iOS-3-5mm-Black-and-white/1071970592.html</p>
<p>For those of you who needs help get in touch with i can help you with almost everything you need</p>
<p>Hi! do you have an example of the implementation of the lib?</p>
Excelent instructable!
Hi Eried, Am from UAE, can I have any ideas of dumps? Coz I can do nice works....if u have 101 tracks, I would like to get it....please reply me
<p>Ashiq please get in touch with i might be able help you. Thanks</p>
http://1wutrans3er.blogspot.com Dumps track1&amp;2 with pin.
No I don't
http://1wutrans3er.blogspot.com Dumps track1&amp;2 with pin.
<p>Hi, </p><p>Can you send me me the sample code of implementing Rhombus Lib in android app?</p><p>I did not get the solution on google and I need to implement it in my android app ASAP.</p>
I've purchased several square readers. They all have several ICs so its not just a head and a resistor! Perhaps the preamp in the smart phone can pull out the signal, but you may need an amp. I used a gain of 100 to bring the signal up to a good volt so maybe a preamp set at 20X might do the trick too. The actual voltage reading coming off the head is only a few mV at best.
Hi, first thanks a lot for the great tutorial!. I am trying to buy a square reader, however as you say in the last part of the tutorial I am afraid it would be one of the encrypted ones and it won't be useful. Any chance you can tell me the square reader you used to buy the same or a equivalent one? <br> <br>Thanks!.
Is there any way to be able to read the information placed on all three tracks... at once?
Not with the square reader, or any reader that simple. There's only one microphone channel in the headphone jack, so to get multiple signals in you'd have to multiplex them somehow. That would involve &quot;smart&quot; reader hardware and much smarter decoding software. There are 3 track magnetic read heads available, and I'm sure it would be possible to construct a 3 channel raw reader with a microcontroller, but you'd still need to get that data to the phone somehow.
Thx for your reply
One more thing, does the lib only reads track2 data? Once I swipe card, how can I interpret track 1 and 2 separately?
The RhombusLib library actually can decode either track 1 or track 2, but not both from the same swipe. The square reader provides only a single channel of data, and is physically aligned to read track 2. You can offset the read head relative to the magnetic stripe by inserting a small shim to align the read head with track 1. This is obviously not an exact science.
Hi, <br> Can you send me or post the code sample how to use the lib? I've no idea when to call those classes in my app. Thx in advance.
I have a demo program that I will create a github repository for, probably this evening. I'll update with a link to it when it exists.
How about writing using the same technique, the other way around ? <br>I mean playing the audio, moving the card at a certain rate, and using the (cassette-recorder?) head to write....

About This Instructable




More by sshipman0:Read any magnetic strip card with a Square reader and an Android device 
Add instructable to: