-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate from <http://gochaism.googlecode.com>
- Loading branch information
Showing
19 changed files
with
622 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.