コード例 #1
0
        /// <summary>
        /// Get note to played, call for every sixteenth but not always return a note
        /// </summary>
        /// <returns></returns>
        public MathMotifNote Calculate()
        {
            LastNotePlayed = null;

            // Add cadence to note
            if (Notes != null && Notes.Count > 0)
            {
                BuildScore();

                if (Score != null && Score.Length > 0)
                {
                    CurrentIndexNotePlayed = (InfinityMusic.instance.IndexMeasure % MeasureCount) * MAX_NOTE_PER_MEASURE + InfinityMusic.instance.IndexSixteenthMeasure;

                    while (CurrentIndexNotePlayed >= Score.Length)
                    {
                        CurrentIndexNotePlayed = CurrentIndexNotePlayed - Score.Length;
                    }
                    if (CurrentIndexNotePlayed < 0)
                    {
                        CurrentIndexNotePlayed = 0;
                    }

                    //Debug.Log("Score[" + ((Global.instance.IndexMeasure % MeasureCount) * NotePerMeasure + Global.instance.IndexSixteenthMeasure) + "] " + CurrentIndexNotePlayed);


                    //Debug.Log("last=" + last + " sendNote:" + sendNote);
                    if (Score[CurrentIndexNotePlayed] != null)
                    {
                        if (!Score[CurrentIndexNotePlayed].CadenceDuration.Silence)
                        {
                            //Debug.Log("CurrentIndexNotePlayed:" + CurrentIndexNotePlayed + "/" + Score.Length + " IndexMeasure:" + Global.instance.IndexMeasure % MeasureCount);
                            LastNotePlayed = new MathMotifNote()
                            {
                                Note            = Score[CurrentIndexNotePlayed].Note + Transpose,
                                CadenceDuration = Score[CurrentIndexNotePlayed].CadenceDuration,
                                Duration        = InfinityMusic.instance.SixteenthDurationMs * Score[CurrentIndexNotePlayed].NbrOfSixteen(),
                            };

                            LastNotePlayed.Velocity = Velocity;
                            if (InfinityMusic.instance.IndexSixteenthMeasure == 0)
                            {
                                // First note in measure, apply accentuation
                                LastNotePlayed.Velocity += (int)((float)Velocity * (float)Accentuation);
                                if (LastNotePlayed.Velocity > 127)
                                {
                                    LastNotePlayed.Velocity = 127;
                                }
                            }

                            if (!DrumKit)
                            {
                                LastNotePlayed.Patch = PatchIndex;
                            }
                            else
                            {
                                LastNotePlayed.Patch = 0;
                            }

                            LastNotePlayed.Drum = DrumKit;
                        }
                        //Debug.Log("Score[" + CurrentIndexNotePlayed + "] " + LastNotePlayed.Note + " " + LastNotePlayed.Duration + " " + LastNotePlayed.Velocity);
                    }
                    //else
                    //    Debug.Log("Score[" + CurrentIndexNotePlayed + "] null");
                }
            }
            return(LastNotePlayed);
        }
