Encode audio from iphone mic into gsm - iphone

I'm using monotouch to create iphone applications and I need to encode audio that I receive from the mic into a gsm file.
I have already encoded audio into wav, but now, for more specific needs, I need to record it into GSM. If someone could tell me or show me some doc that explains either how to encode from mic into gsm or how to convert wav into gsm that would be awesome.
Ty,
Axel
--- UPDATE ---
There's an entry for MicrosoftGSM in MonoTouch.AudioToolbox.AudioFormatType, yet I get a 1718449215 OSStatus error. I guess that the reason is that my other arguments aren't corrent. Tough I don't know the specification for saving as GSM. Here's my not working code:
//set up the NSObject Array of values that will be combined with the keys to make the NSDictionary
NSObject[] values = new NSObject[]
{
NSNumber.FromFloat (44100.0f), //Sample Rate
NSNumber.FromInt32 ((int)MonoTouch.AudioToolbox.AudioFormatType.MicrosoftGSM),
NSNumber.FromInt32(2),
NSNumber.FromInt32((int)AVAudioQuality.High),
};
//Set up the NSObject Array of keys that will be combined with the values to make the NSDictionary
NSObject[] keys = new NSObject[]
{
AVAudioSettings.AVSampleRateKey,
AVAudioSettings.AVFormatIDKey,
AVAudioSettings.AVNumberOfChannelsKey,
AVAudioSettings.AVEncoderAudioQualityKey,
};
//Set Settings with the Values and Keys to create the NSDictionary
settings = NSDictionary.FromObjectsAndKeys (values, keys);

Bad news, there is no built in way to use AVAudioRecorder to record GSM audio files
The only supported audio recording formats are
MPEG4AAC
AppleLossless
AppleIMA4
iLBC
ULaw
LinearPCM
Anyways you could setup a webservice and use a third party converter like SoX that can convert the audio for you.
btw if you try to use the recorder using MicrosoftGSM format you will likely to get an OSStatus error 1718449215 which is the representation of kAudioFormatUnsupportedDataFormatError error
Alex

Related

how can i play pcm with AVPlayer

I'm using AVPlayer to play local .mp3 file and audio stream from server.
And i want to play local .pcm file too.
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
, NSUserDomainMask
, YES);
NSString * voiceFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"OutPut.pcm"];
But it didn't work. i got unknown error.
It seems AudioQueue can play .pcm correctly.
But is there a sample way can let AVPlayer direct play .pcm just like .mp3?
Neither .pcm as a file extension or PCM data specifies a readable format. The player cannot recognize an arbitrary data stream. It is certainly capable of reading file formats which contain PCM data, but this PCM representation is missing several things typical audio file formats represent:
Sample Rate
Sample Size
Sample Format
Channel Count
and so on.
You should instead save that PCM data in an audio file format the player supports (e.g. a WAV file).
If you prefer to simply stream PCM audio information and you know the stream format, you can approach that problem using an AudioQueue.

Capturing and manipulating microphone audio with AVCaptureSession?

While there are plenty of tutorials for how to use AVCaptureSession to grab camera data, I can find no information (even on apple's dev network itself) on how to properly handle microphone data.
I have implemented AVCaptureAudioDataOutputSampleBufferDelegate, and I'm getting calls to my delegate, but I have no idea how the contents of the CMSampleBufferRef I get are formatted. Are the contents of the buffer one discrete sample? What are its properties? Where can these properties be set?
Video properties can be set using [AVCaptureVideoDataOutput setVideoSettings:], but there is no corresponding call for AVCaptureAudioDataOutput (no setAudioSettings or anything similar).
They are formatted as LPCM! You can verify this by getting the AudioStreamBasicDescription like so:
CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(sampleBuffer);
const AudioStreamBasicDescription *streamDescription = CMAudioFormatDescriptionGetStreamBasicDescription(formatDescription);
and then checking the stream description’s mFormatId.

microphone input listening on iOS, AVAudioRecorder or something else?

I'm wondering if there is a way to "listen" without recording and display the microphone's input levels?
Apples SpeakHere sample does the record and playback, and am wondering if there could a be a lighter version of just "listening" without actually recording and saving a file.
I use AudioQueues for this purpose. In your callback, get the input level like so:
AudioQueueLevelMeterState meter[NUM_INPUT_CHANNELS];
UInt32 dataSize = sizeof(meter);
AudioQueueGetProperty(aqInput, kAudioQueueProperty_CurrentLevelMeterDB, meter, &dataSize);
// input 'level' is in meter.mAveragePower
And simply don't write the audio into a file.

how to convert byte array or binary data into audio file

I need help in my iphone application, I want to convert byte array or binary data into audio file.Please help me out and tell me any way so i con do it.
If your array is supposed to contain audio data in PCM format, than you can simply add WAVE header at the beginning to get ready to play audio file. Format of header is described here.

MP3 streaming on iOS

I want to use OpenAL to play music in an iOS game. The music files are stored in mp3 format and I want to stream them using a buffer queue. I load audio data into the buffers using AudioFileReadPacketData(). However playing the buffers only gives me noise. It works perfectly for caf files, but not for mp3s. Did I miss some vital step in decoding the file?
Code I use to open the sound file:
- (void) openFile:(NSString*)fileName {
NSBundle *bundle = [NSBundle mainBundle];
CFURLRef url = (CFURLRef)[[NSURL fileURLWithPath:[bundle pathForResource:fileName ofType:#"mp3"]] retain];
AudioFileOpenURL(url, kAudioFileReadPermission, 0, &audioFile);
AudioStreamBasicDescription theFormat;
UInt32 formatSize = sizeof(theFormat);
AudioFileGetProperty(audioFile, kAudioFilePropertyDataFormat, &formatSize, &theFormat);
freq = (ALsizei)theFormat.mSampleRate;
CFRelease(url);
}
Code I use to fill in buffers:
- (void) loadOneChunkIntoBuffer:(ALuint)buffer {
char data[STREAM_BUFFER_SIZE];
UInt32 loadSize = STREAM_BUFFER_SIZE;
AudioStreamPacketDescription packetDesc[STREAM_PACKETS];
UInt32 numPackets = STREAM_PACKETS;
AudioFileReadPacketData(audioFile, NO, &loadSize, packetDesc, packetsLoaded, &numPackets, data);
alBufferData(buffer, AL_FORMAT_STEREO16, data, loadSize, freq);
packetsLoaded += numPackets;
}
Because you're reading bytes of MP3 data and treating them as PCM data.
You almost certainly want AudioFileReadPacketData(). EDIT: Except that still gives you MP3 data; it just gives it in packets and (possibly) parses packet headers.
If you don't require OpenAL, AVAudioPlayer is probably the better way to go (according to the Multimedia Programming Guide, there's also Audio Queue services if you want more control).
If you really need to use OpenAL, according to TN2199 you'll need to convert it to PCM in the native byte order. See oalTouch/Classes/MyOpenALSupport.c for an example of using Extended Audio File Services to do this. Note that TN2199 says the format "must ... not use hardware decompression" — according to the Multimedia Programming Guide, software decoding is supported for everything except HE-AAC since OS 3.0. Also note that software MP3 decoding can use a significant amount of CPU time.
Alternatively, explicitly convert the audio using AudioConverter or (possibly) AudioUnit with kAudioUnitSubType_AUConverter. If you do this, it might be worthwhile decompressing everything once and keeping it in memory to minimize overhead.