Example #1
0
        public bool SetNote(MeasureElementInfo mei)
        {
            try
            {
                switch (mei.Type)
                {
                case MeasureElementInfoType.Time:
                    timeInfo = (TimeInfo)mei.Element;
                    return(false);

                case MeasureElementInfoType.Note:
                    var noteInfo = (NoteInfo)mei.Element;
                    motor.Speed = Pitch.GetFrequency(noteInfo.Name);
                    if (OctaveOffset < 0)
                    {
                        motor.Speed /= 2 * Math.Abs(OctaveOffset);
                    }
                    else if (OctaveOffset > 0)
                    {
                        motor.Speed *= 2 * Math.Abs(OctaveOffset);
                    }
                    BeatsRemaining = noteInfo.Duration / timeInfo.Divisions;
                    return(true);
                }
            }catch (Exception)
            {
            }

            return(false);
        }
Example #2
0
        //int currTrack = 0;
        //public void MultiPlay(long tick)
        //{
        //    for (int i = 0; i < Tracks.Count; i++)
        //    {
        //        var data = Tracks[i];

        //        if (data.Count > 0)
        //        {
        //            SeqData curr = data.Peek();

        //            if (tick > curr.TickTime)
        //            {
        //                data.Dequeue();

        //                if (curr.Play)
        //                {
        //                    if ((i == 0  || i < currTrack || currTrack == -1) && motor.Speed == 0)
        //                    {
        //                        motor.Speed = Pitch.GetFrequency(curr.NoteId);

        //                        if (OctaveOffset < 0)
        //                        {
        //                            motor.Speed /= 2 * Math.Abs(OctaveOffset);
        //                        }
        //                        else if (OctaveOffset > 0)
        //                        {
        //                            motor.Speed *= 2 * Math.Abs(OctaveOffset);
        //                        }

        //                        currTrack = i;
        //                    }
        //                }
        //                else
        //                {
        //                    if (i == 0 || i == currTrack)
        //                    {
        //                        motor.Speed = 0;
        //                        currTrack = -1;
        //                    }
        //                }
        //            }
        //            //else if (curr.TickTime - tick < 10)
        //            //{
        //            //    motor.Speed = 0;
        //            //}
        //        }
        //    }

        //    motor.Run();
        //}

        public void PlayScale()
        {
            int[] scaleNotes =
            {
                40, 42, 44, 45, 47, 49, 51, 52
            };

            Stopwatch time = new Stopwatch();

            time.Start();

            for (int i = 0; i < scaleNotes.Length; i++)
            {
                motor.Speed = Pitch.GetFrequency(40 + i - 12);
                long start = time.ElapsedMilliseconds;
                while (time.ElapsedMilliseconds - start < 100)
                {
                    motor.Run();
                }
            }


            for (int i = scaleNotes.Length - 1; i >= 0; i--)
            {
                motor.Speed = Pitch.GetFrequency(40 + i - 12);
                long start = time.ElapsedMilliseconds;
                while (time.ElapsedMilliseconds - start < 100)
                {
                    motor.Run();
                }
            }
        }
Example #3
0
        public static async Task <Note[]> ReadNotesFromFile(string filePath)
        {
            //string musicFile = @"Assets\Do_You_Wanna_Build_A_Snowman_piano_Part1_1.txt";
            try
            {
                StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                var           file = await InstallationFolder.GetFileAsync(filePath);

                var read = await FileIO.ReadLinesAsync(file);

                List <Note> notes = new List <Note>();
                foreach (string str in read)
                {
                    var temp = str.Split(',');
                    if (temp.Length == 2)
                    {
                        double pitch    = Pitch.GetFrequency(temp[0]);
                        double duration = Convert.ToDouble(temp[1]);
                        notes.Add(new Note {
                            Name = temp[0], Pitch = pitch, Length = duration
                        });
                    }
                }

                return(notes.ToArray());
            }
            catch (Exception)
            {
                return(null);
            }
        }
Example #4
0
        public void SinglePlay(long tick)
        {
            if (Track.Count > 0)
            {
                SeqData curr = Track.Peek();

                if (tick > curr.TickTime)
                {
                    Track.Dequeue();

                    if (curr.Play && (prev == null || !prev.Play))
                    {
                        motor.Speed = Pitch.GetFrequency(curr.NoteId) / 2;

                        //if (OctaveOffset < 0)
                        //{
                        //    motor.Speed /= 2 * Math.Abs(OctaveOffset);
                        //}
                        //else if (OctaveOffset > 0)
                        //{
                        //    motor.Speed *= 2 * Math.Abs(OctaveOffset);
                        //}

                        prev = curr;
                    }
                    else if (!curr.Play && prev.NoteId == curr.NoteId)
                    {
                        motor.Speed = 0;

                        prev = curr;
                    }
                }
                else if (curr.TickTime - tick < 10)
                {
                    motor.Speed = 0;
                }
            }
            else
            {
                motor.Speed = 0;
                Done        = true;
                return;
            }

            motor.Run();
        }
Example #5
0
        public void MultiPlay(long tick)
        {
            if (Track.Count > 0)
            {
                SeqData curr = Track.Peek();

                if (tick > curr.TickTime)
                {
                    // Stop playing note
                    if (!curr.Play)
                    {
                        for (int i = 0; i < instruments.Length; i++)
                        {
                            // Find motor that was playing this note and stop it
                            if (instruments[i].Note.NoteId == curr.NoteId)
                            {
                                instruments[i].Motor.Speed = 0;
                                instruments[i].Note        = curr;
                                break;
                            }
                        }

                        Track.Dequeue();
                    }
                    // Play note
                    else
                    {
                        // Figure out if this is a chord
                        List <SeqData> chordNotes = new List <SeqData>();
                        while (Track.Peek().TickTime == curr.TickTime && Track.Peek().Play)
                        {
                            chordNotes.Add(Track.Dequeue());
                        }

                        chordNotes = chordNotes.OrderByDescending(n => n.NoteId).ToList();

                        foreach (SeqData chordNote in chordNotes)
                        {
                            for (int i = 0; i < instruments.Length; i++)
                            {
                                // Find a motor that is free and play the note
                                if (instruments[i].Note == null || !instruments[i].Note.Play)
                                //|| (instruments[i].Note.NoteId < curr.NoteId))  // if the new note is a higher pitch, play the new note(I'm assuming it's the melody)
                                {
                                    double freq = Pitch.GetFrequency(chordNote.NoteId);
                                    while (freq < Pitch.GetFrequency(39) / 2)
                                    {
                                        freq *= 2;
                                    }

                                    while (freq > Pitch.GetFrequency(51) * 2)
                                    {
                                        freq /= 2;
                                    }

                                    instruments[i].Motor.Speed = freq / 2;

                                    if (OctaveOffset < 0)
                                    {
                                        instruments[i].Motor.Speed /= 2 * Math.Abs(OctaveOffset);
                                    }
                                    else if (OctaveOffset > 0)
                                    {
                                        instruments[i].Motor.Speed *= 2 * Math.Abs(OctaveOffset);
                                    }

                                    instruments[i].Note = chordNote;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (Instrument inst in instruments)
                {
                    inst.Motor.Speed = 0;
                }
                Done = true;
                return;
            }

            foreach (Instrument inst in instruments)
            {
                inst.Motor.Run();
            }
        }