Пример #1
0
        /// <summary>
        /// Creates a Wave file by reading all the data from a WaveProvider
        /// BEWARE: the WaveProvider MUST return 0 from its Read method when it is finished,
        /// or the Wave File will grow indefinitely.
        /// </summary>
        /// <param name="filename">The filename to use</param>
        /// <param name="sourceProvider">The source WaveProvider</param>
        public static async Task CreateWaveFileAsync(string filename, IWaveProvider sourceProvider)
        {
            StorageFile fileOperation = await StorageFile.GetFileFromPathAsync(filename);

            Stream fileStream = await fileOperation.OpenStreamForWriteAsync();

            using (var writer = new WaveFileWriterRT(fileStream, sourceProvider.WaveFormat))
            {
                writer.filename = filename;
                long outputLength = 0;
                var  buffer       = new byte[sourceProvider.WaveFormat.AverageBytesPerSecond * 4];
                while (true)
                {
                    int bytesRead = sourceProvider.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        // end of source provider
                        break;
                    }
                    outputLength += bytesRead;
                    // Write will throw exception if WAV file becomes too large
                    writer.Write(buffer, 0, bytesRead);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Creates a Wave file by reading all the data from a WaveProvider
        /// BEWARE: the WaveProvider MUST return 0 from its Read method when it is finished,
        /// or the Wave File will grow indefinitely.
        /// </summary>
        /// <param name="filename">The filename to use</param>
        /// <param name="sourceProvider">The source WaveProvider</param>
        public static async Task CreateWaveFileAsync(string filename, IWaveProvider sourceProvider)
        {
            StorageFile fileOperation = await StorageFile.GetFileFromPathAsync(filename);
            Stream fileStream = await fileOperation.OpenStreamForWriteAsync();

            using (var writer = new WaveFileWriterRT(fileStream, sourceProvider.WaveFormat))
            {
                writer.filename = filename;
                long outputLength = 0;
                var buffer = new byte[sourceProvider.WaveFormat.AverageBytesPerSecond * 4];
                while (true)
                {
                    int bytesRead = sourceProvider.Read(buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        // end of source provider
                        break;
                    }
                    outputLength += bytesRead;
                    // Write will throw exception if WAV file becomes too large
                    writer.Write(buffer, 0, bytesRead);
                }
            }
        }