Exemplo n.º 1
0
        void prepareAudioUnit()
        {
            // creating an AudioComponentDescription of the RemoteIO AudioUnit
            AudioComponentDescription cd = new AudioComponentDescription()
            {
                componentType         = AudioComponentDescription.AudioComponentType.kAudioUnitType_Output,
                componentSubType      = AudioComponentDescription.AudioComponentSubType.kAudioUnitSubType_RemoteIO,
                componentManufacturer = AudioComponentDescription.AudioComponentManufacturerType.kAudioUnitManufacturer_Apple,
                componentFlags        = 0,
                componentFlagsMask    = 0
            };

            // Getting AudioComponent using the audio component description
            _audioComponent = AudioComponent.FindComponent(cd);

            // creating an audio unit instance
            _audioUnit = AudioUnit.CreateInstance(_audioComponent);

            // setting audio format
            _audioUnit.SetAudioFormat(_dstFormat,
                                      AudioUnit.AudioUnitScopeType.kAudioUnitScope_Input,
                                      0 // Remote Output
                                      );

            // setting callback method
            _audioUnit.RenderCallback += new EventHandler <AudioUnitEventArgs>(_audioUnit_RenderCallback);

            _audioUnit.Initialize();
        }
Exemplo n.º 2
0
 public override bool initializeAudio()
 {
     try {
         recorder.Initialize();
         recorder.Start();
         return(true);
     } catch {
     }
     return(false);
 }
Exemplo n.º 3
0
        void prepareAudioUnit()
        {
            // Updated for deprecated AudioSession
            var     session = AVAudioSession.SharedInstance();
            NSError error;

            if (session == null)
            {
                var alert = new UIAlertView("Session error", "Unable to create audio session", null, "Cancel");
                alert.Show();
                alert.Clicked += delegate
                {
                    alert.DismissWithClickedButtonIndex(0, true);
                    return;
                };
            }
            session.SetActive(true);
            session.SetCategory(AVAudioSessionCategory.PlayAndRecord);
            session.SetPreferredIOBufferDuration(0.005, out error);

            // Getting AudioComponent Remote output
            _audioComponent = AudioComponent.FindComponent(AudioTypeOutput.Remote);

            // creating an audio unit instance
            _audioUnit = new AudioUnit(_audioComponent);

            // turning on microphone
            _audioUnit.SetEnableIO(true,
                                   AudioUnitScopeType.Input,
                                   1 // Remote Input
                                   );

            // setting audio format
            _audioUnit.SetAudioFormat(_dstFormat,
                                      AudioUnitScopeType.Input,
                                      0 // Remote Output
                                      );

            var format = AudioStreamBasicDescription.CreateLinearPCM(_sampleRate, bitsPerChannel: 32);

            format.FormatFlags = AudioStreamBasicDescription.AudioFormatFlagsNativeFloat;
            _audioUnit.SetAudioFormat(format, AudioUnitScopeType.Output, 1);

            // setting callback method
            _audioUnit.SetRenderCallback(_audioUnit_RenderCallback, AudioUnitScopeType.Global);

            _audioUnit.Initialize();
            _audioUnit.Start();
        }
        void prepareAudioUnit()
        {
            // AudioSession
            AudioSession.Initialize();
            AudioSession.SetActive(true);
            AudioSession.Category = AudioSessionCategory.PlayAndRecord;
            AudioSession.PreferredHardwareIOBufferDuration = 0.005f;

            // creating an AudioComponentDescription of the RemoteIO AudioUnit
            AudioComponentDescription cd = new AudioComponentDescription()
            {
                componentType         = AudioComponentDescription.AudioComponentType.kAudioUnitType_Output,
                componentSubType      = AudioComponentDescription.AudioComponentSubType.kAudioUnitSubType_RemoteIO,
                componentManufacturer = AudioComponentDescription.AudioComponentManufacturerType.kAudioUnitManufacturer_Apple,
                componentFlags        = 0,
                componentFlagsMask    = 0
            };

            // Getting AudioComponent using the audio component description
            _audioComponent = AudioComponent.FindComponent(cd);

            // creating an audio unit instance
            _audioUnit = AudioUnit.CreateInstance(_audioComponent);

            // turning on microphone
            _audioUnit.SetEnableIO(true,
                                   AudioUnit.AudioUnitScopeType.kAudioUnitScope_Input,
                                   1 // Remote Input
                                   );

            // setting audio format
            _audioUnit.SetAudioFormat(_dstFormat,
                                      AudioUnit.AudioUnitScopeType.kAudioUnitScope_Input,
                                      0 // Remote Output
                                      );
            _audioUnit.SetAudioFormat(AudioUnitUtils.AUCanonicalASBD(_sampleRate, 2),
                                      AudioUnit.AudioUnitScopeType.kAudioUnitScope_Output,
                                      1 // Remote input
                                      );


            // setting callback method
            _audioUnit.RenderCallback += new EventHandler <AudioUnitEventArgs>(_audioUnit_RenderCallback);

            _audioUnit.Initialize();
            _audioUnit.Start();
        }
