void playTracks(Rendering rendering) { float position = rendering.renderingStart; float renderLength = rendering.renderingLength; foreach (var track in tracks) { foreach (var element in track.Elements) { if (element.SoundLine == null) { continue; //skip element without output } if (element.Offset < position + rendering.renderingLength && element.Offset + element.Length > position) { var lineRendering = element.SoundLine.getByRendering(rendering); var renderStart = element.Offset - position; var task = new Task <SoundElementRenderResult>(() => { if (renderStart >= 0) //you must wait to start playing { var rendered = element.GetSound(0, rendering.renderingLength - renderStart, rendering); return(new SoundElementRenderResult { data = rendered, offset = (int)CountSamples(renderStart) }); } else { var rendered = element.GetSound(-renderStart, rendering.renderingLength, rendering); return(new SoundElementRenderResult { data = rendered, offset = 0 }); } }); lineRendering.currentToRender.Add(task); task.Start(); } } } }
public async Task Generate(byte[] buffer, int offset, int count) { try { Console.WriteLine("RenderStart"); int renderPeriod = count / WaveFormat.BitsPerSample * 8 / WaveFormat.Channels; float renderTime = (float)renderPeriod / WaveFormat.SampleRate; var renderLength = (renderTime) * Project.current.tempo / 60; if (renderLength < 0) { renderLength = 0; } liveRenderingNow = true; var rendering = new Rendering() { renderingStart = position, renderingLength = (float)renderLength, project = Project.current, type = RenderngType.live }; var soundReady = rendering.project.lines[0].getByRendering(rendering); rendering.project.Render(rendering); var sound = await soundReady; Console.WriteLine("ReturnedSound"); ReturnedSound(sound, buffer); rendering.project.Clear(rendering); if (status == Status.playing) { position += (float)renderLength; positionChanged?.Invoke(position); } Console.WriteLine("RenderEnded"); SoundPlayed?.Invoke(sound); } catch { } }
public float[,] GetSound(float start, float length, Rendering rendering, NotesCollection notes) { long samples = (long)Project.current.CountSamples(length); //how many samples you need on output var ret = new float[2, samples]; //sound that will be returned var notesCount = notes.Count; var tasks = new Task <float[, ]> [notesCount]; for (var i = 0; i < notesCount; i++) { var note = notes[i].Clone(); var item = findItem(note); var currentNoteLength = item.CalcLengthByNote(note); if (note.Offset < start + length && note.Offset + currentNoteLength > start) { tasks[i] = Task.Run(() => { try { if (start > note.Offset) { var l1 = currentNoteLength - (start - note.Offset); if (length < l1) { l1 = length; } return(item.GetSound(start - note.Offset, l1, note)); } else { var l1 = length + start - note.Offset; if (note.Length + item.R < l1) { l1 = currentNoteLength; } return(item.GetSound(0, l1, note)); } } catch { return(null); } }); } } for (var i = 0; i < notesCount; i++) { var note = notes[i]; if (start > note.Offset) { if (tasks[i] != null) { try { var retTask = tasks[i].Result; for (long k = 0; k < retTask.LongLength / 2; k++) { ret[0, k] += retTask[0, k]; ret[1, k] += retTask[1, k]; } } catch (Exception e) { Console.Write(e); } } } else { var notSamplesOffset = (long)Project.current.CountSamples(note.Offset - start); if (tasks[i] != null) { var retTask = tasks[i].Result; var minLength = retTask.LongLength / 2; if (ret.LongLength / 2 < minLength) { minLength = ret.LongLength / 2; } long k; for (k = 0; k < minLength; k++) { ret[0, k + notSamplesOffset] += retTask[0, k]; ret[1, k + notSamplesOffset] += retTask[1, k]; } } } } return(ret); }
public float[,] GetSound(float start, float length, Rendering rendering) { return(Sound.GetSound(start, length, rendering, Items)); }
public void prepareToRender(Rendering rendering) { var slRend = getByRendering(rendering); slRend.data = new float[2, (int)rendering.project.CountSamples(rendering.renderingLength)]; }
public void prepareToRender(Rendering rendering) { }
internal void clearAfterRender(Rendering rendering) { }
public float[,] GetSound(float start, float length, Rendering rendering, NotesCollection notes) { var maxLength = notes.Max(note => pitchesToElement[(int)note.Pitch].SoundLength + note.Offset); if (length > maxLength) { length = maxLength; } long samples = (long)Project.current.CountSamples(length); //how many samples you need on output var ret = new float[2, samples]; //sound that will be returned var notesCount = notes.Count; var tasks = new Task <float[, ]> [notesCount]; for (var i = 0; i < notesCount; i++) { var note = notes[i].Clone(); if (!pitchesToElement.ContainsKey((int)note.Pitch)) { continue; } var BPElement = pitchesToElement[(int)note.Pitch]; if (note.Offset < start + length && note.Offset + BPElement.SoundLength > start) { tasks[i] = Task.Run(() => { if (start > note.Offset) { var l1 = BPElement.SoundLength - (start - note.Offset); if (length < l1) { l1 = length; } return(pitchesToElement[(int)note.Pitch]?.GetSound(start - note.Offset, l1, note)); } else { var l1 = length + start - note.Offset; if (note.Length < l1) { l1 = BPElement.SoundLength; } return(pitchesToElement[(int)note.Pitch]?.GetSound(0, l1, note)); } }); } } for (var i = 0; i < notesCount; i++) { var note = notes[i]; if (start > note.Offset) { if (tasks[i] != null) { try { var retTask = tasks[i].Result; if (retTask == null) { continue; } for (long k = 0; k < retTask.LongLength / 2; k++) { ret[0, k] += retTask[0, k]; ret[1, k] += retTask[1, k]; } } catch (Exception e) { Console.Write(e); } } } else { var notSamplesOffset = (long)Project.current.CountSamples(note.Offset - start); if (tasks[i] != null) { var retTask = tasks[i].Result; if (retTask == null) { continue; } var lengthToCopy = retTask.GetLongLength(1); if (ret.GetLongLength(1) - notSamplesOffset < lengthToCopy) { lengthToCopy = ret.GetLongLength(1) - notSamplesOffset; } for (long k = 0; k < lengthToCopy; k++) { ret[0, k + notSamplesOffset] += retTask[0, k]; ret[1, k + notSamplesOffset] += retTask[1, k]; } } } } return(ret); }
public float[,] GetSound(float start, float length, Rendering rendering, NotesCollection notes) { if (lastPlayedStart > start) { endedNotes.Clear(); } lastPlayedStart = start; int samples = (int)Project.current.CountSamples(length);//how many samples you need on output Note[] startNotes, endNotes; List <float> timeBreaks; //lock (notes) // { //todo dokładna dłuość nuty startNotes = notes.Where(note => (note.Offset < start + length && !startedNotes.Contains(note) && !endedNotes.Contains(note))).ToArray(); endNotes = notes.Where(note => (note.Offset + note.Length < start + length && !endedNotes.Contains(note))).ToArray(); startNotes = notes.Where(note => (note.Offset < start + length)).ToArray(); endNotes = notes.Where(note => (note.Offset + note.Length < start + length)).ToArray(); timeBreaks = new List <float> { start, start + length }; foreach (var x in startedNotes) { if (!timeBreaks.Contains(x.Offset)) { timeBreaks.Add(x.Offset); } } foreach (var x in endNotes) { if (!timeBreaks.Contains(x.Offset + x.Length)) { timeBreaks.Add(x.Offset + x.Length); } } //} timeBreaks.Sort(); int sumSamples = 0; for (var i = 0; i < timeBreaks.Count - 1; i++) { writer.Write((int)JmsVstHost.Commands.GetSoundNoteSynth); var nowSamples = (int)Project.current.CountSamples(timeBreaks[i + 1] - start); if (i == timeBreaks.Count - 2) { nowSamples = samples;//to provide errors of float incorrection } if (nowSamples - sumSamples <= 0) { nowSamples = sumSamples + 1;//todo niebezpieczeńswo ze przekroczymy wartość samples } writer.Write(nowSamples - sumSamples); sumSamples = nowSamples; //writer.Write(0); var startNotes2 = startNotes.Where(note => (note.Offset < timeBreaks[i + 1] && !startedNotes.Contains(note) && !endedNotes.Contains(note))).ToArray(); var endNotes2 = endNotes.Where(note => (note.Offset + note.Length < timeBreaks[i + 1] && !endedNotes.Contains(note))).ToArray(); writer.Write((int)startNotes2.Count() + (int)endNotes2.Count()); foreach (var note in startNotes2) { SendNote(note); startedNotes.Add(note); } foreach (var note in endNotes2) { SendEndNote(note); startedNotes.Remove(note); endedNotes.Add(note); } } writer.Flush(); var ret = new float[2, samples];//sound that will be returned var reader = new BinaryReader(process.StandardOutput.BaseStream); Debug.WriteLine("startRching " + samples); for (var i = 0; i < samples; i++) { ret[0, i] = reader.ReadSingle(); ret[1, i] = reader.ReadSingle(); } Debug.WriteLine("endRching " + samples); return(ret); }
public void clearAfterRender(Rendering rendering) { renderings.Remove(rendering); }
public async void checkIfReady(Rendering rendering) { if (!rendering.canHarvest) { return; } var slRend = getByRendering(rendering); var sound = slRend.data; Console.WriteLine("startAwaiting"); foreach (var oneTask in slRend.currentToRender) { var result = await oneTask; Console.WriteLine("ElementAwaited"); rendered(result.offset, result.data, sound, result.volumeChange); } foreach (var input in inputs) { var inputData = await input.input.getByRendering(rendering); Console.WriteLine("inputAwaited"); var length = sound.GetLength(1); for (int i = 0; i < length; i++) { sound[0, i] += inputData[0, i]; sound[1, i] += inputData[1, i]; } //output.output.rendered(0, sound, rendering, output.volume); } if (volume != 0) { if (volume != 1) { var length = sound.GetLength(1); for (int i = 0; i < length; i++) { sound[0, i] *= volume; sound[1, i] *= volume; } } foreach (var effect in effects) { if (effect.IsActive) { sound = effect.DoFilter(sound); } } } slRend.data = sound; Console.WriteLine("RedyToResolve Line " + this); slRend.Resolve(); if (connectedUIs != 0) { float minL = sound[0, 0]; float minR = sound[1, 0]; float maxL = sound[0, 0]; float maxR = sound[1, 0]; for (var i = 0; i < sound.GetLength(1); i++) { if (sound[0, i] < minL) { minL = sound[0, i]; } else if (sound[0, i] > maxL) { maxL = sound[0, i]; } if (sound[1, i] < minR) { minR = sound[1, i]; } else if (sound[1, i] > maxR) { maxR = sound[1, i]; } } minL = Math.Abs(minL); maxL = Math.Abs(maxL); minR = Math.Abs(minR); maxR = Math.Abs(maxR); LastVolume[0] = minL > maxL ? minL : maxL; LastVolume[1] = minR > maxR ? minR : maxR; } }
public float[,] GetSound(float start, float length, Rendering rendering) { return(original.GetSound(start, length, rendering)); }