Beispiel #1
0
        /// <summary>
        /// Advance until finding a note that is within the current timestamp,
        /// and start playing it.
        /// <br/><br/>
        /// This takes into account that under laggy conditions
        /// our physics updates might be coming too infrequently to hit every note on time.
        /// This will advance to where we *should have been* in the song list by now,
        /// by calculating based on how long the notes were *supppsed* to have
        /// lasted and when they were *suppposed* to have ended.
        /// <br/><br/>
        /// This might mean that a note gets its duration shorted to "catch up", or even
        /// that a note gets skipped entirely in order to "catch up".
        /// This is necessary to keep the voices of multi-voice songs synced up with each other.
        /// <br/><br/>
        /// If there is no next note and the voice isn't in looping mode,
        /// it will set isPlaying to false.
        /// <br/>
        /// </summary>
        /// <param name="now">timestamp for current time right now</param>
        private void AdvanceNote(float now)
        {
            // Keep advancing through the notes list until we get to a note
            // that should have been executing at the current time:
            while (now > noteEndTimeStamp)
            {
                NoteValue prevNote = curNote;
                ++noteNum;
                if (loop)
                {
                    noteNum = noteNum % song.Count(); // wraparound to zero if looping and past end.
                }
                if (noteNum >= song.Count())
                {
                    isPlaying = false; // stop if past end.
                    curNote   = null;
                    break;
                }

                // Advancing the note:
                // -------------------
                curNote = song[noteNum] as NoteValue;
                if (curNote == null)
                {
                    return;
                }
                if (prevNote == null)
                {
                    // No prev note, so start first note at right now:
                    noteStartTimeStamp = now;
                }
                else
                {
                    // Don't set start time to now, but rather set it to when this note
                    // *should* have started if the physics update had hit at the right time:
                    noteStartTimeStamp = noteStartTimeStamp + tempo * prevNote.Duration;
                }

                noteEndTimeStamp    = noteStartTimeStamp + tempo * curNote.Duration;
                noteFreqTotalChange = curNote.EndFrequency - curNote.Frequency;
            }

            // Now play the note we had advanced to:
            if (isPlaying)
            {
                voice.BeginProceduralSound(curNote.Frequency, tempo * curNote.KeyDownLength, curNote.Volume);
            }

            // Be aware that because we told the low level sound chip to start this note *now*, but we
            // tracked our own start time (noteStartTimeStamp) as when the note *should* have started,
            // that the low level sound chip will start the ADSR envelope now, rather than partway through
            // the middle of the envelope.  This means that if a note has to get "shorted" to catch up,
            // then the "shorted" part of the note that gets cut off will be the *end* of that note,
            // not the *start* of it.  Thus if the ADSR envelope makes short staccato notes with fast
            // attack and decay with no sustain, we won't end up silencing the note entirely when it's
            // shorted.  (We would if we had cut off the start of the note and kept the end of it that
            // occurs after the attack and the decay are over.).
            // TL;DR : If we have to play a short duration version of the note, we'd rather snip off the
            // release part at the end then snip off the attack/decay part at the start.
        }
 private static void ResultsFromList(ListValue list, StringBuilder results)
 {
     ResultColumn[] resultColumns = new ResultColumn[] { new ResultColumn("Elements") };
     for (int index = 0; index < list.Count(); index++)
     {
         resultColumns[0].Add(list.GetValue(index).ToString());
     }
     BuildResults(resultColumns, results);
 }
Beispiel #3
0
 public override object InternalExecute(Program program, object[] arguments)
 {
                 #if NILPROPOGATION
     if ((arguments[0] == null) || arguments[1] == null)
     {
         return(null);
     }
                 #endif
     Schema.Library library    = (Schema.Library)arguments[0];
     ListValue      requisites = (ListValue)arguments[1];
     library.Libraries.Clear();
     for (int index = 0; index < requisites.Count(); index++)
     {
         library.Libraries.Add((Schema.LibraryReference)((Schema.LibraryReference)requisites[index]).Clone());
     }
     return(library);
 }