コード例 #2
0
        /// <summary>
        /// Loop every sisteenth duration to calculate music
        /// </summary>
        /// <returns></returns>
        IEnumerator Trigger()
        {
            DateTime now = DateTime.Now;

            DateStart    = now;
            LastDateTick = now;

            while (true)
            {
                /// <summary>
                /// Duree d'un quarter (noire) en seconde, ne depend pas du nombre de temps par mesure
                /// </summary>
                double quarterDurationMs;

                quarterDurationMs     = 60000d / (double)QuarterPerMinute;
                CountSixteenthMeasure = MeasureLength * 4;

                SixteenthDurationMs = quarterDurationMs / 4d;
                //DebugPosition();

                if (!IsPlaying)
                {
                    yield return(new WaitForSeconds(TickDurationSec));
                }
                else
                {
                    lock (this)
                    {
                        // Action at each start of measure
                        if (MeasureTimePlayMs == 0d)
                        {
                        }

                        // Action at each sixt
                        if (PlayASixt)
                        {
                            List <MPTKNote> notes = new List <MPTKNote>();
                            UtMathMotif[]   uts   = GameObject.FindObjectsOfType <UtMathMotif>();
                            if (uts != null && uts.Length > 0)
                            {
                                foreach (UtMathMotif utmotif in uts)
                                {
                                    if (utmotif.IsEnabled)
                                    {
                                        MathMotifNote motifNote = utmotif.Calculate();
                                        if (motifNote != null)
                                        {
                                            notes.Add(new MPTKNote()
                                            {
                                                Note     = motifNote.Note,
                                                Duration = motifNote.Duration,
                                                Velocity = motifNote.Velocity,
                                                Patch    = motifNote.Patch,
                                                Drum     = motifNote.Drum,
                                            });
                                        }
                                    }
                                }

                                MidiStreamPlayer.MPTK_Play(notes);
                            }
                        }
                    }

                    yield return(new WaitForSeconds(TickDurationSec));

                    lock (this)
                    {
                        now = DateTime.Now;

                        TotalTimePlayMs = System.Convert.ToInt64((now - DateStart).TotalMilliseconds);

                        // Delay since last tick
                        TickTimePlayMs = (now - LastDateTick).TotalMilliseconds;

                        // if paused reset timeplay
                        if (TickTimePlayMs > 100)
                        {
                            TickTimePlayMs = 0;
                        }
                        LastDateTick = now;

                        // Time since last sixt
                        SixtTimePlayMs += TickTimePlayMs;

                        // Time since last measure
                        MeasureTimePlayMs += TickTimePlayMs;

                        IndexTicks++;
                        PlayASixt = false;

                        // Check if need to play a sixt
                        if (SixtTimePlayMs >= SixteenthDurationMs)
                        {
                            PlayASixt = true;
                            IndexSixteenthMeasure++;
                            SixtTimePlayMs = SixtTimePlayMs - SixteenthDurationMs;
                        }

                        // Check if start of measure
                        if (IndexSixteenthMeasure >= CountSixteenthMeasure)
                        {
                            IndexTicks            = 0;
                            IndexSixteenthMeasure = 0;
                            //Debug.Log("MeasureTimePlayMs:" + MeasureTimePlayMs);
                            LastMeasureTimePlayMs = MeasureTimePlayMs;
                            MeasureTimePlayMs     = 0;// -2*SixteenthDurationMs;
                            IndexMeasure++;
                            if (IndexMeasure >= MaxMeasure)
                            {
                                IndexMeasure = 0;
                            }
                        }
                        IndexQuarterMeasure = IndexSixteenthMeasure / 4;
                    }
                }
            }
        }
