Exemple #1
0
        public async void PlayStream(string text)
        {
            var bufferSize = AudioTrack.GetMinBufferSize(16000, ChannelOut.Stereo, Encoding.Pcm16bit);

            ////new System.Threading.Thread(delegate (object o)
            ////  {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Helpers.URL.NuanceURL);

            req.Method = "POST";
            req.KeepAlive = false;
            req.Accept = "audio/x-wav;codec=pcm;bit=16;rate=16000";
            // req.Accept = "audio/x-wav";
            req.ContentType = "text/plain";

            var streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
            streamOut.Write(text);
            streamOut.Close();

            audioTrack = new AudioTrack(
                Android.Media.Stream.Music,
                8000,
                ChannelOut.Stereo,
                Android.Media.Encoding.Pcm16bit,
                bufferSize,
                AudioTrackMode.Stream);

            using (var stream = req.GetResponse().GetResponseStream())
            {
                byte[] buffer = new byte[65536];
                int read;
                audioTrack.Play();
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    var pos = ms.Position;
                    ms.Position = ms.Length;
                    ms.Write(buffer, 0, read);
                    ms.Position = pos;
                }

            }

            // }).Start();


            //while (ms.Length < bufferSize * 5)
            //{
            //    System.Threading.Thread.Sleep(1000);
            //}

            var br = new BinaryReader(ms);
            await PlayAudioTrack(br.ReadBytes((int)ms.Length));

            ////ms.Position = 0;
            ////var br = new BinaryReader(ms);

            ////PlayAudioTrack(br.ReadBytes((int)ms.Length));

            audioTrack.Release();

        }
        private void TransferSend()
        {
            CurrentTransferDataLeft = CurrentTransfer.FileLength;

            var at = new AudioTrack(Android.Media.Stream.Music,
                44100,
                ChannelConfiguration.Mono,
                Android.Media.Encoding.Pcm16bit,
                0x1000,
                AudioTrackMode.Stream);
            at.Play();

            // Polarity calibration header
            for (int i = 0; i < 200; i++)
            {
                WriteValue(at, BitConverter.GetBytes(-0x7fff), Bitlen);
                WriteValue(at, BitConverter.GetBytes(0x7fff), Bitlen * 3);
            }

            // Lead-in
            for (int i = 0; i < 20; i++)
                PutByte(at, 0xff);

            // Sync sequence
            for (byte i = 0x08; i >= 0x04; i--)
                PutByte(at, i);

            // Data
            byte[] buf = new byte[1];
            while (CurrentTransferDataLeft > 0 && !AbortCurrentTransfer)
            {
                CurrentTransfer.GetData(buf);
                PutByte(at, buf[0]);

                CurrentTransferDataLeft--;
            }

            at.Stop();
            at.Release();
            at.Dispose();

            CurrentTransfer.Close();

            ParentConnection.RaiseFileTransferEnded(CurrentTransfer, true, AbortCurrentTransfer);
        }
        public void PlaySound(int samplingRate, byte[] pcmData)
        {
            if (previousAudioTrack != null)
            {
                previousAudioTrack.Stop();
                previousAudioTrack.Release();
            }

            AudioTrack audioTrack = new AudioTrack(Stream.Music,
                                                   samplingRate,
                                                   ChannelOut.Mono,
                                                   Android.Media.Encoding.Pcm16bit,
                                                   pcmData.Length * sizeof(short),
                                                   AudioTrackMode.Static);

            audioTrack.Write(pcmData, 0, pcmData.Length);
            audioTrack.Play();

            previousAudioTrack = audioTrack;
        }
		protected async Task PlayAudioTrackAsync ()
		{
			audioTrack = new AudioTrack (
                // Stream type
				Android.Media.Stream.Music,
                // Frequency
				11025,
                // Mono or stereo
				ChannelOut.Mono,
                // Audio encoding
				Android.Media.Encoding.Pcm16bit,
                // Length of the audio clip.
				buffer.Length,
                // Mode. Stream or static.
				AudioTrackMode.Stream);

			audioTrack.Play ();

			await audioTrack.WriteAsync (buffer, 0, buffer.Length);
		}
        protected void PlayAudioTrack()
        {
            audioTrack = new AudioTrack (
                // Stream type
                Android.Media.Stream.Music,
                // Frequency
                11025,
                // Mono or stereo
                ChannelConfiguration.Mono,
                // Audio encoding
                Android.Media.Encoding.Pcm16bit,
                // Length of the audio clip.
                buffer.Length,
                // Mode. Stream or static.
                AudioTrackMode.Stream);

            audioTrack.Play ();

            audioTrack.Write (buffer, 0, buffer.Length);
        }
