Esempio n. 1
0
        public static void Main()
        {
            Console.WriteLine("------- Welcome to The -------");
            Console.WriteLine("==============================");
            Console.WriteLine("==== MUSIC GENERATOR 5000 ====");
            Console.WriteLine("==============================");
            Console.WriteLine();

            bool programRunning = true;

            while (programRunning)
            {
                Console.WriteLine("What would you like to do?");
                Console.WriteLine("1. Procedurally generate a new piece of music.");
                Console.WriteLine("2. Select some basic parameters, then generate a new piece of music.");
                Console.WriteLine("3. Manually write a new piece of music.");
                Console.WriteLine("4. Load a saved piece of music.");
                Console.WriteLine("5. Exit program.");

                string userInput = Console.ReadLine();
                Console.Clear();

                int  parsedInput;
                bool validInput = int.TryParse(userInput, out parsedInput);
                if (validInput)
                {
                    Piece pieceToDisplay = null;
                    bool  pieceSaved     = false;
                    switch (parsedInput)
                    {
                    case 1:
                        pieceToDisplay = Piece.GenerateProcedurally();
                        break;

                    case 2:
                        pieceToDisplay = Piece.GenerateProcedurallyWithParameters();
                        break;

                    case 3:
                        pieceToDisplay = Piece.GenerateManually();
                        pieceSaved     = true;
                        break;

                    case 4:
                        pieceToDisplay = FileLoader.Open();
                        pieceSaved     = true;
                        break;

                    case 5:
                        Console.WriteLine("Goodbye!");
                        Thread.Sleep(1000);
                        programRunning = false;
                        break;

                    default:

                        break;
                    }
                    if (pieceToDisplay != null)
                    {
                        pieceToDisplay.PrintInfo();
                        var printer = new StaffPrinter(pieceToDisplay);
                        printer.Print();
                        Console.ReadKey();
                        if (!pieceSaved)
                        {
                            pieceToDisplay.SavePrompt();
                        }
                        pieceToDisplay = null;
                    }
                }
                else
                {
                    Console.WriteLine($"'{userInput}' is not a valid selection. Please enter an integer.");
                }
                Console.Clear();
            }
        }
Esempio n. 2
0
        public static Piece GenerateManually()
        {
            Dictionary <string, object> parameters = getParameters();

            Console.Clear();
            var numMeasures = (int)parameters["measures"];

            var  measures          = new List <Measure>();
            int  currentMeasureNum = 0;
            int  cursorPosition    = 7;
            bool measureDeleted    = false;

            bool showInstructions = true;
            bool showDescription  = false;
            bool stillEditing     = true;

            Piece tempPiece = new Piece((string)parameters["title"], currentMeasureNum,
                                        (KeySignature)parameters["key"], (TimeSignature)parameters["time"], measures);

            while (currentMeasureNum < numMeasures && stillEditing)
            {
                // Recreate piece each time a measure is added or deleted:
                tempPiece = new Piece((string)parameters["title"], currentMeasureNum,
                                      (KeySignature)parameters["key"], (TimeSignature)parameters["time"], measures);
                // If measure was deleted, do not add a new measure (instead revisit previous measure):
                if (measureDeleted == false)
                {
                    tempPiece.Measures.Add(new Measure(tempPiece.KeySig, tempPiece.TimeSig, new List <MeasureSegment>()));
                }
                // Create reference of current measure:
                var tempMeasure = tempPiece.Measures[currentMeasureNum];
                // Reset measure deleted bool:
                measureDeleted = false;
                while (tempMeasure.CurrentLength < tempMeasure.MeasureLength && !measureDeleted && stillEditing)
                {
                    if (showDescription)
                    {
                        tempPiece.PrintInfo();
                        tempPiece.ListNotes();
                    }
                    var printer = new StaffPrinter(tempPiece);
                    printer.Print(cursorPosition);
                    printInstructions(showInstructions);
                    var editInput = Console.ReadKey();
                    // Up and down arrows move cursor so that a different line is selected:
                    if (editInput.Key == ConsoleKey.UpArrow)
                    {
                        cursorPosition -= 1;
                    }
                    else if (editInput.Key == ConsoleKey.DownArrow)
                    {
                        cursorPosition += 1;
                    }
                    else if (editInput.Key == ConsoleKey.Enter)
                    {
                        tempMeasure.Append(cursorPosition);
                    }
                    else if (editInput.Key == ConsoleKey.Backspace)
                    {
                        if (tempMeasure.Contents.Count > 0)
                        {
                            tempMeasure.Unappend();
                        }
                        // If this is empty and it is the first measure, warn user:
                        else if (currentMeasureNum == 0)
                        {
                            Console.WriteLine("There is nothing left to delete!");
                            Console.ReadKey();
                        }
                        // If this is empty and it is NOT the first measure, unappend from previous,
                        // delete this measure, and change current measure number to previous:
                        else
                        {
                            tempPiece.Measures.RemoveAt(tempPiece.Measures.Count - 1);
                            tempPiece.Measures[currentMeasureNum - 1].Unappend();
                            // subtract two, because current measure number will be incremented at end of loop:
                            currentMeasureNum -= 2;
                            measureDeleted     = true;
                        }
                    }
                    else if (editInput.Key == ConsoleKey.I)
                    {
                        showInstructions = !showInstructions;
                    }
                    else if (editInput.Key == ConsoleKey.D)
                    {
                        showDescription = !showDescription;
                    }
                    else if (editInput.Key == ConsoleKey.L)
                    {
                        MeasureSegment.ShowStaffSymbolLegend();
                    }
                    else if (editInput.Key == ConsoleKey.S)
                    {
                        if (tempPiece.SaveToJson())
                        {
                            stillEditing = false;
                        }
                    }
                    else if (editInput.Key == ConsoleKey.Escape)
                    {
                        tempPiece.SavePrompt();
                        stillEditing = false;
                    }
                    Console.Clear();
                }
                currentMeasureNum++;
            }
            return(tempPiece);
        }