alexbw / novocaine

Painless high-performance audio on iOS and Mac OS X
http://alexbw.github.com/novocaine/
MIT License
2.23k stars 274 forks source link

AudioFileWriter not working #59

Closed idroxid closed 11 years ago

idroxid commented 11 years ago

Hi, I'm trying to use novocaine in order to record some audio from the mic and to write this audio to a m4a file.

The file is created, but it's not readable. The contents of the file looks like:

0000 00 00 00 1C 66 74 79 70 4D 34 41 20 00 00 00 00 ....ftypM4A ....
0010 4D 34 41 20 6D 70 34 32 69 73 6F 6D 00 00 00 00 M4A mp42isom....
0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0030 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0050 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0060 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0070 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0080 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0090 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0130 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0140 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0150 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0160 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................

There is a lot of NULL values, and the file doesn't play in any player.

My first idea was the microphone was not working, but I tried to copy/paste your example (the one who shows the decibel levels) and I got a lot of values (from -50 to -10). I assume this is what we expect. So, the microphone is working.

Then, I tried to run your sample project itself and uncommented the code for:// AUDIO FILE WRITING YEAH! and got the same problem, the MyRecording.m4a is created, but doesn't play in any player.


// init
self.ringBuffer = new RingBuffer(32768, 2);
self.audioManager = [Novocaine audioManager];
[self.audioManager play];

...
self.fileWriter = [[AudioFileWriter alloc]
                       initWithAudioFileURL:outputFileURL
                       samplingRate:self.audioManager.samplingRate
                       numChannels:self.audioManager.numInputChannels];
...

__weak XViewController * wself = self;
self.audioManager.inputBlock = ^(float *data, UInt32 numFrames, UInt32 numChannels) {
    // if I try to show the data here, I have values...
    [wself.fileWriter writeNewAudio:data numFrames:numFrames numChannels:numChannels];
};

I'm using Xcode 5 DP5, an iPhone5 on iOS7 beta-5 and a iPad mini on iOS6.1.3. The problem is the same on each device.

Ho, yes, and the log when I init the audio is:

AudioRoute: Speaker
Input available? 1
AudioRoute: ReceiverAndMicrophone
Input available? 1
We've got 1 input channels
We've got 1 output channels
Current sampling rate: 44100.000000

If you can help, it would be very appreciated :)

idroxid commented 11 years ago

I just run the sample code (File writing) with Xcode 4 and a iPad mini 6.3.1, same problem.

ndonald2 commented 11 years ago

Hi @idroxid

I think the issue is that the file being written is never finalized, which only happens when calling stop on the file writer or when the file writer instance is deallocated.

Since your code appears to be writing directly to the file writer, like in the example, at some point you need to either explicitly call stop or dispose of the file writer instance to get it to actually finalize the file, at which point you won't be able to write to it anymore. That behavior is a bit weird, but that's how it works when you write directly to the file writer.

The other option would be to write data from the input block to the ring buffer, then implement a writer callback block on the file writer that reads new data from the ring buffer and writes it to the file. In that configuration you can use record and stop on the file writer instance to perform the recording.

@alexbw if any of this is wrong feel free to correct me.

alexbw commented 11 years ago

Sorry I've been so silent on the issue. The audio file writers and readers are the least supported aspect of Novocaine, originally included as a bit of sugar. It makes sense to me that they're the part of the codebase getting the most comment.

Nick's comments sound correct to me.

On Mon, Sep 9, 2013 at 2:27 PM, Nick D. notifications@github.com wrote:

Hi @idroxid https://github.com/idroxid

I think the issue is that the file being written is never finalized, which only happens when calling stop on the file writer or when the file writer instance is deallocated.

Since your code appears to be writing directly to the file writer, like in the example, at some point you need to either explicitly call stop or dispose of the file writer instance to get it to actually finalize the file, at which point you won't be able to write to it anymore. That behavior is a bit weird, but that's how it works when you write directly to the file writer.

The other option would be to write data from the input block to the ring buffer, then implement a writer callback block on the file writer that reads new data from the ring buffer and writes it to the file. In that configuration you can use record and stop on the file writer instance to perform the recording.

@alexbw https://github.com/alexbw if any of this is wrong feel free to correct me.

— Reply to this email directly or view it on GitHubhttps://github.com/alexbw/novocaine/issues/59#issuecomment-24102574 .

idroxid commented 11 years ago

Hi @ndonald2 Thank you for your message ! It works :) So, I just did add the declaration of the stop method to AudioFileWriter.h and just call

[self.fileWriter stop];

when I need it. It's woking perfectly ! Thank you @ndonald2 , and thank you @alexbw for this awesome lib !!!

rajendersha commented 9 years ago

Hi I am able to record and save audio file to documents directory, but when trying to play the file is not playing on iOS Devices, however its playing in VLC and iTunes.

Can anyone suggest me what I am doing wrong ?

Thanks,