Example #1
0
 protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 {
     while (!stoppingToken.IsCancellationRequested)
     {
         using var scope = _serviceScopeFactory.CreateScope();
         var  graphHelper = scope.ServiceProvider.GetRequiredService <IGraphHelper>();
         bool isConnected = graphHelper.IsConnected();
         if (isConnected)
         {
             List <EventHolder> eventHolders = graphHelper.ListCalendarEvents();
             foreach (var eventHolder in eventHolders)
             {
                 Console.WriteLine(eventHolder.Date.ToString("g"));
                 Console.WriteLine(eventHolder.Subject);
                 Console.WriteLine(eventHolder.Category);
                 using SoundDevice device = SoundDevice.Create(audioSettings);
                 string sound;
                 var    themeToSound = GetSoundsDictionary();
                 if (themeToSound.TryGetValue(eventHolder.Category, out sound))
                 {
                     device.Play($"./Assets/{sound}.wav");
                 }
             }
         }
         else
         {
             _logger.LogInformation("It's not yet connected...");
         }
         await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
     }
 }
Example #2
0
        public async Task PlaySound(Stream stream, CancellationToken ct = default)
        {
            SoundConnectionSettings settings = new SoundConnectionSettings();

            using SoundDevice device = SoundDevice.Create(settings);
            device.PlaybackVolume    = 80;
            device.Play(stream);
        }
Example #3
0
        static void Main(string[] args)
        {
            SoundConnectionSettings settings = new SoundConnectionSettings();

            using SoundDevice device = SoundDevice.Create(settings);

            Console.WriteLine("Recording...");
            device.Record(10, "/home/pi/record.wav");

            Console.WriteLine("Playing...");
            device.Play("/home/pi/record.wav");
        }
Example #4
0
        static void Main(string[] args)
        {
            SoundConnectionSettings settings = new SoundConnectionSettings();

            using SoundDevice device = SoundDevice.Create(settings);

            string path = Directory.GetCurrentDirectory();

            Console.WriteLine("Recording...");
            device.Record(10, $"{path}/record.wav");

            Console.WriteLine("Playing...");
            device.Play($"{path}/record.wav");
        }
Example #5
0
        /// <summary>
        /// Play a sound, supported only WAV and MP3
        /// </summary>
        /// <param name="fileName"></param>
        public void Play(string fileName)
        {
            IsPlaying = true;
            new Thread(() =>
            {
                SoundConnectionSettings settings = new SoundConnectionSettings();
                try
                {
                    using (SoundDevice device = SoundDevice.Create(settings))
                    {
                        // Check the file type
                        if (fileName.Substring(fileName.Length - 3).ToLower() == "mp3")
                        {
                            // open the mp3 file.
                            MP3Stream stream = new MP3Stream(fileName);

                            // Create the buffer.
                            byte[] buffer = new byte[4096];
                            // read the entire mp3 file.
                            int bytesReturned  = 1;
                            int totalBytesRead = 0;
                            Stream fsWav       = new MemoryStream();

                            while (bytesReturned > 0)
                            {
                                bytesReturned = stream.Read(buffer, 0, buffer.Length);
                                fsWav.Write(buffer, 0, bytesReturned);
                                totalBytesRead += bytesReturned;
                            }
                            // close the stream after we're done with it.
                            stream.Close();
                            stream.Dispose();
                            fsWav.Position = 0;

                            WavHeaderChunk chunk = new WavHeaderChunk
                            {
                                ChunkId   = new[] { 'R', 'I', 'F', 'F' },
                                ChunkSize = (uint)totalBytesRead + 36
                            };
                            WavHeaderChunk subChunk1 = new WavHeaderChunk
                            {
                                ChunkId   = new[] { 'f', 'm', 't', ' ' },
                                ChunkSize = 16
                            };
                            WavHeaderChunk subChunk2 = new WavHeaderChunk
                            {
                                ChunkId   = new[] { 'd', 'a', 't', 'a' },
                                ChunkSize = (uint)totalBytesRead
                            };

                            WavHeader header = new WavHeader
                            {
                                Chunk         = chunk,
                                Format        = new[] { 'W', 'A', 'V', 'E' },
                                SubChunk1     = subChunk1,
                                AudioFormat   = 1,
                                NumChannels   = (ushort)(stream.Format == SoundFormat.Pcm16BitMono ? 1 : 2),
                                SampleRate    = (uint)stream.Frequency,
                                ByteRate      = (uint)(16 * stream.Frequency * (stream.Format == SoundFormat.Pcm16BitMono ? 1 : 2) / 8),
                                BlockAlign    = (ushort)((stream.Format == SoundFormat.Pcm16BitMono ? 1 : 2) * 2),
                                BitsPerSample = 16,
                                SubChunk2     = subChunk2
                            };

                            device.WriteWavHeader(fsWav, header);
                            fsWav.Position = 0;
                            device.Play(fsWav);
                            fsWav.Close();
                            fsWav.Dispose();
                        }
                        else
                        {
                            device.Play(fileName);
                        }
                        IsPlaying = false;
                    }
                }
                catch
                {
                    IsPlaying = false;
                }
            }).Start();
        }