Exemplo n.º 1
0
        private bool _ReadV3(BinaryReader r)
        {
            riff_chunk chunk = new riff_chunk();

            char[]          wavchunk = new char[4];
            fmt_chunk       fmt;
            bool            hasfmt;
            long            fpos;
            wavpack_header3 wvh3   = new wavpack_header3();
            bool            result = false;


            hasfmt = false;

            // read and evaluate header
            chunk.Reset();

            chunk.id   = r.ReadChars(4);
            chunk.size = r.ReadUInt32();
            wavchunk   = r.ReadChars(4);

            if (!StreamUtils.StringEqualsArr("WAVE", wavchunk))
            {
                return(result);
            }

            // start looking for chunks
            chunk.Reset();

            while (r.BaseStream.Position < r.BaseStream.Length)
            {
                chunk.id   = r.ReadChars(4);
                chunk.size = r.ReadUInt32();

                if (chunk.size <= 0)
                {
                    break;
                }

                fpos = r.BaseStream.Position;

                if (StreamUtils.StringEqualsArr("fmt ", chunk.id))  // Format chunk found read it
                {
                    if (chunk.size >= 16 /*sizeof(fmt_chunk)*/)
                    {
                        fmt.wformattag       = r.ReadUInt16();
                        fmt.wchannels        = r.ReadUInt16();
                        fmt.dwsamplespersec  = r.ReadUInt32();
                        fmt.dwavgbytespersec = r.ReadUInt32();
                        fmt.wblockalign      = r.ReadUInt16();
                        fmt.wbitspersample   = r.ReadUInt16();

                        hasfmt              = true;
                        result              = true;
                        formatTag           = fmt.wformattag;
                        channelsArrangement = ChannelsArrangements.GuessFromChannelNumber(fmt.wchannels);
                        sampleRate          = (int)fmt.dwsamplespersec;
                        bits    = fmt.wbitspersample;
                        bitrate = (double)fmt.dwavgbytespersec * 8;
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    if ((StreamUtils.StringEqualsArr("data", chunk.id)) && hasfmt)
                    {
                        wvh3.Reset();

                        wvh3.ckID          = r.ReadChars(4);
                        wvh3.ckSize        = r.ReadUInt32();
                        wvh3.version       = r.ReadUInt16();
                        wvh3.bits          = r.ReadUInt16();
                        wvh3.flags         = r.ReadUInt16();
                        wvh3.shift         = r.ReadUInt16();
                        wvh3.total_samples = r.ReadUInt32();
                        wvh3.crc           = r.ReadUInt32();
                        wvh3.crc2          = r.ReadUInt32();
                        wvh3.extension     = r.ReadChars(4);
                        wvh3.extra_bc      = r.ReadByte();
                        wvh3.extras        = r.ReadChars(3);

                        if (StreamUtils.StringEqualsArr("wvpk", wvh3.ckID))  // wavpack header found
                        {
                            result              = true;
                            version             = wvh3.version;
                            channelsArrangement = ChannelsArrangements.GuessFromChannelNumber(2 - (wvh3.flags & 1));
                            samples             = wvh3.total_samples;

                            codecFamily = AudioDataIOFactory.CF_LOSSLESS;

                            // Encoder guess
                            if (wvh3.bits > 0)
                            {
                                if ((wvh3.flags & NEW_HIGH_FLAG_v3) > 0)
                                {
                                    encoder = "hybrid";
                                    if ((wvh3.flags & WVC_FLAG_v3) > 0)
                                    {
                                        encoder += " lossless";
                                    }
                                    else
                                    {
                                        encoder    += " lossy";
                                        codecFamily = AudioDataIOFactory.CF_LOSSY;
                                    }

                                    if ((wvh3.flags & EXTREME_DECORR_v3) > 0)
                                    {
                                        encoder = encoder + " (high)";
                                    }
                                }
                                else
                                {
                                    if ((wvh3.flags & (HIGH_FLAG_v3 | FAST_FLAG_v3)) == 0)
                                    {
                                        encoder     = (wvh3.bits + 3).ToString() + "-bit lossy";
                                        codecFamily = AudioDataIOFactory.CF_LOSSY;
                                    }
                                    else
                                    {
                                        encoder     = (wvh3.bits + 3).ToString() + "-bit lossy";
                                        codecFamily = AudioDataIOFactory.CF_LOSSY;

                                        if ((wvh3.flags & HIGH_FLAG_v3) > 0)
                                        {
                                            encoder += " high";
                                        }
                                        else
                                        {
                                            encoder += " fast";
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if ((wvh3.flags & HIGH_FLAG_v3) == 0)
                                {
                                    encoder = "lossless (fast mode)";
                                }
                                else
                                {
                                    if ((wvh3.flags & EXTREME_DECORR_v3) > 0)
                                    {
                                        encoder = "lossless (high mode)";
                                    }
                                    else
                                    {
                                        encoder = "lossless";
                                    }
                                }
                            }

                            if (sampleRate <= 0)
                            {
                                sampleRate = 44100;
                            }
                            duration = (double)wvh3.total_samples * 1000.0 / sampleRate;
                            if (duration > 0)
                            {
                                bitrate = 8.0 * (sizeInfo.FileSize - tagSize - (double)wvh3.ckSize) / duration;
                            }
                        }
                        break;
                    }
                    else // not a wv file
                    {
                        break;
                    }
                }
            } // while

            return(result);
        }
Exemplo n.º 2
0
        // ---------------------------------------------------------------------------

        private bool _ReadV3(BinaryReader r)
        {
            riff_chunk chunk = new riff_chunk();

            char[]          wavchunk = new char[4];
            fmt_chunk       fmt;
            bool            hasfmt;
            long            fpos;
            wavpack_header3 wvh3   = new wavpack_header3();
            bool            result = false;


            hasfmt = false;

            // read and evaluate header
            chunk.Reset();

            chunk.id   = r.ReadChars(4);
            chunk.size = r.ReadUInt32();
            wavchunk   = r.ReadChars(4);

            if (Utils.StringEqualsArr("WAVE", wavchunk))
            {
                return(result);
            }

            // start looking for chunks
            chunk.Reset();

            while (r.BaseStream.Position < r.BaseStream.Length)
            {
                chunk.id   = r.ReadChars(4);
                chunk.size = r.ReadUInt32();

                if (chunk.size <= 0)
                {
                    break;
                }

                fpos = r.BaseStream.Position;

                if (Utils.StringEqualsArr("fmt ", chunk.id))                   // Format chunk found read it
                {
                    if (chunk.size >= 16 /*sizeof(fmt_chunk)*/)
                    {
                        fmt.wformattag       = r.ReadUInt16();
                        fmt.wchannels        = r.ReadUInt16();
                        fmt.dwsamplespersec  = r.ReadUInt32();
                        fmt.dwavgbytespersec = r.ReadUInt32();
                        fmt.wblockalign      = r.ReadUInt16();
                        fmt.wbitspersample   = r.ReadUInt16();

                        hasfmt      = true;
                        result      = true;
                        FValid      = true;
                        FFormatTag  = fmt.wformattag;
                        FChannels   = fmt.wchannels;
                        FSampleRate = (int)fmt.dwsamplespersec;
                        FBits       = fmt.wbitspersample;
                        FBitrate    = (double)fmt.dwavgbytespersec / 125.0;                      // 125 == 1/8*1000
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    if ((Utils.StringEqualsArr("data", chunk.id)) && hasfmt)
                    {
                        wvh3.Reset();

                        wvh3.ckID          = r.ReadChars(4);
                        wvh3.ckSize        = r.ReadUInt32();
                        wvh3.version       = r.ReadUInt16();
                        wvh3.bits          = r.ReadUInt16();
                        wvh3.flags         = r.ReadUInt16();
                        wvh3.shift         = r.ReadUInt16();
                        wvh3.total_samples = r.ReadUInt32();
                        wvh3.crc           = r.ReadUInt32();
                        wvh3.crc2          = r.ReadUInt32();
                        wvh3.extension     = r.ReadChars(4);
                        wvh3.extra_bc      = r.ReadByte();
                        wvh3.extras        = r.ReadChars(3);

                        if (Utils.StringEqualsArr("wvpk", wvh3.ckID))                           // wavpack header found
                        {
                            result    = true;
                            FValid    = true;
                            FVersion  = wvh3.version;
                            FChannels = 2 - (wvh3.flags & 1);                              // mono flag
                            FSamples  = wvh3.total_samples;

                            // Encoder guess
                            if (wvh3.bits > 0)
                            {
                                if ((wvh3.flags & NEW_HIGH_FLAG_v3) > 0)
                                {
                                    FEncoder = "hybrid";
                                    if ((wvh3.flags & WVC_FLAG_v3) > 0)
                                    {
                                        FEncoder += " lossless";
                                    }
                                    else
                                    {
                                        FEncoder += " lossy";
                                    }

                                    if ((wvh3.flags & EXTREME_DECORR_v3) > 0)
                                    {
                                        FEncoder = FEncoder + " (high)";
                                    }
                                }
                                else
                                {
                                    if ((wvh3.flags & (HIGH_FLAG_v3 | FAST_FLAG_v3)) == 0)
                                    {
                                        FEncoder = (wvh3.bits + 3).ToString() + "-bit lossy";
                                    }
                                    else
                                    {
                                        FEncoder = (wvh3.bits + 3).ToString() + "-bit lossy";
                                        if ((wvh3.flags & HIGH_FLAG_v3) > 0)
                                        {
                                            FEncoder += " high";
                                        }
                                        else
                                        {
                                            FEncoder += " fast";
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if ((wvh3.flags & HIGH_FLAG_v3) == 0)
                                {
                                    FEncoder = "lossless (fast mode)";
                                }
                                else
                                {
                                    if ((wvh3.flags & EXTREME_DECORR_v3) > 0)
                                    {
                                        FEncoder = "lossless (high mode)";
                                    }
                                    else
                                    {
                                        FEncoder = "lossless";
                                    }
                                }
                            }

                            if (FSampleRate <= 0)
                            {
                                FSampleRate = 44100;
                            }
                            FDuration = (double)wvh3.total_samples / FSampleRate;
                            if (FDuration > 0)
                            {
                                FBitrate = 8.0 * (FFileSize - (long)FTagSize - (double)(wvh3.ckSize)) / (FDuration * 1000.0);
                            }
                        }
                        break;
                    }
                    else                     // not a wv file
                    {
                        break;
                    }
                }
            }             // while

            return(result);
        }
Exemplo n.º 3
0
		// ---------------------------------------------------------------------------

		private bool _ReadV3( BinaryReader r )
		{
			riff_chunk chunk = new riff_chunk();
			char[] wavchunk = new char[4];
			fmt_chunk fmt;
			bool hasfmt;
			long fpos;
			wavpack_header3 wvh3 = new wavpack_header3();
			bool result = false;
  
  
			hasfmt = false;

			// read and evaluate header
			chunk.Reset();
  
			chunk.id = r.ReadChars(4);
			chunk.size = r.ReadUInt32();
			wavchunk = r.ReadChars(4);
  
			if ( Utils.StringEqualsArr("WAVE",wavchunk) ) return result;

			// start looking for chunks
			chunk.Reset();
  
			while (r.BaseStream.Position < r.BaseStream.Length) 
			{
				chunk.id = r.ReadChars(4);
				chunk.size = r.ReadUInt32();
  	
				if (chunk.size <= 0) break;
     	
				fpos = r.BaseStream.Position;

				if ( Utils.StringEqualsArr("fmt ",chunk.id) )  // Format chunk found read it
				{
					if ( chunk.size >= 16/*sizeof(fmt_chunk)*/ )
					{
						fmt.wformattag = r.ReadUInt16();
						fmt.wchannels = r.ReadUInt16();
						fmt.dwsamplespersec = r.ReadUInt32();
						fmt.dwavgbytespersec = r.ReadUInt32();
						fmt.wblockalign = r.ReadUInt16();
						fmt.wbitspersample = r.ReadUInt16();

						hasfmt = true;
						result = true;
						FValid = true;
						FFormatTag = fmt.wformattag;
						FChannels = fmt.wchannels;
						FSampleRate = (int)fmt.dwsamplespersec;
						FBits = fmt.wbitspersample;
						FBitrate = (double)fmt.dwavgbytespersec / 125.0; // 125 == 1/8*1000
					} 
					else 
					{
						break;
					}
				}
				else         
				{
					if ( ( Utils.StringEqualsArr("data",chunk.id)) && hasfmt )
					{
						wvh3.Reset();
        		
						wvh3.ckID = r.ReadChars(4);
						wvh3.ckSize = r.ReadUInt32();
						wvh3.version = r.ReadUInt16();
						wvh3.bits = r.ReadUInt16();
						wvh3.flags = r.ReadUInt16();
						wvh3.shift = r.ReadUInt16();
						wvh3.total_samples = r.ReadUInt32();
						wvh3.crc = r.ReadUInt32();
						wvh3.crc2 = r.ReadUInt32();
						wvh3.extension = r.ReadChars(4);
						wvh3.extra_bc = r.ReadByte();
						wvh3.extras = r.ReadChars(3);
        								
						if ( Utils.StringEqualsArr("wvpk",wvh3.ckID) )  // wavpack header found
						{
							result = true;
							FValid = true;
							FVersion = wvh3.version;
							FChannels = 2 - (wvh3.flags & 1);  // mono flag
							FSamples = wvh3.total_samples;

							// Encoder guess
							if (wvh3.bits > 0)
							{
								if ( (wvh3.flags & NEW_HIGH_FLAG_v3) > 0 )
								{
									FEncoder = "hybrid";
									if ( (wvh3.flags & WVC_FLAG_v3) > 0 )
									{
										FEncoder += " lossless";
									} 
									else 
									{
										FEncoder += " lossy";                 					
									}
                 			
									if ((wvh3.flags & EXTREME_DECORR_v3) > 0) 
										FEncoder = FEncoder + " (high)";
								}
								else
								{
									if ( (wvh3.flags & (HIGH_FLAG_v3 | FAST_FLAG_v3)) == 0 )
									{
										FEncoder = ( wvh3.bits + 3 ).ToString() + "-bit lossy";
									} 
									else 
									{
										FEncoder = ( wvh3.bits + 3 ).ToString() + "-bit lossy";
										if ( (wvh3.flags & HIGH_FLAG_v3) > 0 )
										{
											FEncoder += " high";
										} 
										else 
										{
											FEncoder += " fast";
										}
									}
								}
							} 
							else 
							{
								if ( (wvh3.flags & HIGH_FLAG_v3) == 0 )  
								{
									FEncoder = "lossless (fast mode)";
								} 
								else 
								{
									if ( (wvh3.flags & EXTREME_DECORR_v3) > 0 )  
									{
										FEncoder = "lossless (high mode)";
									} 
									else 
									{
										FEncoder = "lossless";
									}
              				
								}
							}
				
							if ( FSampleRate <= 0)  FSampleRate = 44100;
							FDuration = (double)wvh3.total_samples / FSampleRate;
							if (FDuration > 0)  FBitrate = 8.0*(FFileSize  - (long)FTagSize  - (double)(wvh3.ckSize))/(FDuration*1000.0);
						}
						break;
					} 
					else // not a wv file
					{ 
						break;
					}
				}
			} // while

			return result;
		}