Audio input through USB in Android - android

I have been wondering, on how to capture Audio inputs through USB in Android.
My scenario is to receive audio through external hardware and play that received audio through android app. This transmission is to be done over USB.
Is there any way to do this using Android SDK / Android NDK.
Any suggestion will be helpful to me.
Task Done Right by time I am able to interact with Hardware using CDC class and also able to play some random noisy audio through USB in my app. Neither I am able to get clear sound by that approach, nor there is consistency within the transmission of audio.
Thanks.
Regards, Vivek

Most modern Android devices can act as USB host. So you can connect e.g. USB microphone for capturing the audio. Android also contains support for usb_audio class. Use that to get access to the audio on the device.
Since you have already experimented with Communication Device Class (CDC), you are aware of Android's USB host functionality. Now you need to ensure your peripheral has implemented USB audio class (the audio source part) and make your app to use the audio class to obtain the audio. This pretty well explained here, so it does not make sense to copy all the information to this post. If you are already using audio class, that page may explain some of the issues you have (e.g. using wrong format).
USB Audio class specifications can be found at USB.org website. The problem with those is that Audio class is pretty large and Android probably does not support everything.

Related

How play web audio with bluetooth devices

I programmmed a website which uses text to speech engine to generate audio files.
Then these mp3 files are started using Web Audio API.
Everything works fine when hearing aufio from speakers on a computer or on a smart phone.
However, as soon as I connect my bluetooth helmet to the smart phone, the audio is not played.
Is it a famous issue that Web Audio API doesn't work with bluetooth devices, or does the issue come from my code?
Do I need to change the context's destination ? How can I set it to buetooth? (ex : https://www.html5rocks.com/en/tutorials/webaudio/intro/)
source.connect(context.destination); // connect the source to the context's destination (the speakers)
This question has already been posted on stackoverflow, but I can't find an answer.
Please help me.
Hi use the web bluetooth library for more reference read this here

How can I process audio in real time using an USB guitar interface in android?

