Aufnehmen mit statischem AudioQueue- und Monotouch-Sound

Ich habe ein kleines Programm in MonoTouch geschrieben, um mit einem InputAudioQueue Ton vom Mikrofon meines iPhone 4s aufzunehmen.

Ich speichere die aufgenommenen Daten in einem Array und speise diesen Puffer zur Wiedergabe an meinen Audio-Player (mit OutputAudioQueue).

Bei der Wiedergabe ist es nur ein ruckelnder / statischer Ton. Ich habe versucht, den Puffer vor der Wiedergabe mit Sin-Waves zu füllen, und dann klingt es gut. Ich denke, das Problem liegt bei der Aufnahme, nicht bei der Wiedergabe. Kann mir jemand helfen, zu sehen, was los ist? (Code unten)

public class AQRecorder
{
    private const int CountAudioBuffers = 3;
    private const int AudioBufferLength = 22050;
    private const int SampleRate = 44100;
    private const int BitsPerChannel = 16;
    private const int Channels = 1;
    private const int MaxRecordingTime = 5;
    private AudioStreamBasicDescription audioStreamDescription;
    private InputAudioQueue inputQueue;
    private short[] rawData;
    private int indexNextRawData;

    public AQRecorder ()
    {
        this.audioStreamDescription.Format = AudioFormatType.LinearPCM;
        this.audioStreamDescription.FormatFlags = AudioFormatFlags.LinearPCMIsSignedInteger | 
                                                  AudioFormatFlags.LinearPCMIsPacked;
        this.audioStreamDescription.SampleRate = AQRecorder.SampleRate;
        this.audioStreamDescription.BitsPerChannel = AQRecorder.BitsPerChannel;
        this.audioStreamDescription.ChannelsPerFrame = AQRecorder.Channels;
        this.audioStreamDescription.BytesPerFrame = (AQRecorder.BitsPerChannel / 8) * AQRecorder.Channels;
        this.audioStreamDescription.FramesPerPacket = 1;
        this.audioStreamDescription.BytesPerPacket = audioStreamDescription.BytesPerFrame * audioStreamDescription.FramesPerPacket;
        this.audioStreamDescription.Reserved = 0;
    }

    public void Start ()
    {
        int totalBytesToRecord = this.audioStreamDescription.BytesPerFrame * AQRecorder.SampleRate * AQRecorder.MaxRecordingTime;
        this.rawData = new short[totalBytesToRecord / sizeof(short)];
        this.indexNextRawData = 0;
        this.inputQueue = SetupInputQueue (this.audioStreamDescription);
        this.inputQueue.Start ();
    }

    public void Stop ()
    {
        if (this.inputQueue.IsRunning)
        {
            this.inputQueue.Stop (true);
        }
    }

    public short[] GetData ()
    {
        return this.rawData;;
    }

    private InputAudioQueue SetupInputQueue (AudioStreamBasicDescription audioStreamDescription)
    {
        InputAudioQueue inputQueue = new InputAudioQueue (audioStreamDescription);

        for (int count = 0; count < AQRecorder.CountAudioBuffers; count++)
        {
            IntPtr bufferPointer;
            inputQueue.AllocateBuffer(AQRecorder.AudioBufferLength, out bufferPointer);
            inputQueue.EnqueueBuffer(bufferPointer, AQRecorder.AudioBufferLength, null);
        }
        inputQueue.InputCompleted += HandleInputCompleted;
        return inputQueue;
    }

    private void HandleInputCompleted (object sender, InputCompletedEventArgs e)
    {
        unsafe
        {
            short* shortPtr = (short*)e.IntPtrBuffer;

            for (int count = 0; count < AQRecorder.AudioBufferLength; count += sizeof(short))
            {
                if (indexNextRawData >= this.rawData.Length)
                {
                    this.inputQueue.Stop (true);
                    return;
                }
                this.rawData [indexNextRawData] = *shortPtr;
                indexNextRawData++;
                shortPtr++;
            }
        }
        this.inputQueue.EnqueueBuffer(e.IntPtrBuffer, AQRecorder.AudioBufferLength, null);
    }
}

Antworten auf die Frage(1)

Ihre Antwort auf die Frage