Example of using Audio Queue Services

前端 未结 3 425
别跟我提以往
别跟我提以往 2020-12-12 17:21

I am seeking an example of using Audio Queue Services.

I would like to create a sound using a mathematical equation and then hear it.

3条回答
  •  情歌与酒
    2020-12-12 18:27

    This is a version using C# of the same sample from @lucius

        void SetupAudio ()
        {
            AudioSession.Initialize ();
            AudioSession.Category = AudioSessionCategory.MediaPlayback;
    
            sampleRate = AudioSession.CurrentHardwareSampleRate;
            var format = new AudioStreamBasicDescription () {
                SampleRate = sampleRate,
                Format = AudioFormatType.LinearPCM,
                FormatFlags = AudioFormatFlags.LinearPCMIsSignedInteger | AudioFormatFlags.LinearPCMIsPacked,
                BitsPerChannel = 16,
                ChannelsPerFrame = 1,
                BytesPerFrame = 2,
                BytesPerPacket = 2, 
                FramesPerPacket = 1,
            };
    
            var queue = new OutputAudioQueue (format);
            var bufferByteSize = (sampleRate > 16000)? 2176 : 512; // 40.5 Hz : 31.25 Hz 
            var buffers = new AudioQueueBuffer* [numBuffers];
            for (int i = 0; i < numBuffers; i++){
                queue.AllocateBuffer (bufferByteSize, out buffers [i]);
                GenerateTone (buffers [i]);
                queue.EnqueueBuffer (buffers [i], null);
            }
            queue.OutputCompleted += (object sender, OutputCompletedEventArgs e) => {
                queue.EnqueueBuffer (e.UnsafeBuffer, null);
            };
    
            queue.Start ();
            return true;
        }
    

    This is the tone generator:

        void GenerateTone (AudioQueueBuffer *buffer)
        {
            // Make the buffer length a multiple of the wavelength for the output frequency.
            uint sampleCount = buffer->AudioDataBytesCapacity / 2;
            double bufferLength = sampleCount;
            double wavelength = sampleRate / outputFrequency;
            double repetitions = Math.Floor (bufferLength / wavelength);
            if (repetitions > 0) 
                sampleCount = (uint)Math.Round (wavelength * repetitions);
    
            double      x, y;
            double      sd = 1.0 / sampleRate;
            double      amp = 0.9;
            double      max16bit = Int16.MaxValue;
            int i;
            short *p = (short *) buffer->AudioData;
    
            for (i = 0; i < sampleCount; i++) {
                x = i * sd * outputFrequency;
                switch (outputWaveForm) {
                    case WaveForm.Sine: 
                        y = Math.Sin (x * 2.0 * Math.PI);
                        break;
                    case WaveForm.Triangle:
                        x = x % 1.0;
                        if (x < 0.25)
                            y = x * 4.0; // up 0.0 to 1.0
                        else if (x < 0.75)
                            y = (1.0 - x) * 4.0 - 2.0; // down 1.0 to -1.0
                        else 
                            y = (x - 1.0) * 4.0; // up -1.0 to 0.0
                        break;
                    case WaveForm.Sawtooth:
                        y  = 0.8 - (x % 1.0) * 1.8;
                        break;
                    case WaveForm.Square:
                        y = ((x % 1.0) < 0.5)? 0.7: -0.7;
                        break;
                    default: y = 0; break;
                }
                p[i] = (short)(y * max16bit * amp);
            }
            buffer->AudioDataByteSize = sampleCount * 2;
        }
    }
    

    You also want these definitions:

        enum WaveForm {
            Sine, Triangle, Sawtooth, Square
        }
        WaveForm outputWaveForm;
        const float outputFrequency = 220;
    

提交回复
热议问题