-
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.
Added IFF sample loader as helper method.
- Loading branch information
Showing
4 changed files
with
144 additions
and
92 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
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
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,20 @@ | ||
/******************************************************************************/ | ||
/* This source, or parts thereof, may be used in any software as long the */ | ||
/* license of NostalgicPlayer is keep. See the LICENSE file for more */ | ||
/* information. */ | ||
/******************************************************************************/ | ||
namespace Polycode.NostalgicPlayer.Kit.Containers | ||
{ | ||
/// <summary> | ||
/// Result from sample loaders | ||
/// </summary> | ||
public enum LoadResult | ||
{ | ||
/// <summary></summary> | ||
Ok, | ||
/// <summary></summary> | ||
UnknownFormat, | ||
/// <summary></summary> | ||
Error | ||
} | ||
} |
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,110 @@ | ||
/******************************************************************************/ | ||
/* This source, or parts thereof, may be used in any software as long the */ | ||
/* license of NostalgicPlayer is keep. See the LICENSE file for more */ | ||
/* information. */ | ||
/******************************************************************************/ | ||
using System.IO; | ||
using Polycode.NostalgicPlayer.Kit.Containers; | ||
using Polycode.NostalgicPlayer.Kit.Streams; | ||
|
||
namespace Polycode.NostalgicPlayer.Kit.Utility | ||
{ | ||
/// <summary> | ||
/// Helper class to load IFF samples | ||
/// </summary> | ||
public static class IffSampleLoader | ||
{ | ||
/********************************************************************/ | ||
/// <summary> | ||
/// Load an IFF sample and return the data | ||
/// </summary> | ||
/********************************************************************/ | ||
public static LoadResult Load(ModuleStream moduleStream, int sampleNumber, out IffSample iffSample) | ||
{ | ||
iffSample = null; | ||
|
||
if (moduleStream.Read_B_UINT32() != 0x464f524d) // FORM | ||
{ | ||
// Seek back again | ||
moduleStream.Seek(-4, SeekOrigin.Current); | ||
|
||
return LoadResult.UnknownFormat; | ||
} | ||
|
||
uint formLength = moduleStream.Read_B_UINT32(); | ||
|
||
if (moduleStream.Read_B_UINT32() != 0x38535658) // 8SVX | ||
{ | ||
// Seek back again | ||
moduleStream.Seek(-8, SeekOrigin.Current); | ||
|
||
return LoadResult.UnknownFormat; | ||
} | ||
|
||
formLength -= 4; | ||
|
||
IffSample info = new IffSample(); | ||
|
||
while (formLength > 0) | ||
{ | ||
uint chunkName = moduleStream.Read_B_UINT32(); | ||
uint chunkLength = moduleStream.Read_B_UINT32(); | ||
formLength -= 8; | ||
|
||
if ((chunkLength % 2) != 0) | ||
chunkLength++; | ||
|
||
switch (chunkName) | ||
{ | ||
// VHDR | ||
case 0x56484452: | ||
{ | ||
info.OneShotHiSamples = moduleStream.Read_B_UINT32(); | ||
info.RepeatHiSamples = moduleStream.Read_B_UINT32(); | ||
info.SamplesPerHiCycle = moduleStream.Read_B_UINT32(); | ||
info.SamplesPerSec = moduleStream.Read_B_UINT16(); | ||
info.Octaves = moduleStream.Read_UINT8(); | ||
|
||
byte compressed = moduleStream.Read_UINT8(); | ||
if (compressed != 0) | ||
return LoadResult.Error; | ||
|
||
info.Volume = moduleStream.Read_B_UINT32(); | ||
|
||
formLength -= 20; | ||
chunkLength -= 20; | ||
break; | ||
} | ||
|
||
// BODY | ||
case 0x424f4459: | ||
{ | ||
if ((info.SampleData != null) || (info.Octaves == 0)) | ||
return LoadResult.Error; | ||
|
||
info.SampleData = moduleStream.ReadSampleData(sampleNumber, (int)chunkLength, out int readBytes); | ||
if (readBytes != chunkLength) | ||
return LoadResult.Error; | ||
|
||
formLength -= chunkLength; | ||
chunkLength = 0; | ||
break; | ||
} | ||
} | ||
|
||
if (chunkLength > 0) | ||
{ | ||
moduleStream.Seek(chunkLength, SeekOrigin.Current); | ||
formLength -= chunkLength; | ||
} | ||
} | ||
|
||
if (info.SampleData == null) | ||
return LoadResult.Error; | ||
|
||
iffSample = info; | ||
|
||
return LoadResult.Ok; | ||
} | ||
} | ||
} |