Exemple #1
0
//TODO:
// * Modify lzhuf.c's Encode() so we don't have to use temp files.
// * Handle go errors.
func encDec(data []byte, encode bool) []byte {
	// Create temp files
	outf, _ := ioutil.TempFile("", "lzhuf")
	inf, _ := ioutil.TempFile("", "lzhuf")
	defer func() {
		outf.Close()
		os.Remove(outf.Name())
		os.Remove(inf.Name())
	}()

	// Copy data to in file
	io.Copy(inf, bytes.NewBuffer(data))
	inf.Sync()
	inf.Close()

	// Encode/Decode the inf to outf
	lzs := C.AllocStruct()
	var retval C.int
	if encode {
		retval = C.Encode(0, C.CString(inf.Name()), C.CString(outf.Name()), lzs, 1)
	} else {
		retval = C.Decode(0, C.CString(inf.Name()), C.CString(outf.Name()), lzs, 1)
	}
	C.FreeStruct(lzs)

	if retval < 0 {
		panic("lzhuf encode/decode error")
	}

	// Read the compressed/decompressed data from outf
	b, _ := ioutil.ReadAll(outf)

	return b
}
Exemple #2
0
func Decode(r io.Reader) (f *audio.File, e error) {
	f = &audio.File{}
	c := &container{
		file: f,
	}
	c.decoder = C.FLAC__stream_decoder_new()
	C.FLAC__stream_decoder_set_md5_checking(c.decoder, C.FLAC__bool(0))
	c.input.buffer, e = ioutil.ReadAll(r)
	if e != nil {
		return
	}
	C.Decode(unsafe.Pointer(c))
	return
}