Programmatically Writing PCM WAV Data in Android - android

I'm looking for a way to programmatically save an array of shorts as PCM data. I know that this should be possible, but I haven't found a very easy way to do this on Android.
Essentially, I'm taking voltage data, and I want to save it in PCM format. My function looks something like this:
public void audifySignal(short[] signal) {
// Create a WAV file from the incoming signal
}
Any suggestions would be awesome, or even references. Seems like the audio APIs built in to android are more geared for directly recording from the mic, and not so much for lower level signal processing type work (at least for saving raw data to a file). I'd also like to avoid having to manually write the PCM file headers and what not...
Thanks!

Sam, I dunno about Android-specific libraries, but I'll go ahead and say this:
Raw PCM data is pretty straight forward. It's generally just sequential data. Maybe you need to understand the WAV format in order to understand what PCM is and how it works.
WAV is fairly widely used as a container for uncompressed audio. Gaining an understanding of how the WAV file contains the data will cast a fair bit of light on how raw digital audio works in general.
This page helped me a fair bit:
http://www.sonicspot.com/guide/wavefiles.html
Interestingly you can more or less fire ANY data at a sound-card and it'll play it. It'll probably sound crazy to us humans as the sound card doesn't care about whether it sounds garbled or not.
Whether it sounds pleasing to the ear or not will depend upon whether you've provided the correct sample size, number of channels, frequency and some PCM data that conforms to the former.
See you can't "detect" the sample size, the number of channels or the correct frequency from the raw PCM data itself. You have to store this crucial data ALONG with the PCM data so that other pieces of software can let the sound-card know how to handle your PCM data.
That's where the WAV container format comes in.
There are other formats but WAV is pretty commonplace and it's therefore a good place to start.
Cheers
Tristen

You can use Android's AudioTrack to write raw PCM data that you want to get played, but it's not a function to generate the wav file or so.

Related

Real time audio analysis Android

I've got a rather complicated problem that I need to solve at work. It's pretty far out of my remit of "Android App Developer" - I would class it as a very specialized audio engineering problem.
I am tasked with developing an application, which needs to be able to stream either a local audio file or audio from streaming service apps such as, but not limited to, Spotify, to another device over Bluetooth.
In addition, the app needs to be able to estimate the BPM of the streamed audio (it is assumed all audio will be musical) and use this BPM value to control the playback speed of a lighting sequence.
This question is about how to estimate the BPM of the streamed music.
For the case where the audio file is local, I can think of some solutions for this, such as hardcoding the BPM into the app, in a map against the audio resources URL.
I have also investigated and experimented with "static" library (aubio) than can estimate BPM from an audio file, but not on the fly. It assumes .wav format. This won't be sufficient for what we are trying to achieve here.
However, given the requirement for streaming external audio from streaming service apps such as Spotify, a static analysis solution is pointless as the solution wouldn't work for the streaming service case, and the streaming service case solution will work for both cases.
Therefore, I have come to the conclusion that somehow, I need to on the fly analyze the streamed audio, perhaps with FFT or peak detection algorithms.
This question isn't about the actual BPM estimation algorithm itself (or the implementation details of how I would get there) and is about the basic starting point of such a solution:
How might I go about getting A) the raw bytes of streamed audio for both the local file case and the external streaming service app case and B) how might I process these bytes into a data structure representing the audio stream in a way amenable to running audio analysis algorithms on it.
I realize this is very open ended, quite vague question, but this is so far out of my comfort zone I've no idea how to even formulate a more coherent question.
Any help would be greatly appreciated!
I'd start by creating some separate, more tightly defined questions for the different pieces. For example, ask how to get access to the raw bytes when streaming local file, or streaming URL-sourced audio. Android has some nice support for streaming, including the ability to stream PCM, so I'd be pretty surprised if getting a hook for access to the byte stream were not possible.
Once you have a hooking point, to convert the bytes to "something useful" I'd look at using the audio format to tell you how to read the incoming bytes. The format should tell you how many channels (mono or stereo), the encoding (e.g., signed PCM is common, might be normalized floats), the number of bits per value (16 is common) and the order of the bytes (big-endian vs little endian).
I know that there are posts that will explain how to convert the raw audio bytes to PCM values based on this info, including some on stackoverflow. They should be reachable via search. I think signed normalized floats is the most common data representation used for processing audio signals.