Beispiel #4
0
        public override object InternalExecute(Program program, object argument1, object argument2)
        {
            ListValue list = (ListValue)argument2;

                        #if NILPROPOGATION
            if ((list == null) || (argument1 == null))
            {
                return(null);
            }
                        #endif

            program.Stack.Push(argument1);
            try
            {
                program.Stack.Push(null);
                try
                {
                    object result = false;
                    for (int index = 0; index < list.Count(); index++)
                    {
                        program.Stack.Poke(0, list[index]);
                        object tempValue = _equalNode.Execute(program);
                                                #if NILPROPOGATION
                        if (tempValue == null)
                        {
                            result = null;
                            continue;
                        }
                                                #endif
                        if ((bool)tempValue)
                        {
                            return(tempValue);
                        }
                    }
                    return(result);
                }
                finally
                {
                    program.Stack.Pop();
                }
            }
            finally
            {
                program.Stack.Pop();
            }
        }
Beispiel #5
0
        public void CanGetListIndex()
        {
            var list = new ListValue();

            list.Add(new StringValue("bar"));
            cpu.PushStack(list);

            const int INDEX = 0;

            cpu.PushStack(INDEX);

            var opcode = new OpcodeGetIndex();

            opcode.Execute(cpu);

            Assert.AreEqual(1, list.Count());
            Assert.AreEqual(new StringValue("bar"), cpu.PopStack());
        }
Beispiel #6
0
        public void CanGetDoubleIndex()
        {
            var list = new ListValue();
            list.Add(new StringValue("bar"));
            list.Add(new StringValue("foo"));
            list.Add(new StringValue("fizz"));
            cpu.PushStack(list);

            const double INDEX = 2.5;
            cpu.PushStack(INDEX);

            var opcode = new OpcodeGetIndex();

            opcode.Execute(cpu);

            Assert.AreEqual(3, list.Count());
            Assert.AreEqual(new StringValue("fizz"), cpu.PopStack());
        }
Beispiel #7
0
        public void CanGetDoubleIndex()
        {
            var list = new ListValue();

            list.Add(new StringValue("bar"));
            list.Add(new StringValue("foo"));
            list.Add(new StringValue("fizz"));
            cpu.PushStack(list);

            const double INDEX = 2.5;

            cpu.PushStack(INDEX);

            var opcode = new OpcodeGetIndex();

            opcode.Execute(cpu);

            Assert.AreEqual(3, list.Count());
            Assert.AreEqual(new StringValue("fizz"), cpu.PopStack());
        }
Beispiel #8
0
        public void CanGetCorrectListIndex()
        {
            var list = new ListValue();

            list.Add(new StringValue("bar"));
            list.Add(new StringValue("foo"));
            list.Add(new StringValue("fizz"));
            cpu.PushArgumentStack(list);

            const int INDEX = 1;

            cpu.PushArgumentStack(INDEX);

            var opcode = new OpcodeGetIndex();

            opcode.Execute(cpu);

            Assert.AreEqual(3, list.Count());
            Assert.AreEqual(new StringValue("foo"), cpu.PopArgumentStack());
        }
Beispiel #9
0
        public void CanSetListIndexWithDouble()
        {
            var list = new ListValue();
            list.Add(new StringValue("bar"));
            cpu.PushStack(list);

            const double INDEX = 0.0d;
            cpu.PushStack(INDEX);

            const string VALUE = "foo";
            cpu.PushStack(VALUE);

            var opcode = new OpcodeSetIndex();

            opcode.Execute(cpu);

            Assert.AreEqual(1, list.Count());
            Assert.AreNotEqual(new StringValue("bar"), list[0]);
            Assert.AreEqual(new StringValue("foo"), list[0]);
        }