Exemplo n.º 5
0
        void prepareAudioUnit()
        {
            // Creating AudioComponentDescription instance of RemoteIO Audio Unit
            var cd = new AudioComponentDescription()
            {
                componentType         = AudioComponentDescription.AudioComponentType.kAudioUnitType_Output,
                componentSubType      = AudioComponentDescription.AudioComponentSubType.kAudioUnitSubType_RemoteIO,
                componentManufacturer = AudioComponentDescription.AudioComponentManufacturerType.kAudioUnitManufacturer_Apple,
                componentFlags        = 0,
                componentFlagsMask    = 0
            };

            // Getting AudioComponent from the description
            _component = AudioComponent.FindComponent(cd);

            // Getting Audiounit
            _audioUnit = AudioUnit.CreateInstance(_component);

            // turning on microphone
            _audioUnit.SetEnableIO(true,
                                   AudioUnit.AudioUnitScopeType.kAudioUnitScope_Input,
                                   1 // Remote Input
                                   );

            // setting AudioStreamBasicDescription
            var audioFormat = AudioUnitUtils.AUCanonicalASBD(44100.0, 2);

            _audioUnit.SetAudioFormat(audioFormat,
                                      AudioUnit.AudioUnitScopeType.kAudioUnitScope_Input,
                                      0 // Remote output
                                      );
            _audioUnit.SetAudioFormat(audioFormat,
                                      AudioUnit.AudioUnitScopeType.kAudioUnitScope_Output,
                                      1 // Remote input
                                      );

            // setting callback
            _audioUnit.RenderCallback += new EventHandler <AudioUnitEventArgs>(_audioUnit_RenderCallback);
            // initialize
            _audioUnit.Initialize();
        }
Exemplo n.º 6
0
        void configureAndStartAudioProcessingGraph(AUGraph graph)
        {
            int  result         = 0;
            uint framesPerSlice = 0;

            result = ioUnit.Initialize();
            if (result != 0)
            {
                throw new Exception("Unable to Initialize the I/O unit.  Error code: " + result);
            }

            var status = ioUnit.SetSampleRate(graphSampleRate, AudioUnitScopeType.Output);

            if (status != AudioUnitStatus.NoError)
            {
                throw new Exception("AudioUnitSetProperty (set Sample output stream sample rate).  Error code: " + (int)status);
            }

            framesPerSlice = ioUnit.GetMaximumFramesPerSlice(AudioUnitScopeType.Global);

            samplerUnit.SetSampleRate(graphSampleRate, AudioUnitScopeType.Output);
            samplerUnit.SetMaximumFramesPerSlice(framesPerSlice, AudioUnitScopeType.Global);

            if (graph != null)
            {
                result = (int)graph.Initialize();
                if (result != (int)AUGraphError.OK)
                {
                    throw new Exception("Unable to initialize AUGraph object.  Error code: " + result);
                }

                result = (int)graph.Start();
                if (result != (int)AUGraphError.OK)
                {
                    throw new Exception("Unable to start audio processing graph.  Error code: " + result);
                }

//				TODO: CAShow
                Console.WriteLine(graph);
            }
        }
