Beispiel #1
0
func (m *scannedMp3) scan(ips io.ReadSeeker) os.Error {
	temp := make([]byte, MAX_MPAFRAME_SIZE)

	jh := new(MyCountingJunkHandler)
	mpafp := &mpaFrameParser{ips: ips, junkh: jh}

	filter := FILTER_LAYER3
	var fh *mp3agic.FrameHeader
	frameCounter := 0
	firstFrameFound := false
	isMPEG1 := false
	firstkbps := 0
	sumMusicFrameSize := 0
	//try {
	for {
		fh, err := mpafp.getNextFrame(filter, temp, fh)
		if err != nil && err != os.EOF {
			return err
		}
		frameSize := fh.LengthInBytes()
		//int frameSize = fh.getFrameSize();
		if !firstFrameFound {
			firstFrameFound = true
			firstkbps = fh.BitrateInKbps()
			m.samplesPerFrame = fh.SamplesPerFrame()
			isMPEG1 = (fh.Version() == mp3agic.MPEG_VERSION_1_0)
			if isMPEG1 {
				m.maxRes = 511
			} else {
				m.maxRes = 255
			}
			filter = getFilterFor(fh)
			m.firstFrameHeader = fh
			if m.xiltFrame.parse(temp, 0) {
				if m.xiltFrame.hasXingTag {
					m.isVBR = true
				}
				frameCounter--
				if m.xiltFrame.hasLameTag {
					m.encDelay = m.xiltFrame.encDelay
					m.encPadding = m.xiltFrame.encPadding
				}
			}
		} else {
			checkBitRate := true
			if frameCounter == 0 {
				checkBitRate = false
				// first music frame. might be a PCUT-tag
				// reservoir-filler frame
				sie := fh.SideInfoEnd()
				// a pcut frame contains its tag in the first 10
				// bytes of the
				// main data section
				pcutFrame := (sie+10 <= frameSize) && (temp[sie] == 0x50) && // P
					(temp[sie+1] == 0x43) && // C
					(temp[sie+2] == 0x55) && // U
					(temp[sie+3] == 0x54) // T
				if pcutFrame {
					// temp[sie+4] tag revision (always 0 for now)
					t := int64(temp[sie+5]) // fetch 40 bit start sample
					t = (t << 8) | int64(temp[sie+6])
					t = (t << 8) | int64(temp[sie+7])
					t = (t << 8) | int64(temp[sie+8])
					t = (t << 8) | int64(temp[sie+9])
					m.startSample = t
				} else {
					for b := range temp[fh.SideInfoStart():fh.SideInfoEnd()] {
						if b != 0 {
							checkBitRate = true
							break
						}
					}
				}
			}
			// we don't want the first "music frame" to be checked
			// if it's
			// possibly a PCUT generated reservoir frame
			if checkBitRate && fh.BitrateInKbps() != firstkbps {
				m.isVBR = true
			}
		}
		if frameCounter >= 0 {
			sumMusicFrameSize += frameSize
			accessFrameRecord(frameCounter)
			setFrameFileOfs(int(*jh))
			setFrameSize(frameSize)
			sis := fh.SideInfoStart()
			ofs := sis
			brPointer := int(temp[ofs])
			if isMPEG1 {
				brPointer = (brPointer << 1) | int((temp[ofs+1]&0x80)>>7)
			}
			setBitResPtr(brPointer)
			setMainDataSectionSize(frameSize - sis - fh.SideInfoSize())
		}
		jh.Inc(frameSize)
		frameCounter++
	}
	//}
	//catch (EOFException x) {
	//}

	m.musicFrameCount = frameCounter
	if !firstFrameFound {
		return os.NewError("no mp3 data found")
	}

	var framerate float32 = float32(m.firstFrameHeader.SampleRate()) /
		float32(m.firstFrameHeader.SamplesPerFrame())
	m.avgBitrate = (float32(sumMusicFrameSize) / float32(m.musicFrameCount)) * framerate / 125
	/*
		} finally {
		    try {
		        ips.close();
		    }
		    catch (IOException x) {
		    }
		}
	*/
	return nil
}