Beispiel #10
0
 private void InitializeSuffixes()
 {
     AddSuffix("OPTIONS", new SetSuffix <ListValue>(() => list, value => list = value));
     AddSuffix("ADDOPTION", new OneArgsSuffix <Structure>(AddOption));
     AddSuffix("VALUE", new SetSuffix <Structure>(GetValue, value => Choose(value)));
     AddSuffix("INDEX", new SetSuffix <ScalarIntValue>(() => Index, value => { Index = value; if (Index >= 0 && Index < list.Count())
                                                                               {
                                                                                   SetVisibleText(GetItemString(list[Index]));
                                                                               }
                                                       }));
     AddSuffix("CLEAR", new NoArgsVoidSuffix(Clear));
     AddSuffix("CHANGED", new SetSuffix <BooleanValue>(() => TakeChange(), value => changed       = value));
     AddSuffix("MAXVISIBLE", new SetSuffix <ScalarIntValue>(() => maxVisible, value => maxVisible = value));
     AddSuffix("ONCHANGE", new SetSuffix <UserDelegate>(() => CallbackGetter(UserOnChange), value => UserOnChange = CallbackSetter(value)));
     AddSuffix("OPTIONSUFFIX", new SetSuffix <StringValue>(() => optSuffix, value => optSuffix = value));
 }
Beispiel #11
0
        public void CanSetListIndexWithDouble()
        {
            var list = new ListValue();

            list.Add(new StringValue("bar"));
            cpu.PushStack(list);

            const double INDEX = 0.0d;

            cpu.PushStack(INDEX);

            const string VALUE = "foo";

            cpu.PushStack(VALUE);

            var opcode = new OpcodeSetIndex();

            opcode.Execute(cpu);

            Assert.AreEqual(1, list.Count());
            Assert.AreNotEqual(new StringValue("bar"), list[0]);
            Assert.AreEqual(new StringValue("foo"), list[0]);
        }
Beispiel #12
0
        public static object DataValueToValue(IServerProcess process, IDataValue dataValue)
        {
            if (dataValue == null)
            {
                return(null);
            }

            IScalar scalar = dataValue as IScalar;

            if (scalar != null)
            {
                return(ScalarTypeNameToValue(dataValue.DataType.Name, scalar));
            }

            ListValue list = dataValue as ListValue;

            if (list != null)
            {
                var listValue = new List <object>();
                if (!list.IsNil)
                {
                    for (int index = 0; index < list.Count(); index++)
                    {
                        listValue.Add(DataValueToValue(process, list.GetValue(index)));
                    }
                }
                return(listValue);
            }

            IRow row = dataValue as IRow;

            if (row != null)
            {
                var rowValue = new Dictionary <string, object>();

                if (!row.IsNil)
                {
                    for (int index = 0; index < row.DataType.Columns.Count; index++)
                    {
                        var data = row.GetValue(index);
                        data.DataType.Name = DataTypeToDataTypeName(process.DataTypes, row.DataType.Columns[index].DataType);
                        rowValue.Add(row.DataType.Columns[index].Name, DataValueToValue(process, data));
                    }
                }
                return(rowValue);
            }

            TableValue     tableValue = dataValue as TableValue;
            TableValueScan scan       = null;

            try
            {
                if (tableValue != null)
                {
                    scan = new TableValueScan(tableValue);
                    scan.Open();
                    dataValue = scan;
                }

                ITable table = dataValue as ITable;
                if (table != null)
                {
                    var resultTable = new List <object>();

                    if (!table.BOF())
                    {
                        table.First();
                    }

                    bool[] valueTypes = new bool[table.DataType.Columns.Count];
                    for (int index = 0; index < table.DataType.Columns.Count; index++)
                    {
                        valueTypes[index] = table.DataType.Columns[index].DataType is IScalarType;
                    }

                    while (table.Next())
                    {
                        using (IRow currentRow = table.Select())
                        {
                            object[] nativeRow = new object[table.DataType.Columns.Count];
                            for (int index = 0; index < table.DataType.Columns.Count; index++)
                            {
                                if (valueTypes[index])
                                {
                                    resultTable.Add(currentRow[index]);
                                }
                                else
                                {
                                    resultTable.Add(DataValueToValue(process, currentRow.GetValue(index)));
                                }
                            }
                        }
                    }

                    return(resultTable);
                }
            }
            finally
            {
                if (scan != null)
                {
                    scan.Dispose();
                }
            }

            throw new NotSupportedException(string.Format("Values of type \"{0}\" are not supported.", dataValue.DataType.Name));
        }
