unsafe internal AudioFrame GenerateAudioData(uint samples)
        {
            // Buffer size is (number of samples) * (size of each sample)
            // We choose to generate single channel (mono) audio. For multi-channel, multiply by number of channels
            uint       bufferSize = samples * sizeof(float);
            AudioFrame frame      = new Windows.Media.AudioFrame(bufferSize);

            using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
                using (IMemoryBufferReference reference = buffer.CreateReference())
                {
                    byte * dataInBytes;
                    uint   capacityInBytes;
                    float *dataInFloat;

                    // Get the buffer from the AudioFrame
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes);

                    // Cast to float since the data we are generating is float
                    dataInFloat = (float *)dataInBytes;

                    float  amplitude       = 0.3f;
                    int    sampleRate      = (int)parentGraph.EncodingProperties.SampleRate;
                    double sampleIncrement = (frequency * (Math.PI * 2)) / sampleRate;

                    // Generate a sine wave and populate the values in the memory buffer
                    for (int i = 0; i < samples; i++)
                    {
                        double sinValue = amplitude * Math.Sin(angle);
                        dataInFloat[i] = (float)sinValue;
                        angle         += sampleIncrement;
                    }
                }

            return(frame);
        }
        private unsafe AudioFrame ReadAudioData(uint samples)
        {
            // Buffer size is (number of samples) * (size of each sample)
            uint       bufferSize = samples * sizeof(byte) * 2;
            AudioFrame frame      = new Windows.Media.AudioFrame(bufferSize);

            using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
                using (IMemoryBufferReference reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacityInBytes;

                    // Get the buffer from the AudioFrame
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes);

                    // Read audio data from the stream and copy it to the AudioFrame buffer
                    var  readBytes = new byte[capacityInBytes];
                    uint bytesRead = audioStream.Read(readBytes);

                    if (bytesRead == 0)
                    {
                        frameInputNode.Stop();
                    }

                    for (int i = 0; i < bytesRead; i++)
                    {
                        dataInBytes[i] = readBytes[i];
                    }
                }

            return(frame);
        }
Exemplo n.º 3
0
        unsafe AudioFrame GenerateAudioData(byte[] readedData, uint audioDataLength)
        {
            AudioFrame frame = new Windows.Media.AudioFrame((uint)audioDataLength);

            using (var buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
                using (IMemoryBufferReference reference = buffer.CreateReference())
                {
                    byte *dataInBytes;
                    uint  capacityInBytes;
                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes);

                    for (int i = 0; i < audioDataLength; i++)
                    {
                        dataInBytes[i] = readedData[i];
                    }
                }

            return(frame);
        }
Exemplo n.º 4
0
        unsafe private AudioFrame GenerateAudioData(Int16[] samples)
        {
            uint       bufferSize = (uint)samples.Length * sizeof(Int16);
            AudioFrame frame      = new Windows.Media.AudioFrame(bufferSize);

            using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
                using (IMemoryBufferReference reference = buffer.CreateReference())
                {
                    byte * dataInBytes;
                    uint   capacityInBytes;
                    Int16 *dataIn;

                    ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes);

                    // Cast to float since the data we are generating is float
                    dataIn = (Int16 *)dataInBytes;
                    for (var i = 0; i < samples.Length; i++)
                    {
                        dataIn[i] = samples[i];
                    }
                }

            return(frame);
        }
 public static VisualizationData AsVisualizationData(this Windows.Media.AudioFrame frame)
 {
     return(new VisualizationData(frame));
 }
        unsafe private AudioFrame GenerateAudioData(uint samples)
        {
            // Buffer size is (number of samples) * (size of each sample)
            // We choose to generate single channel (mono) audio. For multi-channel, multiply by number of channels
            uint bufferSize = samples * sizeof(float);
            AudioFrame frame = new Windows.Media.AudioFrame(bufferSize);

            using (AudioBuffer buffer = frame.LockBuffer(AudioBufferAccessMode.Write))
            using (IMemoryBufferReference reference = buffer.CreateReference())
            {
                byte* dataInBytes;
                uint capacityInBytes;
                float* dataInFloat;

                // Get the buffer from the AudioFrame
                ((IMemoryBufferByteAccess)reference).GetBuffer(out dataInBytes, out capacityInBytes);

                // Cast to float since the data we are generating is float
                dataInFloat = (float*)dataInBytes;

                float freq = 1000; // choosing to generate frequency of 1kHz
                float amplitude = 0.3f;
                int sampleRate = (int)graph.EncodingProperties.SampleRate;
                double sampleIncrement = (freq * (Math.PI * 2)) / sampleRate;

                // Generate a 1kHz sine wave and populate the values in the memory buffer
                for (int i = 0; i < samples; i++)
                {
                    double sinValue = amplitude * Math.Sin(theta);
                    dataInFloat[i] = (float)sinValue;
                    theta += sampleIncrement;
                }
            }

            return frame;
        }