Ejemplo n.º 1
0
 /// <summary>
 /// takes posession of the supplied stream
 /// </summary>
 public void LoadStream(Stream s)
 {
     Dispose();
     BaseStream = s;
     readCounter = 0;
     BinaryReader br = new BinaryReader(s);
     RiffChunk chunk = ReadChunk(br);
     if (chunk.tag != "RIFF") throw new FormatException("can't recognize riff chunk");
     riff = (RiffContainer)chunk;
 }
Ejemplo n.º 2
0
        public IRiffContainer Encode(ISound sound, int samplesPerBlock)
        {
            var sampleRate = sound[NumericData.Rate];

            if (sampleRate == null)
            {
                var sampleRates = sound
                                  .Samples
                                  .Select(s => s[NumericData.Rate])
                                  .Where(r => r != null)
                                  .Distinct()
                                  .ToArray();
                sampleRate = sampleRates.SingleOrDefault();
            }

            if (sampleRate == null)
            {
                sampleRate = 44100;
            }

            var channels = sound.Samples.Count;
            var byteRate = sampleRate * channels * 2 / 4;  //not accurate but not far off

            var container = new RiffContainer
            {
                Format = "WAVE",
                Chunks = new List <IRiffChunk>()
            };

            var extraFormat = new MicrosoftAdpcmFormat
            {
                Coefficients    = MicrosoftAdpcmConstants.DefaultCoefficients,
                SamplesPerBlock = 500
            };

            var format = new RiffFormat
            {
                Format        = 2,
                SampleRate    = (int)sampleRate,
                Channels      = channels,
                ByteRate      = (int)byteRate,
                BitsPerSample = 4,
                BlockAlign    = _microsoftAdpcmEncoder.GetBlockSize(samplesPerBlock, channels),
                ExtraData     = extraFormat.ToBytes()
            };

            container.Chunks.Add(_formatEncoder.Encode(format));
            container.Chunks.Add(new RiffChunk
            {
                Id   = "data",
                Data = _microsoftAdpcmEncoder.Encode(sound, samplesPerBlock)
            });

            return(container);
        }
Ejemplo n.º 3
0
    public void LoadStream(Stream s)
    {
        readCounter = 0;
        BinaryReader br    = new BinaryReader(s);
        RiffChunk    chunk = ReadChunk(br);

        if (chunk.tag != "RIFF")
        {
            throw new FormatException("can't recognize riff chunk");
        }
        riff = (RiffContainer)chunk;
    }
Ejemplo n.º 4
0
 public RiffContainer_INFO(RiffContainer rc)
 {
     subchunks = rc.subchunks;
     type      = "INFO";
     foreach (RiffChunk chunk in subchunks)
     {
         RiffSubchunk rsc = chunk as RiffSubchunk;
         if (chunk == null)
         {
             throw new FormatException("Invalid subchunk of INFO list");
         }
         dictionary[rsc.tag] = System.Text.Encoding.ASCII.GetString(rsc.ReadAll());
     }
 }
Ejemplo n.º 5
0
 /// <exception cref="FormatException"><paramref name="rc"/>.<see cref="RiffContainer.subchunks"/> contains a chunk that does not inherit <see cref="RiffSubchunk"/></exception>
 public RiffContainer_INFO(RiffContainer rc)
 {
     subchunks = rc.subchunks;
     type      = "INFO";
     foreach (RiffChunk chunk in subchunks)
     {
         RiffSubchunk rsc = chunk as RiffSubchunk;
         if (chunk == null)
         {
             throw new FormatException("Invalid subchunk of INFO list");                                    //TODO is this supposed to be a check on `rsc` (i.e. as a type check)? --yoshi
         }
         dictionary[rsc.tag] = System.Text.Encoding.ASCII.GetString(rsc.ReadAll());
     }
 }
Ejemplo n.º 6
0
        private RiffChunk ReadChunk(BinaryReader br)
        {
            RiffChunk ret;
            string    tag  = ReadTag(br); readCounter += 4;
            uint      size = br.ReadUInt32(); readCounter += 4;

            if (size > int.MaxValue)
            {
                throw new FormatException("chunk too big");
            }
            if (tag == "RIFF" || tag == "LIST")
            {
                RiffContainer rc = new RiffContainer
                {
                    tag  = tag,
                    type = ReadTag(br)
                };

                readCounter += 4;
                long readEnd = readCounter - 4 + size;
                while (readEnd > readCounter)
                {
                    rc.subchunks.Add(ReadChunk(br));
                }
                ret = rc.Morph();
            }
            else
            {
                RiffSubchunk rsc = new RiffSubchunk
                {
                    tag      = tag,
                    Source   = br.BaseStream,
                    Position = br.BaseStream.Position,
                    Length   = size
                };
                readCounter            += size;
                br.BaseStream.Position += size;
                ret = rsc.Morph();
            }
            if (size % 2 != 0)
            {
                br.ReadByte();
                readCounter += 1;
            }
            return(ret);
        }