Beispiel #13
0
        public void CanGetListIndex()
        {
            var list = new ListValue();
            list.Add(new StringValue("bar"));
            cpu.PushStack(list);

            const int INDEX = 0;
            cpu.PushStack(INDEX);

            var opcode = new OpcodeGetIndex();

            opcode.Execute(cpu);

            Assert.AreEqual(1, list.Count());
            Assert.AreEqual(new StringValue("bar"), cpu.PopStack());
        }
Beispiel #14
0
        public override object InternalExecute(Program program, object[] arguments)
        {
                        #if NILPROPOGATION
            if (arguments[0] == null)
            {
                return(null);
            }
                        #endif
            Schema.Library library = new Schema.Library(Schema.Object.EnsureUnrooted((string)arguments[0]));
            if (arguments.Length >= 2)
            {
                                #if NILPROPOGATION
                if (arguments[1] == null)
                {
                    return(null);
                }
                                #endif
                library.Version = (VersionNumber)arguments[1];
            }
            else
            {
                library.Version = new VersionNumber(-1, -1, -1, -1);
            }

            if (arguments.Length >= 3)
            {
                                #if NILPROPOGATION
                if (arguments[2] == null)
                {
                    return(null);
                }
                                #endif
                library.DefaultDeviceName = (string)arguments[2];
            }

            if (arguments.Length >= 4)
            {
                                #if NILPROPOGATION
                if (arguments[3] == null || arguments[4] == null)
                {
                    return(null);
                }
                                #endif
                ListValue files      = (ListValue)arguments[3];
                ListValue requisites = (ListValue)arguments[4];

                for (int index = 0; index < files.Count(); index++)
                {
                    library.Files.Add((Schema.FileReference)files[index]);
                }

                for (int index = 0; index < requisites.Count(); index++)
                {
                    library.Libraries.Add((Schema.LibraryReference)requisites[index]);
                }
            }

            if (arguments.Length >= 6)
            {
                                #if NILPROPOGATION
                if (arguments[5] == null)
                {
                    return(null);
                }
                                #endif
                library.Directory = (string)arguments[5];
            }

            return(library);
        }
        public void KOSUpdate(double deltaTime)
        {
            if (!IsPlaying)
            {
                return;
            }

            // Be sure we use the same game clock here as in Voice.cs's Update():  (i.e. unscaledTime vs Time vs fixedTime):
            float now = Time.unscaledTime;

            if (Time.timeScale == 0f)          // game is frozen (i.e. the Escape Menu is up.)
            {
                if (freezeBeganTimestamp < 0f) // And we weren't frozen before so it's the start of a new freeze instance.
                {
                    freezeBeganTimestamp = now;
                }
                return; // do none of the rest of this work until the pause is over.
            }
            else // game is not frozen.
            {
                if (freezeBeganTimestamp >= 0f) // And we were frozen before so we just became unfrozen now
                {
                    // Push the timestamp ahead by the duration of the pause so it will continue what's left of the note
                    // instead of truncating it early:
                    float freezeDuration = now - freezeBeganTimestamp;
                    noteStartTimeStamp  += freezeDuration;
                    noteEndTimeStamp    += freezeDuration;
                    freezeBeganTimestamp = -1f;
                }
            }

            // If still playing prev note, do nothing except maybe change
            // its frequency if it's a slidenote:
            if (now < noteEndTimeStamp)
            {
                NoteValue note = song[noteNum] as NoteValue;
                if (noteFreqTotalChange != 0.0)
                {
                    float durationPortion = (now - noteStartTimeStamp) / (noteEndTimeStamp - noteStartTimeStamp);
                    float newFreq         = note.Frequency + durationPortion * noteFreqTotalChange;
                    voice.ChangeFrequency(newFreq);
                }
                return;
            }

            // Increment to next note and start playing it:
            ++noteNum;
            if (noteNum >= song.Count())
            {
                if (loop)
                {
                    noteNum          = -1;
                    noteEndTimeStamp = -1f;
                }
                else
                {
                    IsPlaying = false;
                }
            }
            else
            {
                curNote = song[noteNum] as NoteValue;
                if (curNote != null)
                {
                    noteStartTimeStamp  = now;
                    noteEndTimeStamp    = now + tempo * curNote.Duration;
                    noteFreqTotalChange = curNote.EndFrequency - curNote.Frequency;
                    voice.BeginProceduralSound(curNote.Frequency, tempo * curNote.KeyDownLength, curNote.Volume);
                }
            }
        }
