iPhone - stuff "silence" in the beginning of an audio file - iphone

I am using AVAudioRecorder in my app in order to create a .wav file based on user voice input. I want to be able to stuff" silence in the beginning of an audio file for some time before the actual recording is done.
How can I do this using AVAudioRecorder? Can I mention a time for which I want the "silence" to be recorded?
Thanks.

I haven't worked with the AVAudioRecorder in particular. But silence in PCM audio is just zeros as sample values. You could set the encoding of the AVAudioRecorder to PCM, store the file and then edit the file by prepending the desired number of zeros in the beginning. E.g. at 44100 Hz, 8-bit encoding, you'd add 44100 zero-bytes to the beginning of the file for each second of silence you would like to have. Hope this helps as an idea.
Note: Keep the file header of the PCM file intact and edit the "data chunk".

I'm sure there's probably a better way to do it, but how about simply pre-recording the silent stream you want and then concatenating that file with a file of the user generated content before saving out the final version?

Related

ffmpeg audio conversion in flutter

I would like to get data from audio file based on microphone input (both Android and iOS), currently I'm using audioplayers and recordMp3 to record the microphone input. This results in a mp3 file with a local file path. In order to use the audio data, I want an uncompressed format like WAV. Would ffmpeg help with this conversion ? I want to eventually use this data for visualization.
MP3 to WAV
ffmpeg -i input.mp3 output.wav
Note that any encoding artifacts in the MP3 will be included in the WAV.
Piping from ffmpeg to your visualizer
I'm assuming you need WAV/PCM because your visualizer only accepts that format and does not accept MP3. You can create a WAV file as shown in the example above, but if your visualizer accepts a pipe as input you can avoid creating a temporary file:
ffmpeg -i input.mp3 -f wav - | yourvisualizer …
Using ffmpeg for visualization
See examples at How do I turn audio into video (that is, show the waveforms in a video)?

How to programmatically output fragmented mp4 file using Bento4

I want to record video conference. I can receive rtp media from video conferencing server. I want to output fragmented mp4 file format for live streaming. So, how to write a fragmented mp4 file programmatically using Bento4?
MP4Box supports DASH. i supply the following simple example:
MP4Box -dash 4000 -frag 4000 -rap -segment-name test_ input.mp4
'-dash 4000' to segment the input mp4 file into 4000ms chunks
'-frag 4000' since frag = dash, actually segments are not fragmented further.
'-rap' to enforce each segment to start random access points, i.e. at keyframes. In such case the segment duration may differ from 4000ms depending on distribution of key frames.
'-segment-name' to specify the pattern of segments names. So in this case, the segments will be named like this: test_1.m4s, test_2.m4s, ...

How to programmatically combine .wav files?

I would like to play some kind of text-to-speech with only numbers. I can record 10 wav files, but how can I combine them programmatically ?
For instance, the user types 1234, and the text-to-speech combines 1.wav with 2.wav, 3.wav and 4.wav to produce 1234.wav that plays "one two three four".
1) create a new destination sample buffer (you will want to know the sizes).
2) read the samples (e.g. using AudioFile and ExtAudioFile APIs) and write them in sequence to the buffer. You may want to add silence between the files.
It will help if your files are all the same bit depth (the destination bit depth - 16 should be fine) and sample rate.
Alternatively, if you have fixed, known, sample rates and bit depths for all files, you could just save them as raw sample data and be done in much less time because you could simply append the data as is without writing all the extra audio file reading programs.
The open source project wavtools provides a good reference for this sort of work, if you're ok with perl. Otherwise there is a similar question with some java examples.
The simplist common .wav (RIFF) file format just has a 44 byte header in front of raw PCM samples. So, for these simple types of .wav files, you could just try reading the files as raw bytes, removing the 44 byte header from all but the first file, and concatening the samples. Or just play the concatenated samples directly using the Audio Queue API.

copy part of audio file into a new file in iOS

I want to copy a part of the audio file, given the starting and ending point(in terms of time which I'll convert to packets or frames-is the conversion right?), and create a new audio file for the copied snippet. How do I copy?
Please advice.
Regards,
Namratha
For what file format(s)?
For the simplest and common .WAV (RIFF) file format, you can just copy the canonical 44-byte header (after checking to make sure the file is using only this simple format), update with the target file length, and then copy a selected sub-range of bytes (multiply time by sample rate by frame size) from the source file, and append that PCM data to the copied header. Apple's codec does not complain about audio files patched together this way.
For other formats, you might be able to convert them either to a simple WAVE file, or to an array of raw PCM samples of suitable sample rate, data type and endianess, and then do the above.

Problem of converting caf files using libsnd

I m building a voice recording application in iPhone. The recorded file is transfer to linux server and it need to be converted to wav file.
However, when I try to convert caf file using libsnd, it gives an error.
Error : Not able to open input file testfile.caf
For testing I converted some wave file to caf using libsnd and vice versa.
So I think that there is a problem of my recorded file in iphone.
Anyone has got such an experience ?
I hope can someone help me.
Thanks.
If you use AVAudioRecorder, AudioFile, or any of the other Core Audio APIs, you should be able to record directly to a WAV file, and skip the entire conversion process altogether.
But if you need to convert audio files, first check if the CAF file is a valid file. Does it play? Is the header correct? What is the data format? Is it compressed? Does libsnd support the data format?
(The data format is separate from the file format, which is just a container for various bits of data as well as the sample data. The data format could be PCM, or it could be compressed in any format such as MP3, AAC, uLaw, etc.)