public float[] ReadSamplesFromSource(ISamplesProvider samplesProvider, double secondsToRead, int sampleRate)
        {
            var buffer = GetBuffer(secondsToRead, sampleRate);
            int totalBytesToRead = GetTotalBytesToRead(secondsToRead, sampleRate), totalBytesRead = 0;
            var chunks = new List<float[]>();

            while (totalBytesRead < totalBytesToRead)
            {
                int bytesRead = samplesProvider.GetNextSamples(buffer);

                if (bytesRead < 0)
                {
                    throw new AudioServiceException("Number of read bytes is negative. Check your audio provider.");
                }

                if (bytesRead == 0)
                {
                    break;
                }

                totalBytesRead += bytesRead;

                float[] chunk = GetNewChunkFromBuffer(totalBytesRead, totalBytesToRead, bytesRead, buffer);

                chunks.Add(chunk);
            }

            if (totalBytesRead < GetExactNumberOfBytesToRead(secondsToRead, sampleRate))
            {
                throw new AudioServiceException("Could not read requested number of seconds " + secondsToRead + ", audio file is not that long");
            }

            return ConcatenateChunksOfSamples(chunks);
        }
Esempio n. 2
0
        public float[] ReadSamplesFromSource(ISamplesProvider samplesProvider, double secondsToRead, int sampleRate)
        {
            var buffer = GetBuffer(secondsToRead, sampleRate);
            int totalBytesToRead = GetTotalBytesToRead(secondsToRead, sampleRate), totalBytesRead = 0;
            var chunks = new List <float[]>();

            while (totalBytesRead < totalBytesToRead)
            {
                int bytesRead = samplesProvider.GetNextSamples(buffer);

                if (bytesRead < 0)
                {
                    throw new AudioServiceException("Number of read bytes is negative. Check your audio provider.");
                }

                if (bytesRead == 0)
                {
                    break;
                }

                totalBytesRead += bytesRead;

                float[] chunk = GetNewChunkFromBuffer(totalBytesRead, totalBytesToRead, bytesRead, buffer);

                chunks.Add(chunk);
            }

            if (totalBytesRead < GetExactNumberOfBytesToRead(secondsToRead, sampleRate))
            {
                throw new AudioServiceException("Could not read requested number of seconds " + secondsToRead + ", audio file is not that long");
            }

            return(ConcatenateChunksOfSamples(chunks));
        }
Esempio n. 3
0
        public float[] ReadSamplesFromSource(ISamplesProvider samplesProvider, int secondsToRead, int sampleRate)
        {
            var buffer = GetBuffer(secondsToRead, sampleRate);
            int totalBytesToRead = GetTotalBytesToRead(secondsToRead, sampleRate), totalBytesRead = 0;
            var chunks = new List <float[]>();

            while (totalBytesRead < totalBytesToRead)
            {
                int bytesRead = samplesProvider.GetNextSamples(buffer);

                if (bytesRead < 0)
                {
                    throw new AudioServiceException("Number of bytes read is negative.");
                }

                if (bytesRead == 0)
                {
                    break;
                }

                totalBytesRead += bytesRead;

                float[] chunk;

                if (totalBytesRead > totalBytesToRead)
                {
                    chunk = new float[(totalBytesToRead - (totalBytesRead - bytesRead)) / BlockAlign];
                    Array.Copy(buffer, chunk, (totalBytesToRead - (totalBytesRead - bytesRead)) / BlockAlign);
                }
                else
                {
                    chunk = new float[bytesRead / BlockAlign];
                    Array.Copy(buffer, chunk, bytesRead / BlockAlign);
                }

                chunks.Add(chunk);
            }

            if (totalBytesRead < (secondsToRead * sampleRate * BlockAlign))
            {
                throw new AudioServiceException("Could not read requested number of seconds " + secondsToRead + ", audio file is not that long");
            }

            return(ConcatenateChunksOfSamples(chunks));
        }
        public float[] ReadSamplesFromSource(ISamplesProvider samplesProvider, int secondsToRead, int sampleRate)
        {
            var buffer = GetBuffer(secondsToRead, sampleRate);
            int totalBytesToRead = GetTotalBytesToRead(secondsToRead, sampleRate), totalBytesRead = 0;
            var chunks = new List<float[]>();

            while (totalBytesRead < totalBytesToRead)
            {
                int bytesRead = samplesProvider.GetNextSamples(buffer);

                if (bytesRead  < 0)
                {
                    throw new AudioServiceException("Number of bytes read is negative.");
                }

                if (bytesRead == 0)
                {
                    break;
                }

                totalBytesRead += bytesRead;

                float[] chunk;

                if (totalBytesRead > totalBytesToRead)
                {
                    chunk = new float[(totalBytesToRead - (totalBytesRead - bytesRead)) / BlockAlign];
                    Array.Copy(buffer, chunk, (totalBytesToRead - (totalBytesRead - bytesRead)) / BlockAlign);
                }
                else
                {
                    chunk = new float[bytesRead / BlockAlign];
                    Array.Copy(buffer, chunk, bytesRead / BlockAlign);
                }

                chunks.Add(chunk);
            }

            if (totalBytesRead < (secondsToRead * sampleRate * BlockAlign))
            {
                throw new AudioServiceException("Could not read requested number of seconds " + secondsToRead + ", audio file is not that long");
            }

            return ConcatenateChunksOfSamples(chunks);
        }
Esempio n. 5
0
 public ContinuousStreamSamplesProvider(ISamplesProvider provider)
 {
     this.provider = provider;
 }
 public ContinuousStreamSamplesProvider(ISamplesProvider provider)
 {
     this.provider = provider;
 }
 public SamplesController(ISampleApplicationService sampleApplicationService, ISamplesProvider samplesProvider)
 {
     _sampleApplicationService = sampleApplicationService;
     _samplesProvider = samplesProvider;
 }
Esempio n. 8
0
 public SamplesController(ISampleApplicationService sampleApplicationService, ISamplesProvider samplesProvider)
 {
     _sampleApplicationService = sampleApplicationService;
     _samplesProvider          = samplesProvider;
 }