I want to learn how to process audio input in real time in Android, so I want to make an experimental app that do simple audio manipulation of a guitar connected to the Android device, implementing a simple overdrive effect just to learn.
I know that it's posible to capture audio using the AudioRecord class when using a normal audio input like the built in device mic, or using an TRRS 3.5mm Jack cable, but in most phones the latency is high, I suppose that some guitar tuners apps works in this way, but I'm wondering how AndRig, DrAmp and usbEffects apps works using an external usb guitar interface like the Behringer
UCG102 to achieve a very low latency audio processing.
So, I want to use this usb guitar audio interface in my project to achieve low latency.
I searched for examples using this kind of guitar interfaces and I didn't find any tutorial or library to use it, so...
Do this Guitar to USB interface can be used like a normal input when connected to the device (So I can use the AudioRecord class like in a normal input)?
Do you know if exists some documentation or a tutorial to work with this kind of audio interfaces?
Or do I need to learn the Guitar to USB interface specific protocol and use low level USB API programming to do what I want?
The easiest way is probably buying this. There is also an API for android that looks a lot like LibUsb, you could try that to read the Usb endpoint as raw data (not sure if it would work out for your device though).
Another option is to make your own hardware (buy a microcontroller with a good ADC), and use [bulk transfers](https://developer.android.com/reference/android/hardware/usb/UsbDeviceConnection.html#bulkTransfer(android.hardware.usb.UsbEndpoint, byte[], int, int)) (usb communication protocol for lots of data).

How To Modify Android's Bluetooth Stack to Enable A2dp Sink

I'm working on an audio recorder app that uses a bluetooth mic to record audio on to an Android device (Nexus 7 - rooted Android 4.4.2). It's currently implemented on HFP and everything is working fine. The bluetooth mic is implemented with Bluegiga's WT32 bluetooth module + a mic input, audio quality via HFP isn't great but it's sufficient for now.
However, I'm now trying to change the bluetooth profile to A2dp, since there are two mic inputs (L/R) and WT32 supports A2dp (source). After much research I found that stock Android doesn't support A2dp (sink), and it's possible to modify Android's bluetooth stack to enable A2dp (sink).
What I don't understand is how does one access and modify the bluetooth stack. It would be nice if someone with an answer is able to break-down the steps to achieve this.
I've tried following the answer to this question:
Receive audio via Bluetooth in Android, yet I can't seem to find the appropriate file to modify. Actually, I don't even know if I'm looking into the right folder. I've looked through the devices file via Android-studio's DDMS-File Explorer.
ps, I'm still fairly new with Android app development, so I may have misused some of the terminologies and I apologies in-advance for that.
So the above answer isn't totally correct.
The following is how it breaks down:
HAL, is the hardware abstraction layer that implements the actual Bluetooth state machines in c/cpp code, as such it controls the various state machines for A2dp, HFP, GATT, SPP, AVRCP, etc. services. Each of these services also reference SMP and ATT files for controlling the actual Bluetooth server or client databases, and there security.
HCI, is where the actual work gets done. the HAL doesn't really do anything, it assembles the complex data messages that get sent along a tty serial (either spi, or UART) to an inter-network connected chip on the PCBA via the methods used in the HCI layer, which can be found in the "BTE" layer in /external/bluetooth/bluedroid/ directory of an android compiling trunk from AOSP 4.2.2 to current. - currently there are several manufacturers of these chips but they are mostly all Broadcom based ic's packaged in a dual, or triple radio package that contains a wifi, Bluetooth 4.0 Smart, and Bluetooth 4.0 radio.
It is possible to do what you are trying to do, but you would need to include hardware.so, and bluetooth_jni.so into an NDK/JNI package/project that goes with your apps, and registers via the calls from the .cpp files for each of the Bluetooth services found in "Packages/apps/Bluetooth/jni", you would then handle the registration in your NDK library of 'com_android_bluetooth_a2dp.cpp', and 'com_android_bluetooth_avrcp.cpp', as their appropriately typed objects.
The other issue is, you will need to implement your own custom A2DP stack, as the Android Bluedroid stack only has bit's and piece's of the Sink role implemented in the frameworks while the A2DP role has a full implementation of Source role. Additionally, depending what you actually intend to do with your Bluetooth A2DP sink implementation you will need to implement AVRCP as well - as per the Bluetooth SIG (special interest group), there are inter-connectivity requirements between Bluetooth devices that will lead to major issues if you implement sink role, without AVRCP "remote control target device" and "remote control control device", as the sink role ATT commands from Bluetooth over A2DP (or any Bluetooth service/profile) execute certain handshakes during the service discovery process, when the associated gateway ( the connecting device ), executes a capabilities request the A2DP service is expected to implement i/o capabilities for Start Stop commands, and possibly skip/track advance commands.
Additional to all of this, when implementing A2DP you will need to choose whether you will be handling PCM streams or AAC streams. If you are handling AAC streams (or DRM protected PCM streams for that matter, which anything like Pandora, spotify, etc. uses), you need to implement the SBC Encoder or Decoder appropriate to you implementation, else all you will have is a bunch of encrypted data. Also, be sure to implement the bitrate at the appropriate speed for your devices AudioManager implementation, some phones use 48,000hZ and some us 44,100hZ, this is important if you want high quality audio as generally most PCM A2DP implementations that are utilizing Surround Sound 7.1+ will require 48,000hZ as well as AAC encoding/decoding.
I hope this provides you with some insight.
https://android-review.googlesource.com/#/c/98161/ implements A2DP Sink. It works on Nexus 5. You can try it.
This is actually what I'm trying to do for a long time...
The reason you can't find the configuration file is because Google replaced the Bluetooth stack from BlueZ with a new stack built by Google and Broadcom. The new stack uses a different configuration file, which I don't know how to tinker with.
If you are serious about it, the closest thing I found to start with is the official introduction for the bluetooth framework on Android:
https://source.android.com/devices/bluetooth.html
There are lots of changes happened in Android OS over the time.
As of Android O (Android 8.*), sink profiles are partially supported by Google. Such as Audio sink will easily work if enabled in profile. It looks like the higher layer of BT is kind of implementation complete at the framework, which is in the form of App i.e. packages/app/Bluetooth (with some bugs but still works). But all profiles are not completed at framework lower layer via HAL interfaces which is btif framework (such as btif_rc.cpp, etc which you can look at Android source) and which is the replacement to older Bluez stack by Google.
As I said BT sink is partially implemented and is in work in progress state. BT sink such as Audio will easily work if enabled as sink profile but not all such as AVRCP will not work. At present, I saw the issue with AOSP code that incoming traffic from the remote device to Android works but outgoing traffic from Android to the remote device doesn't work (upon which AVRCP profile works) as remote device object is not maintained in the stack and so JNI calls from app/Bluetooth fails with null device at btif_*.cpp files. For example, send pass-through commands doesn't work.
So, we may see Bluetooth sink profiles working in future.
If you want to explore more check AOSP,
Services at packages/app/Bluetooth/
HAL's at system/bt/btif/

Accessing video stream from Looxcie device

I'm looking to see if there's a way to get the audio and video streaming in from the bluetooth connection in an android app that I'd create.
http://www.looxcie.com/index.php/companion-apps/looxciecam
Looxcie has their own app which saves their video to a file, but I'd want access to the raw video stream using MediaRecorder if possible.
Thanks!
The Looxcie device lacks in technical information that could help a developer to use it in a custom application. My advice is that first to check what Bluetooth profiles are available on that device and maybe there is VDP - Video Distribution Profile, A2DP - Advanced Audio Distribution Profile and AVRCP - Audio/Video Remote Control Profile.
But from what I know on Android you have only A2DP and no VDP. So, probably they send data over SPP - Serial Port Profile and reconstruct it on Android. If they work this way, is nothing you can do, it's like a proprietary format. You could ask Looxcie for more information about the device, they look like a friendly/enthusiastic open company and maybe they will share information.
A trick you could make is use that file feature, and have it like a virtual file that goes into your MediaRecorder. Again it may not work, it's possible that they don't write all the time, but periodically put chunks of data.

Android background app

Is it possible for an android app running in the background to access the sound being produced by a media player (to be used for recording to another file, streaming etc)? Will the app read from the sound port? How will this work if it is possible? Any existing apps?
Appreciate any help/pointers.
No.
Or more precisely: on an unmodified Android device there is no audio loop back or pass through functionality.
http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html lists all the audio sources available to the MediaRecorder and AudioRecorder classes.
Now, if you are willing to write a device driver and have the capacity to root the target device and load your driver on it you could possibly implement a loop back driver, a pass through driver or (much more interesting) an alternative audio output device that would perform your streaming/recording and then forward to the regular audio output.
To caputre a Sound-Output form the Android Device, you can use the MediaRecorder-class, like shown in this tutorial.
But recording a Sound-File, which already exists (on the SDcard ore something) doesn't make any sense to me.

Categories

Resources