コード例 #3
0
        public override void Generate(bool genRandom = false)
        {
            Debug.Log("GenerateMotif Selected Algo:" + SelectedAlgo);
            if (!Application.isPlaying)
            {
                return;
            }

            if (ScaleDefinition.Scales == null)
            {
                return;
            }

            if (OctaveMin > OctaveMax)
            {
                OctaveMax = OctaveMin;
            }

            // Generate seed notes from the selected scale (octave start to octave end i.e. the ambitus)
            List <int> scaleNotes = new List <int>();
            int        step       = StepInScale < 1 ? 1 : StepInScale;

            for (int scale = OctaveMin; scale <= OctaveMax; scale++)
            {
                for (int delta = 0; delta < ScaleDefinition.Scales[ScaleIndex].Ecart.Length; delta += step)
                {
                    scaleNotes.Add(scale * 12 + ScaleDefinition.Scales[ScaleIndex].Ecart[delta]);
                }
            }
            if (scaleNotes.Count == 0)
            {
                Debug.LogWarning("No scale defined");
                return;
            }

            int noteIndex = 0;

            if (SelectedAlgo == Mode.Down)// || SelectedMode == Mode.CircleDown)
            {
                noteIndex = scaleNotes.Count - 1;
            }

            Notes = new List <MathMotifNote>();

            for (int measure = 0; measure < MeasureCount; measure++)
            {
                // Generate NotePerMeasure (16) sixteeth by measure. It's the maximum of note.
                for (int notepos = 0; notepos < MAX_NOTE_PER_MEASURE; notepos++)
                {
                    MathMotifNote note = new MathMotifNote
                    {
                        Note = -1
                    };

                    switch (SelectedAlgo)
                    {
                    case Mode.Random:
                        note.Note = RepeatLast();
                        if (note.Note < 0 && scaleNotes.Count > 0)
                        {
                            note.Note = scaleNotes[UnityEngine.Random.Range(0, scaleNotes.Count)];
                        }
                        break;

                    case Mode.Up:
                        note.Note = RepeatLast();
                        if (note.Note < 0)
                        {
                            if (noteIndex >= scaleNotes.Count)
                            {
                                noteIndex = 0;
                            }
                            note.Note = scaleNotes[noteIndex];
                            noteIndex++;
                        }
                        break;

                    case Mode.Down:
                        note.Note = RepeatLast();
                        if (note.Note < 0)
                        {
                            if (noteIndex < 0)
                            {
                                noteIndex = scaleNotes.Count - 1;
                            }
                            note.Note = scaleNotes[noteIndex];
                            noteIndex--;
                        }
                        break;

                    case Mode.Fixed:
                        note.Note = scaleNotes[0];
                        break;

                    case Mode.CircleDown:
                    case Mode.CircleUp:
                    {
                        float angle = Mathf.Lerp(0, Mathf.PI / 2f, ((float)noteIndex) / scaleNotes.Count);
                        float sin;
                        angle *= (RotationSpeed / 100f);
                        if (SelectedAlgo == Mode.CircleUp)
                        {
                            // Entre 0 et 1;
                            sin = Mathf.Sin(angle);
                        }
                        else
                        {
                            // Entre 1 et 0;
                            sin = Mathf.Cos(angle);
                        }

                        int iseed = Mathf.RoundToInt(Mathf.Lerp(0, scaleNotes.Count - 1, sin));
                        if (iseed < 0)
                        {
                            iseed = 0;
                        }
                        else if (iseed >= scaleNotes.Count)
                        {
                            iseed = scaleNotes.Count - 1;
                        }
                        note.Note = scaleNotes[iseed];
                        Debug.Log(noteIndex + " " + sin + " " + note.Note);
                        noteIndex++;
                    }
                    break;
                    }

                    // Add note with only gamme index + delta from first note in gamme (C)
                    // Only these information are saved, others are rebuild with buildscore
                    Notes.Add(note);
                }
            }
            LastCadenceApplied = DateTime.MinValue;
        }
コード例 #4
0
ファイル: UtCadence.cs プロジェクト: dreadcrumb/minibeansjam2
        public override void Generate(bool fake)
        {
            try
            {
                int indexCadence = 0;
                Durations = new List <Cadence>();
                Debug.Log("Generate Cadence");
                for (int measure = 0; measure < MeasureCount; measure++)
                {
                    int countSixteen = 16; // Always the max else : Global.instance.countSixteenthMeasure;

                    // Generate for the maximum : 4 measures
                    while (countSixteen > 0)
                    {
                        Cadence.Duration enDuration = Cadence.Duration.NotDefined;

                        float randDuration = UnityEngine.Random.Range(1f, 100f);

                        if (randDuration < RatioWhole)
                        {
                            enDuration = Cadence.Duration.Whole;
                        }
                        if (randDuration < RatioHalf)
                        {
                            enDuration = Cadence.Duration.Half;
                        }
                        if (randDuration < RatioQuarter)
                        {
                            enDuration = Cadence.Duration.Quarter;
                        }
                        if (randDuration < RatioEighth)
                        {
                            enDuration = Cadence.Duration.Eighth;
                        }
                        if (randDuration < RatioSixteen)
                        {
                            enDuration = Cadence.Duration.Sixteenth;
                        }
                        if (enDuration == Cadence.Duration.NotDefined)
                        {
                            enDuration = Cadence.Duration.Quarter;
                        }

                        countSixteen -= MathMotifNote.NbrOfSixteen(enDuration);

                        float randSilence = UnityEngine.Random.Range(1f, 100f);

                        Cadence cadence = new Cadence()
                        {
                            enDuration = enDuration, Silence = randSilence >= PctSilence ? false : true
                        };
                        //Debug.Log("   cadence:" + indexCadence + " measure:" + measure + " countSixteen:" + countSixteen + " enDuration:" + cadence.enDuration + " Silence:" + cadence.Silence);
                        Durations.Add(cadence);
                        indexCadence++;
                    }
                }

                LastCadenceGenerated = DateTime.Now;
            }
            catch (System.Exception ex)
            {
                MidiPlayerGlobal.ErrorDetail(ex);
            }
        }