public static void ConvertToCsv(IEnumerable <Note> notes, Stream stream, TempoMap tempoMap, NoteCsvConversionSettings settings) { using (var csvWriter = new CsvWriter(stream, settings.CsvDelimiter)) { foreach (var note in notes.Where(n => n != null)) { csvWriter.WriteRecord(new object[] { note.TimeAs(settings.TimeType, tempoMap), note.Channel, NoteCsvConversionUtilities.FormatNoteNumber(note.NoteNumber, settings.NoteNumberFormat), note.LengthAs(settings.NoteLengthType, tempoMap), note.Velocity, note.OffVelocity }); } } }
public static IEnumerable <Note> ConvertToNotes(Stream stream, TempoMap tempoMap, NoteCsvConversionSettings settings) { using (var csvReader = new CsvReader(stream, settings.CsvDelimiter)) { CsvRecord record = null; while ((record = csvReader.ReadRecord()) != null) { var values = record.Values; if (values.Length < 6) { CsvError.ThrowBadFormat(record.LineNumber, "Missing required parameters."); } ITimeSpan time; if (!TimeSpanUtilities.TryParse(values[0], settings.TimeType, out time)) { CsvError.ThrowBadFormat(record.LineNumber, "Invalid time."); } FourBitNumber channel; if (!FourBitNumber.TryParse(values[1], out channel)) { CsvError.ThrowBadFormat(record.LineNumber, "Invalid channel."); } SevenBitNumber noteNumber; if (!TryParseNoteNumber(values[2], settings.NoteNumberFormat, out noteNumber)) { CsvError.ThrowBadFormat(record.LineNumber, "Invalid note number or letter."); } ITimeSpan length; if (!TimeSpanUtilities.TryParse(values[3], settings.NoteLengthType, out length)) { CsvError.ThrowBadFormat(record.LineNumber, "Invalid length."); } SevenBitNumber velocity; if (!SevenBitNumber.TryParse(values[4], out velocity)) { CsvError.ThrowBadFormat(record.LineNumber, "Invalid velocity."); } SevenBitNumber offVelocity; if (!SevenBitNumber.TryParse(values[5], out offVelocity)) { CsvError.ThrowBadFormat(record.LineNumber, "Invalid off velocity."); } var convertedTime = TimeConverter.ConvertFrom(time, tempoMap); var convertedLength = LengthConverter.ConvertFrom(length, convertedTime, tempoMap); yield return(new Note(noteNumber, convertedLength, convertedTime) { Channel = channel, Velocity = velocity, OffVelocity = offVelocity }); } } }
/// <summary> /// Converts CSV representation of notes to collection of <see cref="MidiFile"/> reading CSV data from a stream. /// </summary> /// <param name="stream">Stream to read notes from.</param> /// <param name="tempoMap">Tempo map used to convert notes from CSV.</param> /// <param name="settings">Settings according to which CSV data must be converted. Pass null to /// use default settings.</param> /// <returns>Collection of <see cref="Note"/> representing notes written in CSV format.</returns> /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null. -or- /// <paramref name="tempoMap"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="stream"/> doesn't support reading.</exception> /// <exception cref="IOException">An I/O error occurred while reading from the stream.</exception> /// <exception cref="ObjectDisposedException"><paramref name="stream"/> is disposed.</exception> public IEnumerable <Note> ConvertCsvToNotes(Stream stream, TempoMap tempoMap, NoteCsvConversionSettings settings = null) { ThrowIfArgument.IsNull(nameof(stream), stream); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); if (!stream.CanRead) { throw new ArgumentException("Stream doesn't support reading.", nameof(stream)); } return(CsvToNotesConverter.ConvertToNotes(stream, tempoMap, settings ?? new NoteCsvConversionSettings())); }
/// <summary> /// Converts CSV representation of notes to collection of <see cref="Note"/> reading CSV data from a file. /// </summary> /// <param name="filePath">Path of the file with CSV representation of notes.</param> /// <param name="tempoMap">Tempo map used to convert notes from CSV.</param> /// <param name="settings">Settings according to which CSV data must be converted. Pass null to /// use default settings.</param> /// <returns>Collection of <see cref="Note"/> representing notes written in CSV format.</returns> /// <exception cref="ArgumentException"><paramref name="filePath"/> is a zero-length string, /// contains only white space, or contains one or more invalid characters as defined by /// <see cref="Path.InvalidPathChars"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="filePath"/> is null. -or- /// <paramref name="tempoMap"/> is null.</exception> /// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined /// maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, /// and file names must be less than 260 characters.</exception> /// <exception cref="DirectoryNotFoundException">The specified path is invalid, (for example, /// it is on an unmapped drive).</exception> /// <exception cref="IOException">An I/O error occurred while reading the file.</exception> /// <exception cref="NotSupportedException"><paramref name="filePath"/> is in an invalid format.</exception> /// <exception cref="UnauthorizedAccessException">This operation is not supported on the current platform. -or- /// <paramref name="filePath"/> specified a directory. -or- The caller does not have the required permission.</exception> public IEnumerable <Note> ConvertCsvToNotes(string filePath, TempoMap tempoMap, NoteCsvConversionSettings settings = null) { ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); using (var fileStream = FileUtilities.OpenFileForRead(filePath)) { return(ConvertCsvToNotes(fileStream, tempoMap, settings).ToList()); } }
/// <summary> /// Converts the specified collection of <see cref="Note"/> to CSV represenattion and writes it to a stream. /// </summary> /// <param name="notes">Collection of <see cref="Note"/> to convert to CSV.</param> /// <param name="stream">Stream to write CSV representation to.</param> /// <param name="tempoMap">Tempo map used to convert <paramref name="notes"/> to CSV.</param> /// <param name="settings">Settings according to which <paramref name="notes"/> must be converted. /// Pass null to use default settings.</param> /// <exception cref="ArgumentNullException"><paramref name="notes"/> is null. -or- /// <paramref name="stream"/> is null. -or- <paramref name="tempoMap"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="stream"/> doesn't support writing.</exception> /// <exception cref="IOException">An I/O error occurred while writing to the stream.</exception> /// <exception cref="ObjectDisposedException"><paramref name="stream"/> is disposed.</exception> public void ConvertNotesToCsv(IEnumerable <Note> notes, Stream stream, TempoMap tempoMap, NoteCsvConversionSettings settings = null) { ThrowIfArgument.IsNull(nameof(notes), notes); ThrowIfArgument.IsNull(nameof(stream), stream); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); if (!stream.CanWrite) { throw new ArgumentException("Stream doesn't support writing.", nameof(stream)); } NotesToCsvConverter.ConvertToCsv(notes, stream, tempoMap, settings ?? new NoteCsvConversionSettings()); }
/// <summary> /// Converts the specified collection of <see cref="Note"/> to CSV represenattion and writes it to a file. /// </summary> /// <param name="notes">Collection of <see cref="Note"/> to convert to CSV.</param> /// <param name="filePath">Path of the output CSV file.</param> /// <param name="tempoMap">Tempo map used to convert <paramref name="notes"/> to CSV.</param> /// <param name="overwriteFile">If true and file specified by <paramref name="filePath"/> already /// exists it will be overwritten; if false and the file exists, exception will be thrown.</param> /// <param name="settings">Settings according to which <paramref name="notes"/> must be converted. /// Pass null to use default settings.</param> /// <exception cref="ArgumentNullException"><paramref name="notes"/> is null. -or- /// <paramref name="tempoMap"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="filePath"/> is a zero-length string, /// contains only white space, or contains one or more invalid characters as defined by /// <see cref="Path.InvalidPathChars"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="filePath"/> is null.</exception> /// <exception cref="PathTooLongException">The specified path, file name, or both exceed the system-defined /// maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, /// and file names must be less than 260 characters.</exception> /// <exception cref="DirectoryNotFoundException">The specified path is invalid, (for example, /// it is on an unmapped drive).</exception> /// <exception cref="IOException">An I/O error occurred while writing the file.</exception> /// <exception cref="NotSupportedException"><paramref name="filePath"/> is in an invalid format.</exception> /// <exception cref="UnauthorizedAccessException">This operation is not supported on the current platform.-or- /// <paramref name="filePath"/> specified a directory.-or- The caller does not have the required permission.</exception> public void ConvertNotesToCsv(IEnumerable <Note> notes, string filePath, TempoMap tempoMap, bool overwriteFile = false, NoteCsvConversionSettings settings = null) { ThrowIfArgument.IsNull(nameof(notes), notes); ThrowIfArgument.IsNull(nameof(tempoMap), tempoMap); using (var fileStream = FileUtilities.OpenFileForWrite(filePath, overwriteFile)) { ConvertNotesToCsv(notes, fileStream, tempoMap, settings); } }