Exemplo n.º 7
0
        public SoundWAVInstance(SoundWAV sound, bool looped)
            : base(sound)
        {
            this.sound  = sound;
            this.looped = looped;
            State       = SoundStates.Stopped;
            volume      = 1;

            instance = new AudioUnit(sound.audio.component);
            instance.SetAudioFormat(sound.desc, AudioUnitScopeType.Input, 0);
            if (sound.channels == 2)
            {
                switch (sound.bitDepth)
                {
                case 8: instance.RenderCallback += render2Channel8BitCallback; break;

                case 16: instance.RenderCallback += render2Channel16BitCallback; break;

                default: Debug.ThrowError("SoundWAVInstance", "Unsuported WAV bit depth"); break;
                }
            }
            else
            {
                switch (sound.bitDepth)
                {
                case 8: instance.RenderCallback += render1Channel8BitCallback; break;

                case 16: instance.RenderCallback += render1Channel16BitCallback; break;

                default: Debug.ThrowError("SoundWAVInstance", "Unsuported WAV bit depth"); break;
                }
            }
            instance.Initialize();

                        #if iOS
            OS.CurrentApplication.PauseCallback  += Pause;
            OS.CurrentApplication.ResumeCallback += resume;
                        #endif
        }
        void prepareAudioUnit()
        {
            // AudioSession
            AudioSession.Initialize();
            AudioSession.SetActive(true);
            AudioSession.Category = AudioSessionCategory.PlayAndRecord;
            AudioSession.PreferredHardwareIOBufferDuration = 0.005f;

            // Getting AudioComponent Remote output
            _audioComponent = AudioComponent.FindComponent(AudioTypeOutput.Remote);

            // creating an audio unit instance
            _audioUnit = new AudioUnit(_audioComponent);

            // turning on microphone
            _audioUnit.SetEnableIO(true,
                                   AudioUnitScopeType.Input,
                                   1 // Remote Input
                                   );

            // setting audio format
            _audioUnit.SetAudioFormat(_dstFormat,
                                      AudioUnitScopeType.Input,
                                      0 // Remote Output
                                      );

            var format = AudioStreamBasicDescription.CreateLinearPCM(_sampleRate, bitsPerChannel: 32);

            format.FormatFlags = AudioStreamBasicDescription.AudioFormatFlagsAudioUnitCanonical;
            _audioUnit.SetAudioFormat(format, AudioUnitScopeType.Output, 1);

            // setting callback method
            _audioUnit.SetRenderCallback(_audioUnit_RenderCallback, AudioUnitScopeType.Global);

            _audioUnit.Initialize();
            _audioUnit.Start();
        }
Exemplo n.º 9
0
        private void startTalking(UdpClient audioCaller)
        {
            //Stop old recording session

            //Generate new WaveFormat
            //    recorder.WaveFormat = new WaveFormat(16000, 16, 1);
            //    recorder.BufferMilliseconds = 50;
            //    recorder.DataAvailable += SendAudio; //Add event to SendAudio


//			recorder = new InputAudioQueue (playerFormat);
//
//
//			for (int i = 0; i < BUFFERCOUNT; i++) {
//				IntPtr aBUff;
//				//recorder.AllocateBuffer (AUDIOBUFFERSIZE, out aBUff);
//				byteSize = AUDIOBUFFERSIZE * playerFormat.BytesPerPacket;
//				recorder.AllocateBufferWithPacketDescriptors (byteSize, AUDIOBUFFERSIZE, out aBUff);
//				recorder.EnqueueBuffer (aBUff, byteSize, null);
//				Console.WriteLine ("Buffer allocated, enqueueing");
//			}

            //New stuffs

            var inputComponent = AudioComponent.FindNextComponent(
                null,
                new AudioComponentDescription
            {
                ComponentFlags        = 0,
                ComponentFlagsMask    = 0,
                ComponentManufacturer = AudioComponentManufacturerType.Apple,
                ComponentSubType      = (int)AudioTypeOutput.Remote,
                ComponentType         = AudioComponentType.Output
            });

            recorder = inputComponent.CreateAudioUnit();
            recorder.SetEnableIO(true, AudioUnitScopeType.Input, inputBus);
            recorder.SetEnableIO(false, AudioUnitScopeType.Output, outputBus);

            var audioFormat = new AudioStreamBasicDescription
            {
                SampleRate       = Globals.SAMPLERATE,
                Format           = AudioFormatType.LinearPCM,
                FormatFlags      = AudioFormatFlags.IsSignedInteger | AudioFormatFlags.IsPacked,
                FramesPerPacket  = 1,
                ChannelsPerFrame = 1,
                BitsPerChannel   = 16,
                BytesPerPacket   = 2,
                BytesPerFrame    = 2
            };

            recorder.SetAudioFormat(audioFormat, AudioUnitScopeType.Output, inputBus);
            recorder.SetAudioFormat(audioFormat, AudioUnitScopeType.Input, outputBus);

            recorder.SetInputCallback(AudioInputCallBack, AudioUnitScopeType.Global, inputBus);

            // TODO: Disable buffers (requires interop)
            aBuffer = new AudioBuffer
            {
                NumberChannels = 1,
                DataByteSize   = 512 * 2,
                Data           = System.Runtime.InteropServices.Marshal.AllocHGlobal(512 * 2)
            };
            isTalking = true;
            //recorder.InputCompleted += SendAudio;
            //recorder.Start ();

            recorder.Initialize();
            recorder.Start();
        }
