private void Work() { sampleSource.SetPosition(BeginSample); long pos = beginSample; while (!pleaseTerminate && (endSample < 0 || pos < endSample)) { double[] chunk = sampleSource.GetChunk(chunkSize); if (chunk.LongLength == 0) { chunk = null; } if (SamplesRetrieved != null) { SamplesRetrieved(this, chunk, pos); } if (chunk == null) { break; } pos += chunk.LongLength; } if (SamplesRetrievingDone != null) { SamplesRetrievingDone(this, new EventArgs()); } }
public override void PreviewSelected() { timePassed = 0; doPreview = true; string previewPath = Path.Combine(songContainer.chart.chartPath, songContainer.chart.chartInfo.songPath); previewSampleSource = CodecFactory.Instance.GetCodec(previewPath).ChangeSampleRate(AudioManager.sampleRate).ToStereo().ToSampleSource().AppendSource(x => new VolumeSource(x), out previewVol); previewAudio = new AudioTrack("preview", previewSampleSource, previewVol); previewSampleSource.SetPosition(TimeSpan.FromSeconds(songContainer.chart.chartInfo.preview)); previewVol.Volume = 0; Game.mainInstance.audioManager.addTrack(previewAudio); }
public List<Onset> Detect(ISampleSource audio) { _onsets.Clear(); _completed = 0; _sliceCount = 0; _onsets = new List<float>(); _amplitudes = new List<float>(); var onsets = new List<Onset>(); //init detection specific variables int sliceSampleSize = (int)Math.Ceiling(_options.SliceLength * audio.WaveFormat.SampleRate); //the size of each slice's sample int slicePaddingSize = (int)Math.Ceiling(_options.SlicePaddingLength * audio.WaveFormat.SampleRate); _sliceCount = (int)Math.Ceiling((float)audio.Length/audio.WaveFormat.Channels / sliceSampleSize); //the number of slices needed var samples = (int)audio.Length / audio.WaveFormat.Channels; //init parallel specific variables var pOptions = new ParallelOptions(); if (_options.MaxDegreeOfParallelism != -1) pOptions.MaxDegreeOfParallelism = _options.MaxDegreeOfParallelism; ParallelLoopState loopState; List<Wav> wavSlices = new List<Wav>(); for (int i = 0; i < _sliceCount; i++) { int baseStart = i * sliceSampleSize; int adjustedStart = (baseStart - sliceSampleSize > 0) ? baseStart - slicePaddingSize : 0; int count = (sliceSampleSize + slicePaddingSize + baseStart > samples) ? samples - adjustedStart : sliceSampleSize + (baseStart - adjustedStart) + slicePaddingSize; float delay = (float)adjustedStart / audio.WaveFormat.SampleRate; float[] buffer = new float[count * audio.WaveFormat.Channels]; audio.SetPosition(TimeConverter.SampleSourceTimeConverter.ToTimeSpan(audio.WaveFormat, adjustedStart * audio.WaveFormat.Channels)); audio.Read(buffer, 0, count * audio.WaveFormat.Channels); wavSlices.Add(new Wav(buffer, audio.WaveFormat.SampleRate, count, audio.WaveFormat.Channels) { Delay = delay, Padding = ((delay > 0) ? slicePaddingSize : 0) / audio.WaveFormat.SampleRate }); } int bucketSize = 5; int bucketcount = (int)Math.Ceiling((double)wavSlices.Count / bucketSize); MemoryAllocator _allocator = new MemoryAllocator(); for (int i = 0; i < bucketcount; i++) { _allocator.Reset(); int count = bucketSize; if ((i + 1) * bucketSize > wavSlices.Count) count = wavSlices.Count - i * bucketSize; if (count < 0) continue; List<Wav> parallel = wavSlices.GetRange(i * bucketSize, count); var ploopResult = Parallel.ForEach(parallel, pOptions, (w, state) => GetOnsets(w, _allocator)); if (!ploopResult.IsCompleted) throw new Exception(); } onsets = _onsets.Zip(_amplitudes, (onset, amplitude) => new Onset { OnsetTime = onset, OnsetAmplitude = amplitude }).ToList(); onsets = onsets.OrderBy(f => f.OnsetTime).ToList(); float prev = 0; float combine = 0.03f; var ret = new List<Onset>(); for (int i = 0; i < onsets.Count; i++) { if (onsets[i].OnsetTime - prev < _options.MinimumTimeDelta / 1000.0f) continue; prev = onsets[i].OnsetTime; ret.Add(onsets[i]); } return ret; }
public List <Onset> Detect(ISampleSource audio) { _onsets.Clear(); _completed = 0; _sliceCount = 0; _onsets = new List <float>(); _amplitudes = new List <float>(); var onsets = new List <Onset>(); //init detection specific variables int sliceSampleSize = (int)Math.Ceiling(_options.SliceLength * audio.WaveFormat.SampleRate); //the size of each slice's sample int slicePaddingSize = (int)Math.Ceiling(_options.SlicePaddingLength * audio.WaveFormat.SampleRate); _sliceCount = (int)Math.Ceiling((float)audio.Length / audio.WaveFormat.Channels / sliceSampleSize); //the number of slices needed var samples = (int)audio.Length / audio.WaveFormat.Channels; //init parallel specific variables var pOptions = new ParallelOptions(); if (_options.MaxDegreeOfParallelism != -1) { pOptions.MaxDegreeOfParallelism = _options.MaxDegreeOfParallelism; } ParallelLoopState loopState; List <Wav> wavSlices = new List <Wav>(); for (int i = 0; i < _sliceCount; i++) { int baseStart = i * sliceSampleSize; int adjustedStart = (baseStart - sliceSampleSize > 0) ? baseStart - slicePaddingSize : 0; int count = (sliceSampleSize + slicePaddingSize + baseStart > samples) ? samples - adjustedStart : sliceSampleSize + (baseStart - adjustedStart) + slicePaddingSize; float delay = (float)adjustedStart / audio.WaveFormat.SampleRate; float[] buffer = new float[count * audio.WaveFormat.Channels]; audio.SetPosition(TimeConverter.SampleSourceTimeConverter.ToTimeSpan(audio.WaveFormat, adjustedStart * audio.WaveFormat.Channels)); audio.Read(buffer, 0, count * audio.WaveFormat.Channels); wavSlices.Add(new Wav(buffer, audio.WaveFormat.SampleRate, count, audio.WaveFormat.Channels) { Delay = delay, Padding = ((delay > 0) ? slicePaddingSize : 0) / audio.WaveFormat.SampleRate }); } int bucketSize = 5; int bucketcount = (int)Math.Ceiling((double)wavSlices.Count / bucketSize); MemoryAllocator _allocator = new MemoryAllocator(); for (int i = 0; i < bucketcount; i++) { _allocator.Reset(); int count = bucketSize; if ((i + 1) * bucketSize > wavSlices.Count) { count = wavSlices.Count - i * bucketSize; } if (count < 0) { continue; } List <Wav> parallel = wavSlices.GetRange(i * bucketSize, count); var ploopResult = Parallel.ForEach(parallel, pOptions, (w, state) => GetOnsets(w, _allocator)); if (!ploopResult.IsCompleted) { throw new Exception(); } } onsets = _onsets.Zip(_amplitudes, (onset, amplitude) => new Onset { OnsetTime = onset, OnsetAmplitude = amplitude }).ToList(); onsets = onsets.OrderBy(f => f.OnsetTime).ToList(); float prev = 0; float combine = 0.03f; var ret = new List <Onset>(); for (int i = 0; i < onsets.Count; i++) { if (onsets[i].OnsetTime - prev < _options.MinimumTimeDelta / 1000.0f) { continue; } prev = onsets[i].OnsetTime; ret.Add(onsets[i]); } return(ret); }