Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gocha committed Jun 26, 2014
1 parent 54a8cef commit 023ec17
Show file tree
Hide file tree
Showing 19 changed files with 622 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/MidiSplit/FileReaderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.ComponentModel.Composition.Hosting;
using CannedBytes.ComponentModel.Composition;
using CannedBytes.Media.IO;
using CannedBytes.Media.IO.Services;
using CannedBytes.Midi.IO;

namespace MidiSplit
{
internal class FileReaderFactory
{
public static FileChunkReader CreateReader(string filePath)
{
var context = CreateFileContextForReading(filePath);

var reader = context.CompositionContainer.CreateFileChunkReader();

return reader;
}

public static ChunkFileContext CreateFileContextForReading(string filePath)
{
var context = new ChunkFileContext();
context.ChunkFile = ChunkFileInfo.OpenRead(filePath);

context.CompositionContainer = CreateCompositionContextForReading();

return context;
}

public static CompositionContainer CreateCompositionContextForReading()
{
var factory = new CompositionContainerFactory();

factory.AddMarkedTypesInAssembly(null, typeof(IFileChunkHandler));
// add midi exports
factory.AddMarkedTypesInAssembly(typeof(MTrkChunkHandler).Assembly, typeof(IFileChunkHandler));

// note that Midi files use big endian.
// and the chunks are not aligned.
factory.AddTypes(
typeof(BigEndianNumberReader),
typeof(SizePrefixedStringReader),
typeof(ChunkTypeFactory),
typeof(FileChunkHandlerManager));

var container = factory.CreateNew();

var chunkFactory = container.GetService<ChunkTypeFactory>();
// add midi chunks
chunkFactory.AddChunksFrom(typeof(MTrkChunkHandler).Assembly, true);

return container;
}
}
}
11 changes: 11 additions & 0 deletions src/MidiSplit/MidiFileData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using CannedBytes.Midi.IO;

namespace MidiSplit
{
class MidiFileData
{
public MThdChunk Header;
public IEnumerable<MTrkChunk> Tracks;
}
}
68 changes: 68 additions & 0 deletions src/MidiSplit/MidiFileSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using CannedBytes;
using CannedBytes.ComponentModel.Composition;
using CannedBytes.Media.IO;
using CannedBytes.Media.IO.Services;
using CannedBytes.Midi.IO;

namespace MidiSplit
{
class MidiFileSerializer : DisposableBase
{
private ChunkFileContext context = new ChunkFileContext();

public MidiFileSerializer(string filePath)
{
this.context.ChunkFile = ChunkFileInfo.OpenWrite(filePath);
this.context.CompositionContainer = CreateCompositionContainer();
}

private CompositionContainer CreateCompositionContainer()
{
var factory = new CompositionContainerFactory();
var midiIOAssembly = typeof(MTrkChunkHandler).Assembly;

// add basic file handlers
factory.AddMarkedTypesInAssembly(null, typeof(IFileChunkHandler));

// add midi file handlers
factory.AddMarkedTypesInAssembly(midiIOAssembly, typeof(IFileChunkHandler));

// note that Midi files use big endian.
// and the chunks are not aligned.
factory.AddTypes(
typeof(BigEndianNumberWriter),
typeof(SizePrefixedStringWriter),
typeof(ChunkTypeFactory),
typeof(FileChunkHandlerManager));

var container = factory.CreateNew();

var chunkFactory = container.GetService<ChunkTypeFactory>();

// add midi chunks
chunkFactory.AddChunksFrom(midiIOAssembly, true);

return container;
}

public void Serialize(MidiFileData midiFileData)
{
FileChunkWriter writer = new FileChunkWriter(this.context);

writer.WriteNextChunk(midiFileData.Header);

foreach (MTrkChunk trackChunk in midiFileData.Tracks)
{
writer.WriteNextChunk(trackChunk);
}
}

protected override void Dispose(DisposeObjectKind disposeKind)
{
this.context.Dispose();
}
}
}
Loading

0 comments on commit 023ec17

Please sign in to comment.