Exemple #6
0
        protected Task PlayAudioTrackAsync()
        {
            return new Task(() => {
            audioTrack = new AudioTrack (
                // Stream type
                Android.Media.Stream.Music,
                // Frequency
                44100,
                // Mono or stereo
                ChannelConfiguration.Mono,
                // Audio encoding
                Android.Media.Encoding.Pcm16bit,
                // Length of the audio clip.
                RecordAudio.fullAudioBuffer.Count,
                // Mode. Stream or static.
                AudioTrackMode.Stream);

            audioTrack.Play ();

                audioTrack.Write(RecordAudio.fullAudioBuffer.ToArray(), 0, RecordAudio.fullAudioBuffer.Count);
            });
        }
        private void PlayMetronome()
        {
            const int amp = 10000;
            double twopi = 8*Math.Atan(1.0);
            const double fr = 440.0;
            double ph = 0.0;

            int lastBpm = metronCurrentBpm;

            Animation anim = new AlphaAnimation(0.5f, 1.0f);
            anim.Duration = (60000/metronCurrentBpm)/2;
            anim.StartOffset = 0;
            anim.RepeatMode = RepeatMode.Reverse;
            anim.RepeatCount = Animation.Infinite;
            RunOnUiThread(() => { metronBpmText.StartAnimation(anim); });

            metronAudioTrack = new AudioTrack(Android.Media.Stream.Music, 44100, ChannelOut.Mono,
                Encoding.Pcm16bit, metronBuffSize, AudioTrackMode.Stream);

            metronAudioTrack.Play();

            while (reading)
            {
                Thread.Sleep(60000/metronCurrentBpm);

                if (lastBpm != metronCurrentBpm)
                {
                    // The BPM has changed - change the animation speed!
                    lastBpm = metronCurrentBpm;
                    anim.Duration = (60000/metronCurrentBpm)/2;

                    RunOnUiThread(() =>
                    {
                        metronBpmText.ClearAnimation();
                        metronBpmText.StartAnimation(anim);
                    });
                }

                for (int i = 0; i < metronAudioBuffer.Length; i++)
                {
                    metronAudioBuffer[i] = (short) (amp*Math.Sin(ph));
                    ph += twopi*fr/44100;
                }

                metronAudioTrack.Write(metronAudioBuffer, 0, metronAudioBuffer.Length);
            }

            metronAudioTrack.Stop();
            metronAudioTrack.Release();

            RunOnUiThread(() => { metronBpmText.ClearAnimation(); });
        }
Exemple #8
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button buttonRec = FindViewById<Button> (Resource.Id.myButton);
            Button buttonPlay = FindViewById<Button> (Resource.Id.btnPlay);

            ar = findAudioRecord ();
            audioBuffer = new Int16[bufferSize];
            //ar.Release ();

            buttonRec.Click += delegate {

                ar.StartRecording();
                while (true) {
                    try
                    {
                        // Keep reading the buffer
                        //while there is audio input.
                        ar.Read(
                            audioBuffer,
                            0,
                            audioBuffer.Length);

                        if(count++ > audioBuffer.Length)
                        {
                            ar.Stop();
                            break;
                        }
                        // Write out the audio file.
                    }
                    catch (Exception ex)
                    {
                        Console.Out.WriteLine(ex.Message);
                        break;
                    }
                }
            };

            buttonPlay.Click += (sender, e) =>
            {
                int minimumBufferSize = AudioTrack.GetMinBufferSize(ar.SampleRate, ChannelOut.Mono, Android.Media.Encoding.Pcm16bit);

                 audioTrack = new AudioTrack(
                    // Stream type
                    Android.Media.Stream.Music,
                    // Frequency
                    ar.SampleRate,
                    // Mono or stereo
                    ChannelConfiguration.Mono,
                    // Audio encoding
                    Android.Media.Encoding.Pcm16bit,
                    // Length of the audio clip.
                    (minimumBufferSize < audioBuffer.Length ? audioBuffer.Length : minimumBufferSize),
                    // Mode. Stream or static.
                    AudioTrackMode.Static);

                audioTrack.Play();
                audioTrack.Write(audioBuffer, 0, audioBuffer.Length);
            };
        }
        private void Click_Play(object sender, EventArgs e)
        {
            if (_isPlaying)
            {          
                _audioTrack.Stop();
                _isPlaying = false;
                _play.SetImageResource(Resource.Drawable.play);
                return;
            }

            _play.SetImageResource(Resource.Drawable.stop);

            var byteArr = _audioData.ToArray();

            int sampleRate = 0;
            Android.Media.Encoding audioFormat = Android.Media.Encoding.Pcm16bit;
            ChannelOut channelConfig = ChannelOut.Stereo;
            int bufferLength = 0;
            _audioTrack = AudioHelper.FindAudioTrack(ref sampleRate, ref audioFormat, ref channelConfig, ref bufferLength);

            _isPlaying = true;

            (new TaskFactory()).StartNew(() =>
                {
                    _audioTrack.Play();
                    _audioTrack.Write(byteArr, 0, byteArr.Length);
                    _activity.RunOnUiThread(() =>
                        {
                            _isPlaying = false;
                            _play.SetImageResource(Resource.Drawable.play);
                        });
                });
        }