Decoding only some PCM bytes at a time from an mp3 file

How do I decode something on the order of a 1000 bytes of PCM audio from an mp3 file, without decoding the whole thing?
I need to mix together four to six tracks, to one, so that they're played simultaneously on an AudioTrack in the Android app.
This can be done if I can get a stream of PCM samples, and so simple add the decoded tracks together (and maybe adjust for clipping and volume), and then write them to an AudioTrack buffer.
That part is simple.
But how do I decode the individual mp3 files, to inputstreams I can get byte arrays from? I've found something called JLayer, but its not quite clear to me how to do this.
I'd rather avoid doing it in C++ (I'm a bit rusty, and my team doesn't like it), though if that's needed I can do it. Though I'd need a short example of how get say 240 decoded bytes from a file via mpg123, or other such libraries.
Any help is appreciated.
The smallest you can do is 576 samples, which is the smallest MP3 frame size. However, most MP3 streams use the bit reservoir meaning you likely have to decode frames around the frame you want to decode as well.
Complicating things further, bare MP3 streams don't have any internal timestamping, so if you want to drop accurately in the middle of a file, you have to decode up until that point. (MP3 frame headers don't contain byte lengths, so you can't just skim frame headers accurately.) You can try to needle-drop into the middle of the file based on byte length, but this isn't an accurate way of seeking and can be off by several seconds, even for CBR. For VBR, it's all over the place.
It sounds like all you need to do is have a stream decoder, so that the decoding happens as playback is occurring. I'm no Android developer, but it seems you can just use AudioTrack from the framework, in streaming mode. https://developer.android.com/reference/android/media/AudioTrack.html And then the MediaCodec to actually do the decoding. https://developer.android.com/reference/android/media/MediaCodec.html Android devices support MP3, so you don't need to do anything else.

android get frequencies peaks from mp3

im creating android audio player and i want to add the possibility of drawing live chart with frequencies amplitudes (i dont know how it is named exactly). I know how to do it with FFT. But FFT can be applied on raw data only. My player gets mp3's. So how to extract frequencies and their amplitudes from mp3 file?
I see single possible decision: to write own native library that would decompress mp3 file (AFAIK android hasn't tools for decompressing mp3s) and after that to create spectrogram using FFT.
But this method has one essential minus - it needs large quantity of time. Converting mp3 to wav and applying FFT on full raw data consumes many time. Obviously better to do it on-the-go during playback. But i dont know how to do that.
Are there any other ways to achieve my goal?
P.S. I need something like this
This goal can be achived by using mp3 decoders, for example, mpg123. It can be build for android and gives possibility for accessing to raw song data. And i've found this project which use different mp3 decoders.

How do I read time samples of audio files in Android?

I want to write an app on Android to record snoring sounds of a sleeper and analyze it afterwards (i.e., not in real-time) for signs of a medical condition called obstructive sleep apnea.
The Android devices I've experimented with have voice recorders that produce a file format called .3ga. I want to programmatically read in the audio file and look at the amplitude for each individual time-sample. Then I can analyze that for patterns. Would this be easier if I converted this to a different format, e.g., MP3, and if so how can I do that programmatically?
I did a Google search on this and most of the hits seemed to be related to audio recording or playback which are unrelated to what I'm trying to do. I haven't coded anything yet because I don't know how to get started.
You are looking to do sample-based analysis on a raw audio signal, but the formats you mention are compressed. You will need to either deal with raw samples directly, or decompress the audio and then analyze.
Since you said you can do this work after-the-fact, why not upload to a server and analyze there?

3GP/AMR mix/merge tracks

Is there an easy way to merge 2 3gp (amr) audio files into a single audio file?
I need them to be synchronous/over top of each other not one after the other. I am using android to do this. I have heard somewhere that for some audio formats you can simply add the bytes (being careful that you dont get a too high or too low result). Is this true with the 3gp/amr format on android?
Android only allows playback/recording of 3GP/AMR files. To mix audio you will need the decoded PCM data. This means you have to decode both streams mix (this is indeed adding + normalizing) and then playback.
The bad side - there no way to get access to the build in AMR decoder which allows you to decode without playback.
So ... no easy way.

Categories

Resources