Example #1
0
// Read reads a full mp3 frame. If p is too small to hold the frame,
// return the part that does fit (and io.ErrShortBuffer) and discard
// the rest of the frame.
func (mr *MP3Reader) Read(p []byte) (n int, err error) {
	var f mp3.Frame
	err = mr.d.Decode(&f)
	if err != nil {
		if err == io.ErrUnexpectedEOF {
			err = io.EOF
		}
		return
	}
	r := f.Reader()

	var x int
	for {
		x, err = r.Read(p)
		n += x
		if len(p) == 0 && err == nil {
			err = io.ErrShortBuffer
		}
		if err != nil {
			break
		}
		p = p[x:]
	}
	if err == io.EOF {
		err = nil
	}
	return
}
Example #2
0
func (hub *Hub) streamStep() {
	r, err := os.Open(hub.track)
	defer r.Close()

	if err != nil {
		log.Println(err)
		return
	}

	d := mp3.NewDecoder(r)
	var f mp3.Frame

	for {
		if err := d.Decode(&f); err != nil {
			log.Println(err)
			return
		}
		b := make([]byte, f.Size())

		f.Reader().Read(b)
		hub.SendMessage(b)

		time.Sleep(f.Duration())
	}
}
Example #3
0
func getMp3Length(path string) (totalTime int64) {
	// Returns length in milliseconds
	r, err := os.Open(path)
	if err != nil {
		//fmt.Println(err)
		return
	}

	d := mp3.NewDecoder(r)
	var f mp3.Frame
	totalTime = 0
	for {

		if err := d.Decode(&f); err != nil {
			//fmt.Println(err)
			return
		}

		totalTime += f.Duration().Nanoseconds() / 1000000
	}
}