filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.14k stars 450 forks source link

Convert a MemoryStream to IAudioSource? #471

Closed itsAbhi650 closed 1 year ago

itsAbhi650 commented 1 year ago

I'm trying to cut a part from an audio and resample it but after cutting I store the cut in a stream how should I change it to an IAudioSource to perform resampling?

The cut method is:

public void CutStream(IWaveSource Source, long CutFrom, long CutTo, Stream stream, IProgress<int> p)
{
    if (ReferenceEquals(Source, null))
    {
        throw new ArgumentNullException("source");
    }
    if (ReferenceEquals(stream, null))
    {
        throw new ArgumentNullException("stream");
    }
    if (!stream.CanWrite)
    {
        throw new ArgumentException("Stream is not writeable.", "stream");
    }
    using (WaveWriter writer = new WaveWriter(stream, Source.WaveFormat))
    {
       Source.Position = CutFrom;
       byte[] buffer = new byte[Source.WaveFormat.BytesPerSecond];
       while (buffer.Length != 0)
       {
           int count = source.Read(buffer, 0, buffer.Length);
           if (count <= 0)
           {
               return;
           }
           writer.Write(buffer, 0, count);
           if (CutTo - Source.Position < Source.WaveFormat.BytesPerSecond)
           {
               buffer = new byte[CutTo - Source.Position];
           }
           float perc = (float)(source.Position / (double)CutLength);
           p.Report((int)Math.Floor(perc * 100));
       }
    }
 }
filoe commented 1 year ago

You don't need the WaveWriter. You can store it within a normal Stream. Just make sure to reset the Position back to zero by and open it with the RawDataReader providing the Stream (containing the data with Position = 0) and the WaveFormat of the Source.

itsAbhi650 commented 1 year ago

Just for confirmation! Are you saying that I should do something like:

source.Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, buffer.Length);
stream.Position = 0;
RawDataReader rawdat = new RawDataReader(stream, source.WaveFormat);
...
itsAbhi650 commented 1 year ago

Hey! I just tested this now and its working as expected.

Thanks..

filoe commented 1 year ago

Yes, but set the position of stream to 0 after you call Write()

Am 09.08.2022 um 21:40 schrieb Abhinav Pandey @.***>:

 Just for confirmation! Are you saying that I should do something like:

source.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, buffer.Length); RawDataReader rawdat = new RawDataReader(stream, source.WaveFormat); ... — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

itsAbhi650 commented 1 year ago

Yes I missed that part, Added that later and is working fine!