Saturday, January 30, 2010

"Demodulator" Updated

So, this thing now finally works for all intended purposes of getting any kind of data out of a Android device as it should, so it would be safe now to reveal some details.

Simple schematics and implementation:



And now some software tips. For this to work it is recommended to have pre-generated data samples, mostly because of the timing.

In this case, raw binary data of serial ASCII packets (start bit, payload, stop bit). You might make them like this:

ofstream outfile;
char * buffer = new char [55];
// Start bit (low)
for (int i = 0; i < 5; i++) {
buffer[i] = 25;
}

// What you need here in binary (low is 25, high is 230), just remember to reverse the order!

// Stop bit (hight)
for (int i = 45; i < 50; i++) {
buffer[i] = 230;
}

outfile.open ("snd.raw",ofstream::binary);
outfile.write (buffer,55);
outfile.close();


Written for C, assuming the sound sampling frequency is 48000, because then one bit is 5 samples in length.

All of that produces this nifty image on the oscilloscope (blue being Schmitt trigger output and the yellow being serial output) :


Now to use it in an Android app:
  • we need direct access to the PCM stream and a buffer to generate what we want to output from the pre-generated samples:

private static final Integer minBufferSize = android.media.AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT);

private final AudioTrack oTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_8BIT, minBufferSize, AudioTrack.MODE_STREAM);

private byte[] sndBufferTrack = new byte[minBufferSize];

  • next we need an array for the samples (because of the faster read access) and then to read them in:
private byte[][] sndSamples = new byte[256][50];