Ejemplo n.º 7
0
 public RiffChunk GetSubchunk(string tag, string type)
 {
     foreach (RiffChunk rc in subchunks)
     {
         if (rc.tag == tag)
         {
             if (type == null)
             {
                 return(rc);
             }
             RiffContainer cont = rc as RiffContainer;
             if (cont != null && cont.type == type)
             {
                 return(rc);
             }
         }
     }
     return(null);
 }
Ejemplo n.º 8
0
    private RiffChunk ReadChunk(BinaryReader br)
    {
        RiffChunk ret;
        string    tag  = ReadTag(br); readCounter += 4;
        uint      size = br.ReadUInt32(); readCounter += 4;

        if (size > int.MaxValue)
        {
            throw new FormatException("chunk too big");
        }
        if (tag == "RIFF" || tag == "LIST")
        {
            RiffContainer rc = new RiffContainer();
            rc.tag  = tag;
            rc.type = ReadTag(br); readCounter += 4;
            long readEnd = readCounter - 4 + size;
            while (readEnd > readCounter)
            {
                rc.subchunks.Add(ReadChunk(br));
            }
            ret = rc.Morph();
        }
        else
        {
            RiffSubchunk rsc = new RiffSubchunk();
            rsc.tag  = tag;
            rsc.data = br.ReadBytes((int)size); readCounter += size;
            ret      = rsc.Morph();
        }
        if (size % 2 != 0)
        {
            br.ReadByte();
            readCounter += 1;
        }
        return(ret);
    }
        public IRiffContainer Encode(ISound sound)
        {
            var sampleRate = sound[NumericData.Rate];

            if (sampleRate == null)
            {
                var sampleRates = sound
                                  .Samples
                                  .Select(s => s[NumericData.Rate])
                                  .Where(r => r != null)
                                  .Distinct()
                                  .ToArray();
                sampleRate = sampleRates.SingleOrDefault();
            }

            if (sampleRate == null)
            {
                sampleRate = 44100;
            }

            var channels  = sound.Samples.Count;
            var byteRate  = sampleRate * channels * 2;
            var container = new RiffContainer
            {
                Format = "WAVE",
                Chunks = new List <IRiffChunk>()
            };

            var format = new RiffFormat
            {
                Format        = 1,
                SampleRate    = (int)sampleRate,
                Channels      = channels,
                ByteRate      = (int)byteRate,
                BitsPerSample = 16,
                BlockAlign    = channels * 2,
                ExtraData     = new byte[0]
            };

            container.Chunks.Add(_formatEncoder.Encode(format));

            var totalSamples = sound.Samples.Max(s => s.Data.Count);

            using (var stream = new MemoryStream())
                using (var writer = new BinaryWriter(stream))
                {
                    for (var i = 0; i < totalSamples; i++)
                    {
                        for (var j = 0; j < sound.Samples.Count; j++)
                        {
                            var sample      = sound.Samples[j];
                            var source      = sample.Data;
                            var sourceValue = i < source.Count ? source[i] : 0f;
                            var value       = Math.Round(sourceValue * 32767f);
                            if (value > 32767f)
                            {
                                value = 32767f;
                            }
                            else if (value < -32767f)
                            {
                                value = -32767f;
                            }
                            writer.Write((short)value);
                        }
                    }

                    container.Chunks.Add(new RiffChunk
                    {
                        Id   = "data",
                        Data = stream.ToArray()
                    });
                }

            return(container);
        }
Ejemplo n.º 10
0
		private RiffChunk ReadChunk(BinaryReader br)
		 {
			RiffChunk ret;
			string tag = ReadTag(br); readCounter += 4;
			uint size = br.ReadUInt32(); readCounter += 4;
			if (size > int.MaxValue)
				throw new FormatException("chunk too big");
			if (tag == "RIFF" || tag == "LIST")
			{
				RiffContainer rc = new RiffContainer
					{
						tag = tag,
						type = ReadTag(br)
					};

				readCounter += 4;
				long readEnd = readCounter - 4 + size;
				while (readEnd > readCounter)
					rc.subchunks.Add(ReadChunk(br));
				ret = rc.Morph();
			}
			else
			{
				RiffSubchunk rsc = new RiffSubchunk
					{
						tag = tag,
						Source = br.BaseStream,
						Position = br.BaseStream.Position,
						Length = size
					};
				readCounter += size;
				br.BaseStream.Position += size;
				ret = rsc.Morph();
			}
			if (size % 2 != 0)
			{
				br.ReadByte();
				readCounter += 1;
			}
			return ret;

		}
Ejemplo n.º 11
0
			public RiffContainer_INFO(RiffContainer rc)
			{
				subchunks = rc.subchunks;
				type = "INFO";
				foreach (RiffChunk chunk in subchunks)
				{
					RiffSubchunk rsc = chunk as RiffSubchunk;
					if (chunk == null)
						throw new FormatException("Invalid subchunk of INFO list");
					dictionary[rsc.tag] = System.Text.Encoding.ASCII.GetString(rsc.ReadAll());
				}
			}