Esempio n. 1
0
        private ISampleProvider BuildWavePartAudio(UWavePart part, UProject project)
        {
            AudioFileReader stream;

            try { stream = new AudioFileReader(part.FilePath); }
            catch { return(null); }
            return(new WaveToSampleProvider(stream));
        }
Esempio n. 2
0
        public static float[] BuildPeaks(UWavePart part, System.ComponentModel.BackgroundWorker worker)
        {
            const double peaksRate = 4000;

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            float[] peaks;
            using (var stream = new AudioFileReader(part.FilePath))
            {
                int    channels     = part.Channels;
                double peaksSamples = (int)((double)stream.Length / stream.WaveFormat.BlockAlign / stream.WaveFormat.SampleRate * peaksRate);
                peaks = new float[(int)(peaksSamples + 1) * channels];
                double blocksPerPixel = stream.Length / stream.WaveFormat.BlockAlign / peaksSamples;

                var converted = new WaveToSampleProvider(stream);

                float[] buffer = new float[4096];

                int    readed;
                int    readPos = 0;
                int    peaksPos = 0;
                double bufferPos = 0;
                float  lmax = 0, lmin = 0, rmax = 0, rmin = 0;
                while ((readed = converted.Read(buffer, 0, 4096)) != 0)
                {
                    readPos += readed;
                    for (int i = 0; i < readed; i += channels)
                    {
                        lmax = Math.Max(lmax, buffer[i]);
                        lmin = Math.Min(lmin, buffer[i]);
                        if (channels > 1)
                        {
                            rmax = Math.Max(rmax, buffer[i + 1]);
                            rmin = Math.Min(rmin, buffer[i + 1]);
                        }
                        if (i > bufferPos)
                        {
                            lmax = -lmax; lmin = -lmin; rmax = -rmax; rmin = -rmin; // negate peaks to fipped waveform
                            peaks[peaksPos * channels]     = lmax == 0 ? lmin : lmin == 0 ? lmax : (lmin + lmax) / 2;
                            peaks[peaksPos * channels + 1] = rmax == 0 ? rmin : rmin == 0 ? rmax : (rmin + rmax) / 2;
                            peaksPos++;
                            lmax       = lmin = rmax = rmin = 0;
                            bufferPos += blocksPerPixel * stream.WaveFormat.Channels;
                        }
                    }
                    bufferPos -= readed;
                    worker.ReportProgress((int)((double)readPos * sizeof(float) * 100 / stream.Length));
                }
            }
            sw.Stop();
            System.Diagnostics.Debug.WriteLine("Build peaks {0} ms", sw.Elapsed.TotalMilliseconds);
            return(peaks);
        }
Esempio n. 3
0
        public override UPart ReadJson(JsonReader reader, Type objectType, UPart existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            JObject jObj = JObject.Load(reader);
            UPart   part;

            if (jObj.Property("notes") != null)
            {
                part = new UVoicePart();
            }
            else
            {
                part = new UWavePart();
            }
            serializer.Populate(jObj.CreateReader(), part);
            return(part);
        }
Esempio n. 4
0
        public static UWavePart CreatePart(string filepath)
        {
            foreach (var part in DocManager.Inst.Project.Parts)
            {
                var _part = part as UWavePart;
                if (_part != null && _part.FilePath == filepath)
                {
                    return(new UWavePart()
                    {
                        FilePath = filepath,
                        FileDurTick = _part.FileDurTick,
                        DurTick = _part.DurTick,
                        Channels = _part.Channels,
                        Peaks = _part.Peaks
                    });
                }
            }
            WaveStream stream = null;

            try
            {
                stream = new AudioFileReader(filepath);
            }
            catch
            {
                return(null);
            }
            int       durTick   = DocManager.Inst.Project.MillisecondToTick(1000.0 * stream.Length / stream.WaveFormat.AverageBytesPerSecond);
            UWavePart uwavepart = new UWavePart()
            {
                FilePath    = filepath,
                FileDurTick = durTick,
                DurTick     = durTick,
                Channels    = stream.WaveFormat.Channels
            };

            stream.Close();
            return(uwavepart);
        }
Esempio n. 5
0
        public async Task <List <TrackSampleProvider> > RenderAsync()
        {
            List <TrackSampleProvider> trackSampleProviders = project.Tracks.Select(
                track => new TrackSampleProvider()
            {
                Volume = PlaybackManager.DecibelToVolume(track.Volume)
            }).ToList();
            var cacheDir = PathManager.Inst.GetCachePath(project.FilePath);

            foreach (UPart part in project.Parts)
            {
                UVoicePart voicePart = part as UVoicePart;
                if (voicePart != null)
                {
                    SequencingSampleProvider sampleProvider = await RenderPartAsync(voicePart, project, cacheDir);

                    if (sampleProvider != null)
                    {
                        trackSampleProviders[voicePart.TrackNo].AddSource(
                            sampleProvider,
                            TimeSpan.FromMilliseconds(project.TickToMillisecond(voicePart.PosTick)));
                    }
                }
                UWavePart wavePart = part as UWavePart;
                if (wavePart != null)
                {
                    try {
                        var stream = new AudioFileReader(wavePart.FilePath);
                        trackSampleProviders[wavePart.TrackNo].AddSource(
                            new WaveToSampleProvider(stream),
                            TimeSpan.FromMilliseconds(project.TickToMillisecond(wavePart.PosTick)));
                    } catch (Exception e) {
                        Log.Error(e, "Failed to open audio file");
                    }
                }
            }
            return(trackSampleProviders);
        }