try {
inputStream = getResources().openRawResource(R.raw.snd);
inputStream.read(sndSamples[0]);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//You'll all figure out how to do this for the rest 255 of them

  • then we fill the buffer with the generated data and play it:
oTrack.play();
for (int i = 0; i < 50; i++) {
sndBufferTrack[i] = sndSamples[0][i];
}
oTrack.write(sndBufferTrack, 0, minBufferSize);
oTrack.stop();
oTrack.release();

And all of this, with right polarity, correctly generated samples and the right amount of volume finally produces consistent and mostly artefact's free data transfer:



With end result being able to use G1's orientometer to control the plane in FlightGear simulator:

video

60 comments:

  1. Please, post all source code and LTSpice schematics. It is very help me.

    ReplyDelete
  2. Send me an email, since I'm disinclined to host files on the blog.

    ReplyDelete
    Replies
    1. Hi can u share source code..
      Actually i want to convert Audio data in actual data

      Delete
    2. Hi, Can you mail me the source code?
      kitsooftlk@gmailcom

      Delete
    3. Hello, i am also very interested on the source code.
      My mail is ingold.alvaro@hotmail.com

      In addition to that, i would also like to know how to make it work the other way around, that is, to receive data in the headphone jack and digitalize it on the phone.

      THANKS!

      Delete
  3. Very cool! Thanks for sharing this. I wonder if some simple checksum or some error correction code algorithm would make it 100% artifact free?

    ReplyDelete
  4. Hi,
    I've been searching for days, would you please email all source code and LTSpice schematics to my email...

    ramyalisawi at hotmail point com

    ReplyDelete
  5. I also would love to see all of the source code schematics for this. If you could email me that would be great. Thank you very, very much.

    ghast09 at hotmail dot com

    ReplyDelete
  6. I was wondering if you have tried to get serial communication both ways. I'm trying to get serial input to the android using the microphone input but am having trouble. Did you attempt to try serial to the android and do you have any suggestions. thaaanks

    ReplyDelete
  7. I'd love to check out the source code if you would email it to me. It's tyresume at gmail dot com. I've written a fun app to remotely open my garage door via bluetooth and and have some other ideas for remotes but the range of bluetooth is too short. I'd love to implement what you have. Check out my video if you like.
    http://www.youtube.com/watch?v=Ec4xj1VBrlo

    ReplyDelete
  8. If you dont mind, can you send it to me too? thanks! my email is edfungus at gmail dot com

    ReplyDelete
  9. Hey! Very cool!!
    I´ve tried to do this some months ago, but I had timing problems caused by audio-generation on the android device. Atm I´m using the Bluetooth-Module "btm-222" but I would prefer your solution.
    Can you send me the android-project and if possible the C-Code, too?
    Thank You!

    ReplyDelete
  10. my e-mail is janr.desire at googlemail dot com

    ReplyDelete
  11. where is the source ??

    you can send to my email?

    ricardo.jl.rufino@gmail.com

    ReplyDelete
  12. could you forward the source my way as well. Your help would be highly appreciated. Thanks.

    chhabra.abhin at gmail dot com

    Again, thank you very much.

    ReplyDelete
  13. Very impressive. Could you send me the source code and schematics, it is very help for me.

    pqqoeh@gmail.com

    Thanks for sharing..

    ReplyDelete
  14. This is very cool, I was just wondering if it was possible. Please send the source code and schematics.
    birkebjerg|gmail.com

    ReplyDelete
  15. Hi... I've been searching a solution like this for days. Please send the code and schematics to: fbayoff@newlinebuilders.com

    Thanks.

    ReplyDelete
  16. Hello! You have an awesome post here. Can you please send me the complete source code? pdrocb@gmail.com

    Thank You!

    ReplyDelete
  17. If you are still around and able, I'd love for you to send me the source code as well please?
    Thanks.
    Picmes@Yahoo.com

    ReplyDelete
  18. Hello, would you please send me the source code & schematics to: crabryan@ gmail dot com

    Thank you very much!!!

    ReplyDelete
  19. Hi, Please share me that source code: nguund@gmail.com.
    Thank you so much!

    ReplyDelete
  20. Hi. Nice Work! Really useful for dummies like me! Pls mail me the source: mahesh.bsb@gmail.com

    ReplyDelete
  21. Please mail me the Schematics and full source code
    I need this for my project of android based robot

    my email : zubairbh@gmail.com

    www.mzubair.com

    ReplyDelete
  22. would like the source, too!

    aschapupaya[at]gmx.de

    ReplyDelete
  23. interested in source code and schematics
    andrea.manzato92@gmail.com

    ReplyDelete
  24. I want to read the audio jack(as in squareup)

    ReplyDelete
    Replies
    1. Connect the input data line to microphone connector on the jack. Then record the input and demodulate it.

      Delete
    2. "input data line?"

      Yes, the line which contains the data from the device you want to read trough the audio jack.

      Delete
    3. How will I set MediaRecorder.AudioSource as audio jack?

      Delete
  25. Wow, awesome post, hey could you please send me the source code at sanjupahari at gmail dot com please.
    Commendable job friend
    A beer on me, let me know how to send

    ReplyDelete
  26. Please send me the source code and schematics vinartrulz@gmail.com
    appreciated...

    ReplyDelete
  27. I would also love to have the source code and schematics, if you wouldn't mind. This would be perfect for a project I want to work on. Thank you!

    (Email to dylandroid.20.trauts@xoxy.net )

    ReplyDelete
  28. Please email Source info@greenbox.lv . Thank you!

    ReplyDelete
  29. Hi, looks like a great project.. can you please email me the source too, looking forward to experimenting with it.

    avireider@gmail.com Thanks!

    ReplyDelete
  30. Ohh great example. Please email me the source to try with this one. ricardo.castro.rada [at] gmail.com

    ReplyDelete
  31. Please, send source code to risele0@gmail.com
    thanks!

    ReplyDelete
  32. Very cool! Great sample.
    Can u please email the source code and schematic to me?

    chanlokchuen@gmail.com

    ReplyDelete
  33. Can you please email the source code?

    hitesh24by365@gmail.com

    ReplyDelete
  34. Great Work!! Exactly what i search to begin my project.

    Can you email the source code?

    svenh817@gmail.com

    Thank you.

    ReplyDelete
  35. This is Great!, Can you please email the source code and schematic.
    ikash.malik@gmail.com
    Love You.

    ReplyDelete
  36. awesome work. please mail me the source code..
    at neelkanth.jha@gmail.com

    ReplyDelete
  37. Wow! Very nice!
    Can I get your code and schematic?
    suwangjang@gmail.com

    Thank you!

    ReplyDelete
  38. hi,
    i'm trying to do something similar, just the other way round (audio in on android, get digital data from analog audio stream).
    could you send me the source-code?

    herbert.hartkor@gmail.com

    thanks,
    lukas

    ReplyDelete
  39. Wow! Very nice!
    Can I get your code and schematic?
    aruncheela@gmail.com

    Thank you!

    ReplyDelete
  40. intresting I like to view your code too
    m.heydar.elahi@gmail.com

    ReplyDelete
  41. It is very interesting! Could you share the the full source code?
    jasher.itzarel777@gmail.com
    Thanks!

    ReplyDelete
  42. looks amazing!

    love to have the source and schematic :)

    eman4489@gmail.com

    ReplyDelete
  43. I'am doing same kind of project. Can you send me source and other nessasary details. (like how to make that circuit....)
    Thanks in advance..
    my mail is : isharafe@gmail.com

    ReplyDelete
  44. Hi, I am suffer on this issue, and would like to learn from your source code. could you please mail your source code.
    Thanks in advance,
    my mail is tonylu880042@gmail.com

    ReplyDelete
  45. Can you please email the source code and schematic?

    edsonbysat@yahoo.com.br

    ReplyDelete
  46. Can you please email the source code as well?

    grasserlaurent@gmail.com

    ReplyDelete
  47. Nice work and really great idea!

    Can you please email the source code and schematic?
    rtrampenau@gmail[dot]com

    ReplyDelete
  48. Nice work
    can you please email the source code and schematic to me?
    nlalitn@gmail.com

    ReplyDelete
  49. Please send me the source code, thanks.

    ReplyDelete
  50. I am just wondering that you allocate a buffer with length 50, but tried to generate a snd.raw with length 55. `outfile.write(buffer,55); `. Is it a typo or I missed something? I'd be very appreciated, if you can share the source code with me (sx.away[at]gmail.c0m)

    ReplyDelete
  51. Very cool, I am trying to get data from Android phone jack, but it's not easy to get the right frequency of input data. Could you send me the source code to refer if you don't mine? My email is up2uhclg@gmail.com.
    Many thanks!
    Tom

    ReplyDelete
  52. Very nice piece of work! I've been looking for a way to hook into the android's mic and headset. Have you tried going the other way. That is data into the mic jack? Could you send schematics and source. I really appreciate it. My e-mail is bishop.ellison at gmail.com.
    Many Thanks,

    Bishop

    ReplyDelete