Beispiel #16
0
        public static NativeValue DataValueToNativeValue(IServerProcess process, IDataValue dataValue)
        {
            if (dataValue == null)
            {
                return(null);
            }

            IScalar scalar = dataValue as IScalar;

            if (scalar != null)
            {
                NativeScalarValue nativeScalar = new NativeScalarValue();
                nativeScalar.DataTypeName = ScalarTypeToDataTypeName(process.DataTypes, scalar.DataType);
                nativeScalar.Value        = dataValue.IsNil ? null : scalar.AsNative;
                return(nativeScalar);
            }

            ListValue list = dataValue as ListValue;

            if (list != null)
            {
                NativeListValue nativeList = new NativeListValue();
                if (!list.IsNil)
                {
                    nativeList.Elements = new NativeValue[list.Count()];
                    for (int index = 0; index < list.Count(); index++)
                    {
                        nativeList.Elements[index] = DataValueToNativeValue(process, list.GetValue(index));
                    }
                }
                return(nativeList);
            }

            IRow row = dataValue as IRow;

            if (row != null)
            {
                NativeRowValue nativeRow = new NativeRowValue();
                nativeRow.Columns = ColumnsToNativeColumns(process.DataTypes, row.DataType.Columns);

                if (!row.IsNil)
                {
                    nativeRow.Values = new NativeValue[nativeRow.Columns.Length];
                    for (int index = 0; index < nativeRow.Values.Length; index++)
                    {
                        nativeRow.Values[index] = DataValueToNativeValue(process, row.GetValue(index));
                    }
                }
                return(nativeRow);
            }

            TableValue     tableValue = dataValue as TableValue;
            TableValueScan scan       = null;

            try
            {
                if (tableValue != null)
                {
                    scan = new TableValueScan(tableValue);
                    scan.Open();
                    dataValue = scan;
                }

                ITable table = dataValue as ITable;
                if (table != null)
                {
                    NativeTableValue nativeTable = new NativeTableValue();
                    nativeTable.Columns = ColumnsToNativeColumns(process.DataTypes, table.DataType.Columns);

                    List <object[]> nativeRows = new List <object[]>();

                    if (!table.BOF())
                    {
                        table.First();
                    }

                    bool[] valueTypes = new bool[nativeTable.Columns.Length];
                    for (int index = 0; index < nativeTable.Columns.Length; index++)
                    {
                        valueTypes[index] = table.DataType.Columns[index].DataType is IScalarType;
                    }

                    while (table.Next())
                    {
                        using (IRow currentRow = table.Select())
                        {
                            object[] nativeRow = new object[nativeTable.Columns.Length];
                            for (int index = 0; index < nativeTable.Columns.Length; index++)
                            {
                                if (valueTypes[index])
                                {
                                    nativeRow[index] = currentRow[index];
                                }
                                else
                                {
                                    nativeRow[index] = DataValueToNativeValue(process, currentRow.GetValue(index));
                                }
                            }

                            nativeRows.Add(nativeRow);
                        }
                    }

                    nativeTable.Rows = nativeRows.ToArray();
                    return(nativeTable);
                }
            }
            finally
            {
                if (scan != null)
                {
                    scan.Dispose();
                }
            }

            throw new NotSupportedException(String.Format("Values of type \"{0}\" are not supported.", dataValue.DataType.Name));
        }