Exemple #1
0
        public Tuple <MasterAdapter, List <Fader>, CancellationTokenSource, Task> RenderProject(int startTick)
        {
            var source = new CancellationTokenSource();
            var items  = new List <RenderItem>();
            var faders = new List <Fader>();

            foreach (var track in project.tracks)
            {
                var trackItems = PrepareTrack(track, project, startTick).ToArray();
                var sources    = trackItems.Select(item => {
                    var waveSource  = new WaveSource(item.PosMs, item.DurMs, item.Envelope, item.SkipOver);
                    item.OnComplete = data => waveSource.SetWaveData(data);
                    return(waveSource);
                }).ToList();
                sources.AddRange(project.parts
                                 .Where(part => part is UWavePart && part.trackNo == track.TrackNo)
                                 .Select(part => part as UWavePart)
                                 .Select(part => {
                    var waveSource = new WaveSource(
                        project.TickToMillisecond(part.position),
                        project.TickToMillisecond(part.Duration),
                        null,
                        part.skipMs);
                    if (part.Samples != null)
                    {
                        waveSource.SetSamples(part.Samples);
                    }
                    return(waveSource);
                }));
                var trackMix = new WaveMix(sources);
                items.AddRange(trackItems);
                var fader = new Fader(trackMix);
                fader.Scale = PlaybackManager.DecibelToVolume(track.Volume);
                faders.Add(fader);
            }
            items = items.OrderBy(item => item.PosMs).ToList();
            int threads  = Util.Preferences.Default.PrerenderThreads;
            var progress = new Progress(items.Count);
            var task     = Task.Run(() => {
                var progress = new Progress(items.Count);
                Parallel.ForEach(source: items, parallelOptions: new ParallelOptions()
                {
                    MaxDegreeOfParallelism = threads
                }, body: item => {
                    if (source.Token.IsCancellationRequested)
                    {
                        return;
                    }
                    item.progress = progress;
                    Resample(item);
                });
                ReleaseSourceTemp();
                progress.Clear();
            });
            var master = new MasterAdapter(new WaveMix(faders));

            master.SetPosition((int)(project.TickToMillisecond(startTick) * 44100 / 1000));
            return(Tuple.Create(master, faders, source, task));
        }
Exemple #2
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);
        }