private IEnumerator GenerateMelody(int seed) { if (seed == 0) { seed = System.DateTime.Now.Ticks.GetHashCode(); } curplaying = seed; Random.seed = seed; MelodyGenerator generator = Utils.Utility.RandomElement(generators); Debug.Log(generator + "," + seed); GenericMelody melody = musicCache.ContainsKey(seed)?musicCache[seed]:generator.GenerateMelody(); if (musicCache.ContainsKey(seed) == false) { musicCache[seed] = melody; } // melody.StripToLength(5); StopCoroutine("PlayMelody"); StartCoroutine("PlayMelody", melody); yield break; }
private IEnumerator GenerateMelody() { GenericMelody melody = Utility.RandomElement(generators).GenerateMelody(); StopCoroutine("PlayMelody"); StartCoroutine("PlayMelody", melody); yield break; }
public static GenericMelody Deserialize(string dat) { GenericMelody result = new GenericMelody(); string[][] dats = dat.Split('|').Select(l => l.Split(',')).ToArray(); result.voices = dats[0].Select(v => int.Parse(v)).ToList(); result.frequencies = dats[1].Select(v => float.Parse(v)).ToList(); result.durations = dats[2].Select(v => float.Parse(v)).ToList(); result.volumes = dats[3].Select(v => float.Parse(v)).ToList(); return(result); }
public override GenericMelody GenerateMelody() { GenerateLines(); float tempo_bpm = Random.Range(60.0f, 260.0f); List <AudioClip> sounds = new List <AudioClip>(); for (int i = 0; i < lines.Count; i++) { sounds.Add(Utility.RandomElement(Jazzy.allsounds)); } float beatlength = 60.0f / (tempo_bpm); GenericMelody melody = new GenericMelody(); melody.frequencies = new List <float>(); melody.sounds = new List <AudioClip>(); melody.volumes = new List <float>(); melody.durations = new List <float>(); melody.clearchannelonretrigger = Random.Range(0, 4) == 0; //I actually don't like this effect too much melody.specifychannels = melody.clearchannelonretrigger; melody.channels = new List <int>(); //Debug.Log("specifying " + melody.specifychannels.ToString()); //for each vertical slice for (int j = 0; j < lines[0].Count; j++) { for (int i = 0; i < lines.Count; i++) { if (lines[i][j] != 666) { if ((j == 0) || (lines[i][j] != lines[i][j - 1])) { melody.frequencies.Add(NoteToFreq(lines[i][j])); melody.sounds.Add(sounds[i]); melody.volumes.Add(1.0f); melody.durations.Add(0.0f); melody.channels.Add(i); } } } melody.durations[melody.durations.Count - 1] += beatlength; } return(melody); }
public override GenericMelody GenerateMelody() { GenerateLines(); float tempo_bpm = Random.Range(60.0f, 260.0f); //Debug.Log ("tempo = " + tempo_bpm ); List <int> sounds = new List <int>(); for (int i = 0; i < lines.Count; i++) { sounds.Add(Random.Range(0, 5)); } float beatlength = 60.0f / (tempo_bpm); GenericMelody melody = new GenericMelody(); melody.frequencies = new List <float>(); melody.voices = new List <int>(); melody.volumes = new List <float>(); melody.durations = new List <float>(); //Debug.Log("specifying " + melody.specifychannels.ToString()); //for each vertical slice for (int j = 0; j < lines[0].Count; j++) { for (int i = 0; i < lines.Count; i++) { if (lines[i][j] != 666) { if ((j == 0) || (lines[i][j] != lines[i][j - 1])) { melody.frequencies.Add(Utils.Utility.NoteToFreq(lines[i][j])); melody.voices.Add(sounds[i]); melody.volumes.Add(1.0f); melody.durations.Add(0.0f); } } } melody.durations[melody.durations.Count - 1] += beatlength; } return(melody); }
public MidiFileGenerator(TextAsset dat) { ta = dat; var s = new System.IO.MemoryStream(ta.bytes); var br = new System.IO.BinaryReader(s); var mfr = new Multimedia.Midi.MidiFileReader(br); GenericMelody melody = new GenericMelody(); melody.frequencies = new List <float>(); melody.voices = new List <int>(); melody.volumes = new List <float>(); melody.durations = new List <float>(); int accum = 0; foreach (var track in mfr.tracks) { for (int i = 0; i < track.Count; i++) { var e = track[i]; var cm = e.Message is Multimedia.Midi.ChannelMessage ? (Multimedia.Midi.ChannelMessage)e.Message : null; accum += e.Ticks; if (cm != null && cm.Command == Multimedia.Midi.ChannelCommand.NoteOn) { melody.frequencies.Add(Utils.Utility.NoteToFreq(cm.Data1 - 64)); melody.voices.Add(0); melody.volumes.Add(1.0f); if (melody.durations.Count > 0) { melody.durations[melody.durations.Count - 1] = accum / timescale; } melody.durations.Add(0); accum = 0; } } gm = melody; } }
public override GenericMelody GenerateMelody() { MakeMotives(); ProcessMotiveList(); FlattenMotiveList(); //float tempo_bpm=Random.Range(60.0f,260.0f); List <AudioClip> sounds = new List <AudioClip>(); for (int i = 0; i < voices; i++) { sounds.Add(Utility.RandomElement(Jazzy.allsounds)); } GenericMelody melody = new GenericMelody(); melody.frequencies = new List <float>(); melody.sounds = new List <AudioClip>(); melody.volumes = new List <float>(); melody.durations = new List <float>(); melody.clearchannelonretrigger = false; melody.specifychannels = false; melody.channels = new List <int>(); float lasttime = 0; for (int i = 0; i < flatnotes.Count; i++) { float duration = flatnotes[i].onset - lasttime; melody.frequencies.Add(flatnotes[i].pitch); melody.sounds.Add(sounds[flatnotes[i].voice]); melody.volumes.Add(flatnotes[i].volume); melody.durations.Add(duration); lasttime = flatnotes[i].onset; } return(melody); }
public override GenericMelody GenerateMelody() { MakeMotives(); ProcessMotiveList(); FlattenMotiveList(); //float tempo_bpm=Random.Range(60.0f,260.0f); List <int> sounds = new List <int>(); for (int i = 0; i < voices; i++) { sounds.Add(Random.Range(0, 5)); } GenericMelody melody = new GenericMelody(); melody.frequencies = new List <float>(); melody.voices = new List <int>(); melody.volumes = new List <float>(); melody.durations = new List <float>(); float lasttime = 0; for (int i = 0; i < flatnotes.Count; i++) { float duration = flatnotes[i].onset - lasttime; melody.frequencies.Add(flatnotes[i].pitch); melody.voices.Add(sounds[flatnotes[i].voice]); melody.volumes.Add(flatnotes[i].volume); melody.durations.Add(duration); lasttime = flatnotes[i].onset; } return(melody); }
public override GenericMelody GenerateMelody() { //Debug.Log("RANDOM SEED : " + Random.seed.ToString()); minduration = 0.01f; maxduration = Random.Range(0.1f, 2.0f); melodic = Random.Range(0, 4) > 0; diatonic = Random.Range(0, 6) > 0; //weight diatonic chainmemory = Random.Range(4, 11); proceedByDifferences = Random.Range(0, 2) == 0; bars = Random.Range(3, 1) > 0; barprob = Random.Range(0.75f, 1.0f); subbarprob = Random.Range(0.0f, barprob); beatcount = Random.Range(2, 7); barlength = Random.Range(1.0f, 5.0f); //Debug.Log("bar length: " + barlength.ToString()); //Debug.Log("beat count: " + beatcount.ToString()); //Debug.Log("bar prob: " + barprob.ToString()); //Debug.Log("beat prob: " + subbarprob.ToString()); if (proceedByDifferences) { //Debug.Log("differences"); } else { //Debug.Log("nondifferences"); } if (melodic) { //Debug.Log("melodic"); frequencycount = Random.Range(5, 10); MusicPlayer.mainCam.GetComponent <PP_ScreenWaves>().amplitude = frequencycount / 50f; durationcount = Random.Range(2, 3); samplecount = Random.Range(2, 4); } else { //Debug.Log("not melodic"); frequencycount = Random.Range(1, 3); durationcount = Random.Range(2, 5); samplecount = Random.Range(5, 10); } detune = Random.Range(Mathf.Pow(2.0f, -5.0f / 12.0f), Mathf.Pow(2.0f, 5.0f / 12.0f)); //Debug.Log("detune by " + detune.ToString()); if (diatonic) { //Debug.Log("diatonic"); scale = Random.Range(0, scales.GetLength(0)); transposition = Random.Range(0, 12); //Debug.Log("Scale = "+scale.ToString()); //Debug.Log("Transposition = "+transposition.ToString()); //Debug.Log(MusicPlayer.mainCam); // MusicPlayer.mainCam.GetComponent<Vortex>().radius = new Vector2(scale/3f, scale/3f); // MusicPlayer.mainCam.GetComponent<Vortex>().center = new Vector2(samplecount, samplecount/ 2f); // MusicPlayer.mainCam.GetComponent<Vortex>().angle = transposition * 30f; } else { //Debug.Log("not diatonic"); } sounds = RandomSublist(allsounds, samplecount); bool rhythmic = Random.Range(0, 3) >= 0; //weight for beats if (rhythmic) { // if (maxduration<0.5) // maxduration*=2; //Debug.Log("Beaty"); } else { //Debug.Log("Not Beaty"); } durations = new List <float>(); for (int i = 0; i < durationcount; i++) { if (rhythmic) { float r = 1.0f / Random.Range(1, 5); if (profile != 6) { r *= maxduration * Random.Range(1, 5); } durations.Add(r); } else { durations.Add(Random.Range(minduration, maxduration)); } } durations.Sort(); frequencies = new List <float>(); if (diatonic) { int range = Random.Range(0, 4); range = 1; //Debug.Log("range = " + range.ToString()); int low = 0; int high = 0; switch (range) { case 0: low = -27; high = -13; break; case 1: low = -20; high = 0; break; case 2: low = -13; high = 13; break; case 3: low = -27; high = 13; break; } for (int j = low; j < high; j++) { if (scales[scale, (j + transposition + 48) % 12] != 1) { continue; } float note = Mathf.Pow(2.0f, j / 12.0f); frequencies.Add(note * detune); } } else { for (int i = 0; i < frequencycount; i++) { float f = Random.Range(minfrequency, maxfrequency); frequencies.Add(f * detune); } } frequencies.Sort(); //for the non-diatonic case - already sorted in diatonic - for proceedByDifferences durationhistory = new List <int>(); for (int i = 0; i < leadin_durations; i++) { durationhistory.Add(Random.Range(0, durations.Count)); } frequencyhistory = new List <int>(); for (int i = 0; i < leadin_frequency; i++) { frequencyhistory.Add(Random.Range(0, frequencies.Count)); } soundhistory = new List <int>(); for (int i = 0; i < leadin_samples; i++) { soundhistory.Add(Random.Range(0, sounds.Count)); } bool modulate = Random.Range(0, 3) > 0; phraselength = Random.Range(15.0F, 35.0F); keychangehistory = new List <float>(); keychangehistory.Add(1); if (modulate) { //public float phraselength;//between 10 - 30 seconds for (int i = 0; i < 10; i++) { keychangehistory.Add(Random.Range(0.75F, 1.25F)); } } //pregenerate first ten levels, f**k it for (int level = 1; level < 10; level++) { //durations Chain <int> chain_durations = new Chain <int>(durationhistory, chainmemory); List <int> durations_adding = new List <int>(); while (durations_adding.Count < level * levelmultiplier) { durations_adding.AddRange(chain_durations.Generate(level * levelmultiplier - durations_adding.Count)); } durationhistory.AddRange(durations_adding); //frequencies if (proceedByDifferences) { DeltaChain <int> chain_frequencies = new DeltaChain <int>(frequencyhistory, chainmemory, Subtract.IntSubtract); List <int> frequencies_adding = new List <int>(); while (frequencies_adding.Count < level * levelmultiplier) { frequencies_adding.AddRange(chain_frequencies.Generate(level * levelmultiplier - frequencies_adding.Count)); } frequencyhistory.AddRange(frequencies_adding); } else { Chain <int> chain_frequencies = new Chain <int>(frequencyhistory, chainmemory); List <int> frequencies_adding = new List <int>(); while (frequencies_adding.Count < level * levelmultiplier) { frequencies_adding.AddRange(chain_frequencies.Generate(level * levelmultiplier - frequencies_adding.Count)); } frequencyhistory.AddRange(frequencies_adding); } //samples Chain <int> chain_sounds = new Chain <int>(soundhistory, chainmemory); List <int> sounds_adding = new List <int>(); while (sounds_adding.Count < level * levelmultiplier) { sounds_adding.AddRange(chain_sounds.Generate(level * levelmultiplier - sounds_adding.Count)); } soundhistory.AddRange(sounds_adding); } hasleadinstrument = Random.Range(0, 4) > 0; leadreplaceprob = Random.Range(0.6f, 1.0f); leadinstrument = Random.Range(0, sounds.Count); leadspread = Random.Range(1, 5); leadpasses = Random.Range(1, 4); //Debug.Log("leadspread " + leadspread.ToString()); //Debug.Log("leadpasses " + leadpasses.ToString()); //Debug.Log("leadreplaceprob " + leadreplaceprob.ToString()); if (hasleadinstrument && (frequencies.Count > 2)) { for (int t = 0; t < leadpasses; t++) { for (int i = 0; i < Mathf.Min(new int[] { soundhistory.Count, frequencyhistory.Count, durationhistory.Count }); i++) { if (soundhistory[i] == leadinstrument && durationhistory[i] > 0 && Random.value < leadreplaceprob) { durationhistory[i]--; durationhistory.Insert(i + 1, durationhistory[i]); soundhistory.Insert(i + 1, leadinstrument); frequencyhistory.Insert(i + 1, frequencyhistory[i]); frequencyhistory[i + 1] = Mathf.Clamp(frequencyhistory[i + 1] + (Random.value < 0.5f ? 1 : -1) * Random.Range(0, leadspread), 0, frequencies.Count - 1); i++; } } } } //Debug.Log("history lengths : " + soundhistory.Count.ToString() + " , " + durationhistory.Count.ToString()); soundlocation = 0; frequencylocation = 0; durationlocation = 0; melody = new GenericMelody(); melody.durations = new List <float>(); melody.frequencies = new List <float>(); melody.sounds = new List <AudioClip>(); melody.volumes = new List <float>(); ProcessMusic(); return(melody); }
private IEnumerator PlayMelody(GenericMelody melody) { ResetDensityIndex(); if (melody.durations != null) { pitchindex = 0; durationindex = 0; velocityindex = 0; voiceindex = 0; channelindex = 0; curmelody = melody; } else { melody = curmelody; } while (true) { if (quiettime > 0) { yield return(new WaitForSeconds(quiettime)); } float duration = melody.durations[durationindex]; if (pitchindex == 0 || Random.Range(0.0f, 1.0f) <= density[densitywindow]) { musicsynth.freqs[channelindex] = melody.frequencies[pitchindex] * 440.0f; musicsynth.vols[channelindex] = melody.volumes[velocityindex]; musicsynth.attacks[channelindex] = musicsynth.defend; musicsynth.voice[channelindex] = melody.voices[voiceindex] % 2; musicsynth.phase[channelindex] = 0; musicsynth.increments[channelindex] = 0; } if (duration > 0) { yield return(new WaitForSeconds(Mathf.Max(duration, quiettime))); } pitchindex = (pitchindex + 1); durationindex = (durationindex + 1); velocityindex = (velocityindex + 1); voiceindex = (voiceindex + 1); if (pitchindex >= melody.frequencies.Count) { //yield break; pitchindex = 0; durationindex = 0; velocityindex = 0; voiceindex = 0; channelindex = 0; curmelody = melody; yield return(new WaitForSeconds(3)); } channelindex = (channelindex + 1) % musicsynth.freqs.Length; } }
private IEnumerator PlayMelody(GenericMelody melody) { int pitchindex = 0; int durationindex = 0; int velocityindex = 0; int sampleindex = 0; int channelindex = 0; float tempRand = Random.Range(0f, 100f); AudioSource[] audiosources = GetComponents <AudioSource>(); mainCam.GetComponent <PP_LineArt>().lineColor = lightColors[(int)tempRand % 9]; while (true) { float pitch = melody.frequencies[pitchindex]; float duration = melody.durations[durationindex]; float velocity = melody.volumes[velocityindex] / 2; AudioClip sample = melody.sounds[sampleindex]; //Debug.Log("DurationIndex "+durationindex); //Debug.Log("PitchIndex " + pitchindex); //Debug.Log("VelocityIndex " + velocityindex); // Debug.Log("SampleIndex " + sampleindex); mainCam.GetComponent <Vortex>().radius = new Vector2(sampleindex % 14, sampleindex % 13); mainCam.GetComponent <Vortex>().center = new Vector2(sampleindex % 16, sampleindex % 18); mainCam.GetComponent <Vortex>().angle = tempRand + (sampleindex * 4f); dirLight.GetComponent <Light>().color = lightColors[sampleindex % 9]; if (discomfort < 10) { mainCam.GetComponent <PP_LineArt>().lineAmount = (sampleindex % (10 - discomfort) * 500); } else { mainCam.GetComponent <PP_LineArt>().lineAmount = (sampleindex % 10 * 500); } mainCam.GetComponent <PP_ScreenWaves>().amplitude = pitch / 9f; mainCam.GetComponent <NoiseAndGrain>().intensityMultiplier = discomfort; mainCam.transform.parent.gameObject.GetComponent <Gain>().level = discomfort; mainCam.transform.parent.gameObject.GetComponent <Noise>().offset = 2f - (discomfort / 5f); mainCam.transform.parent.gameObject.GetComponent <AudioDistortionFilter>().distortionLevel = discomfort / 10f; sampleIndex = sampleindex; AudioSource a_s; if (melody.specifychannels) { a_s = audiosources[melody.channels[channelindex]]; } else { a_s = audiosources[channelindex]; } a_s.pitch = pitch; a_s.volume = velocity; //produces clicks :( //if (melody.clearchannelonretrigger) // a_s.Stop(); a_s.PlayOneShot(sample); yield return(new WaitForSeconds(duration)); pitchindex = (pitchindex + 1) % melody.frequencies.Count; durationindex = (durationindex + 1) % melody.durations.Count; velocityindex = (velocityindex + 1) % melody.volumes.Count; sampleindex = (sampleindex + 1) % melody.sounds.Count; if (melody.specifychannels) { channelindex = (channelindex + 1) % melody.channels.Count; } else { channelindex = (channelindex + 1) % audiosources.Length; } } }