Example #1
0
        /// <summary>
        ///     Gets musical scale's intervals sequence by the scale's name.
        /// </summary>
        /// <param name="name">The name of a scale.</param>
        /// <returns>
        ///     Intervals sequence for the scale with the name <paramref name="name" />; or null if
        ///     there is no a scale with this name.
        /// </returns>
        /// <exception cref="ArgumentException"><paramref name="name" /> is null or contains white-spaces only.</exception>
        public static IEnumerable <Interval> GetByName(string name)
        {
            ThrowIfArgument.IsNullOrWhiteSpaceString(nameof(name), name, "Scale's name");

            foreach (var fieldInfo in typeof(ScaleIntervals).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                var displayName =
                    (Attribute.GetCustomAttribute(fieldInfo, typeof(DisplayNameAttribute)) as DisplayNameAttribute)
                    ?.Name;
                if (string.IsNullOrWhiteSpace(displayName) ||
                    !displayName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                var intervals = fieldInfo.GetValue(null) as IEnumerable <Interval>;
                if (intervals == null)
                {
                    continue;
                }

                return(intervals);
            }

            return(null);
        }
        /// <summary>
        /// Retrieves a first output MIDI device with the specified name.
        /// </summary>
        /// <param name="name">The name of an output MIDI device to retrieve.</param>
        /// <returns>Output MIDI device with the specified name.</returns>
        /// <exception cref="ArgumentException">
        /// <para>One of the following errors occured:</para>
        /// <list type="bullet">
        /// <item>
        /// <description><paramref name="name"/> is <c>null</c> or contains white-spaces only.</description>
        /// </item>
        /// <item>
        /// <description><paramref name="name"/> specifies an output MIDI device which is not presented in the system.</description>
        /// </item>
        /// </list>
        /// </exception>
        public static OutputDevice GetByName(string name)
        {
            ThrowIfArgument.IsNullOrWhiteSpaceString(nameof(name), name, "Device name");

            var device = GetAll().FirstOrDefault(d => d.Name == name);

            if (device == null)
            {
                throw new ArgumentException($"There is no output MIDI device '{name}'.", nameof(name));
            }

            return(device);
        }
Example #3
0
        internal static FileStream OpenFileForWrite(string filePath, bool overwriteFile)
        {
            ThrowIfArgument.IsNullOrWhiteSpaceString(nameof(filePath), filePath, "File path");

            try
            {
                return(File.Open(filePath, overwriteFile ? FileMode.Create : FileMode.CreateNew));
            }
            catch (PathTooLongException)
            {
                var fileHandle = GetFileHandle(filePath, GENERIC_WRITE, overwriteFile ? CREATE_ALWAYS : CREATE_NEW);
                return(new FileStream(fileHandle, FileAccess.Write));
            }
        }
Example #4
0
        internal static FileStream OpenFileForRead(string filePath)
        {
            ThrowIfArgument.IsNullOrWhiteSpaceString(nameof(filePath), filePath, "File path");

            try
            {
                return(File.OpenRead(filePath));
            }
            catch (PathTooLongException)
            {
                var fileHandle = GetFileHandle(filePath, GENERIC_READ, OPEN_EXISTING);
                return(new FileStream(fileHandle, FileAccess.Read));
            }
        }
Example #5
0
        /// <summary>
        /// Converts the string representation of a time span to its <see cref="ITimeSpan"/> equivalent.
        /// </summary>
        /// <param name="input">A string containing a time span to convert.</param>
        /// <returns>A <see cref="ITimeSpan"/> equivalent to the time span contained in <paramref name="input"/>.</returns>
        /// <exception cref="ArgumentException"><paramref name="input"/> is null or contains white-spaces only.</exception>
        /// <exception cref="FormatException"><paramref name="input"/> has invalid format.</exception>
        public static ITimeSpan Parse(string input)
        {
            ThrowIfArgument.IsNullOrWhiteSpaceString(nameof(input), input, "Input string");

            foreach (var parser in Parsers.Values)
            {
                ITimeSpan timeSpan;
                var       parsingResult = parser(input, out timeSpan);

                if (parsingResult.Status == ParsingStatus.Parsed)
                {
                    return(timeSpan);
                }
                else if (parsingResult.Status == ParsingStatus.FormatError)
                {
                    throw parsingResult.Exception;
                }
            }

            throw new FormatException("Time span has unknown format.");
        }
Example #6
0
        /// <summary>
        ///     Converts the string representation of a time span to its <see cref="ITimeSpan" /> equivalent.
        /// </summary>
        /// <param name="input">A string containing a time span to convert.</param>
        /// <returns>A <see cref="ITimeSpan" /> equivalent to the time span contained in <paramref name="input" />.</returns>
        /// <exception cref="ArgumentException"><paramref name="input" /> is null or contains white-spaces only.</exception>
        /// <exception cref="FormatException"><paramref name="input" /> has invalid format.</exception>
        public static ITimeSpan Parse(string input)
        {
            ThrowIfArgument.IsNullOrWhiteSpaceString(nameof(input), input, "Input string");

            foreach (var parser in Parsers)
            {
                var parsingResult = parser(input);

                var result   = parsingResult.Item1;
                var timeSpan = parsingResult.Item2;

                if (result.Status == ParsingStatus.Parsed)
                {
                    return(timeSpan);
                }
                if (result.Status == ParsingStatus.FormatError)
                {
                    throw result.Exception;
                }
            }

            throw new FormatException("Time span has unknown format.");
        }