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:

82 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
    4. Hi, Can you mail me the source code?
      i have project read controller
      email: khanhleviet.hut@gmail.com

      Delete
    5. hi, can you please share source code?
      I`am working on a android project which tries to gather signal from
      a 3.5mm jack , find your work might be useful
      email:rayallen3001@gmail.com , thanks

      Delete
    6. Hi. I read this blog thoroughly.
      But I could not your email, where is it?
      I would like to your source codes, please send me those.
      (mhbae@utarex.com)
      Thanks for your working.

      Delete
    7. If this works for my band, you'll surely be credited.
      pageau4@gmail.com

      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
  53. Please can I have circuit and source as it looks interesting.
    Thanks.
    Please e mail me bobbybuilder90 at gmail dot com

    ReplyDelete
  54. Hi, could ypu please shere the source via azakalyukin@gmail.com ?

    ReplyDelete
  55. Hello, would you please send me the source code & schematics to: blueberry.org.in@gmail.com

    Thank you very much in advance!!!

    ReplyDelete
  56. Hi Jernej,

    I know you posted this some time ago, but I'm hoping you still monitor this post and respond to questions. As, I'm really interested in giving this a try and want to try to adapt it to work with Arduino.

    Could you please email me the schematic and source? lukegmATgmailDOTcom

    I also have a few questions mostly about the schematic, which I hope you'll have the time to answer:

    - Is the serial connection duplex (in both directions) or is it only Android to PC?

    - Are V1 & V2 are the headphone socket/s? If they are input & output, which is which?

    - There appear to be 2 headphone sockets on your board (V1 & V2?), 1 at the bottom of the photo and the other at the top next to the 9pin Dsub (serial port). Is that correct or am I a bit confused?

    - If there is a second headphone socket, why didn't you use either the second channel or microphone connection in the first one? Or did not have a 4 pole socket lying around, so you've used 2 and then joined them in the cable?

    - What component/s did you use for U1 & U2?

    - Lastly, on schematic it looks like there is only a single connection going to the PC serial port, not even ground/common. Is that correct?

    Thanks in advance for taking the time to answer my questions, I hope I haven't taken too much time out of your day.

    Regards
    Lukegm

    ReplyDelete
  57. Hey there Jernej,

    I too hope you are still keeping up on the blog and would love to take a look at your code for this project. My email is nickcres13@gmail.com. Thanks a bunch!

    ReplyDelete
  58. Peace from ukraine. Need sources and schematics to see how abroad colegue works =) progressman757@gmail.com

    ReplyDelete
  59. Hi! Nice work! Could you please send me the source code & schematics to: neuronea.development [at] gmail dot c0m ?

    ReplyDelete
  60. Hi Jernej,
    Amazing work! I would be very glad if you could send the source code to my mail: shahaf.bassan@gmail.com
    It would help me very much on a project I'm currently working on.
    Thank you very much!

    ReplyDelete
  61. Hi Jernej

    Nice work and really great idea!

    If it is possible, could you send me the source-code?

    minsus79@gmail.com

    thank you very much

    ReplyDelete
  62. Hey Jernej!

    Really cool stuff here. I know its been a while but if you can, could you send me the source code? It would help a lot with a project that I'm currently working on!

    jimmy.lee.chen@gmail.com

    Thanks so much!

    ReplyDelete
  63. hi, hope you still see this. this is a very cool stuff. could you please also send me the code and schematic? many thanks.
    ansonchan911@gmail.com

    ReplyDelete
  64. hi can you send me the source code it'll be a real help to me
    saeeid.ehsani@gmail.com

    ReplyDelete
  65. Hey Jernej!

    Can you send me the source code it'll be a real help to me. Thanks so much!
    Rostislav_tt@mail.ru

    ReplyDelete
  66. Thanks for such a wonderful and brilliant idea....please if possible send me the code and schematics
    Email: davies.segera@gmail.com

    ReplyDelete
  67. This comment has been removed by the author.

    ReplyDelete
  68. Hi,
    Please, send to me source code, it will be a real help to me. Thanks so much!
    tunguyen90vn@gmail.com

    ReplyDelete
  69. This comment has been removed by the author.

    ReplyDelete
  70. Hi nice Project, I would like to have the source code and schematics to create a project that saves the output to an audio file julio7k (at) gmail (dot) com

    ReplyDelete