Exemplo n.º 10
0
        public SoundWAVInstance(SoundWAV sound, bool looped)
            : base(sound)
        {
            this.sound = sound;
            this.looped = looped;
            State = SoundStates.Stopped;
            volume = 1;

            instance = new AudioUnit(sound.audio.component);
            instance.SetAudioFormat(sound.desc, AudioUnitScopeType.Input, 0);
            if (sound.channels == 2)
            {
                switch (sound.bitDepth)
                {
                    case 8: instance.RenderCallback += render2Channel8BitCallback; break;
                    case 16: instance.RenderCallback += render2Channel16BitCallback; break;
                    default: Debug.ThrowError("SoundWAVInstance", "Unsuported WAV bit depth"); break;
                }
            }
            else
            {
                switch (sound.bitDepth)
                {
                    case 8: instance.RenderCallback += render1Channel8BitCallback; break;
                    case 16: instance.RenderCallback += render1Channel16BitCallback; break;
                    default: Debug.ThrowError("SoundWAVInstance", "Unsuported WAV bit depth"); break;
                }
            }
            instance.Initialize();

            #if iOS
            OS.CurrentApplication.PauseCallback += Pause;
            OS.CurrentApplication.ResumeCallback += resume;
            #endif
        }
Exemplo n.º 11
0
        void prepareAudioUnit()
        {
            // AudioSession
            AudioSession.Initialize();
            AudioSession.SetActive(true);
            AudioSession.Category = AudioSessionCategory.PlayAndRecord;
            AudioSession.PreferredHardwareIOBufferDuration = 0.01f;

            // Creating AudioComponentDescription instance of RemoteIO Audio Unit
            var cd = new AudioComponentDescription()
            {
                componentType         = AudioComponentDescription.AudioComponentType.kAudioUnitType_Output,
                componentSubType      = AudioComponentDescription.AudioComponentSubType.kAudioUnitSubType_RemoteIO,
                componentManufacturer = AudioComponentDescription.AudioComponentManufacturerType.kAudioUnitManufacturer_Apple,
                componentFlags        = 0,
                componentFlagsMask    = 0
            };

            // Getting AudioComponent from the description
            _component = AudioComponent.FindComponent(cd);

            // Getting Audiounit
            _audioUnit = AudioUnit.CreateInstance(_component);

            // turning on microphone
            _audioUnit.SetEnableIO(true,
                                   AudioUnit.AudioUnitScopeType.kAudioUnitScope_Input,
                                   1 // Remote Input
                                   );

            // setting AudioStreamBasicDescription
            int AudioUnitSampleTypeSize             = (MonoTouch.ObjCRuntime.Runtime.Arch == MonoTouch.ObjCRuntime.Arch.SIMULATOR) ? sizeof(float) : sizeof(uint);
            AudioStreamBasicDescription audioFormat = new AudioStreamBasicDescription()
            {
                SampleRate = _sampleRate,
                Format     = AudioFormatType.LinearPCM,
                //kAudioFormatFlagsAudioUnitCanonical = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved | (kAudioUnitSampleFractionBits << kLinearPCMFormatFlagsSampleFractionShift),
                FormatFlags      = (AudioFormatFlags)((int)AudioFormatFlags.IsSignedInteger | (int)AudioFormatFlags.IsPacked | (int)AudioFormatFlags.IsNonInterleaved | (int)(kAudioUnitSampleFractionBits << (int)AudioFormatFlags.LinearPCMSampleFractionShift)),
                ChannelsPerFrame = 2,
                BytesPerPacket   = AudioUnitSampleTypeSize,
                BytesPerFrame    = AudioUnitSampleTypeSize,
                FramesPerPacket  = 1,
                BitsPerChannel   = 8 * AudioUnitSampleTypeSize,
                Reserved         = 0
            };

            _audioUnit.SetAudioFormat(audioFormat,
                                      AudioUnit.AudioUnitScopeType.kAudioUnitScope_Input,
                                      0 // Remote output
                                      );
            _audioUnit.SetAudioFormat(audioFormat,
                                      AudioUnit.AudioUnitScopeType.kAudioUnitScope_Output,
                                      1 // Remote input
                                      );

            // setting callback

            /*
             * if (MonoTouch.ObjCRuntime.Runtime.Arch == MonoTouch.ObjCRuntime.Arch.SIMULATOR)
             *  _audioUnit.RenderCallback += new EventHandler<AudioUnitEventArgs>(simulator_callback);
             * else
             *  _audioUnit.RenderCallback += new EventHandler<AudioUnitEventArgs>(device_callback);
             */
            _audioUnit.RenderCallback += new EventHandler <AudioUnitEventArgs>(_callback);
            // initialize
            _audioUnit.Initialize();
        }