コード例 #1
0
ファイル: raw.go プロジェクト: mkb218/gosndfile
//The raw read and write functions return the number of bytes read or written (which should be the same as the bytes parameter) and any error that occurs while reading or writing
// needs test
func (f *File) WriteRaw(data []byte) (written int64, err error) {
	written = int64(C.sf_write_raw(f.s, unsafe.Pointer(&data[0]), C.sf_count_t(len(data))))
	if written != int64(len(data)) {
		err = sErrorType(C.sf_error(f.s))
	}
	return
}
コード例 #2
0
ファイル: raw.go プロジェクト: mkb218/gosndfile
//The raw read and write functions return the number of bytes read or written (which should be the same as the bytes parameter) and any error that occurs while reading or writing
// needs test
func (f *File) ReadRaw(data []byte) (read int64, err error) {
	read = int64(C.sf_read_raw(f.s, unsafe.Pointer(&data[0]), C.sf_count_t(len(data))))
	if read != int64(len(data)) {
		err = sErrorType(C.sf_error(f.s))
	}
	return
}
コード例 #3
0
ファイル: sndfile.go プロジェクト: krig/gosndfile
//The file seek functions work much like lseek in unistd.h with the exception that the non-audio data is ignored and the seek only moves within the audio data section of the file. In addition, seeks are defined in number of (multichannel) frames. Therefore, a seek in a stereo file from the current position forward with an offset of 1 would skip forward by one sample of both channels. This function returns the new offset, and a non-nil error value if unsuccessful
func (f *File) Seek(frames int64, w Whence) (offset int64, err error) {
	r := C.sf_seek(f.s, C.sf_count_t(frames), C.int(w))
	if r == -1 {
		err = errors.New(C.GoString(C.sf_strerror(f.s)))
	} else {
		offset = int64(r)
	}
	return
}
コード例 #4
0
ファイル: sndfile.go プロジェクト: krig/gosndfile
func (i Info) toCinfo() (out *C.SF_INFO) {
	out = new(C.SF_INFO) // libsndfile makes a copy of this, safe for GC
	out.frames = C.sf_count_t(i.Frames)
	out.samplerate = C.int(i.Samplerate)
	out.channels = C.int(i.Channels)
	out.format = C.int(i.Format)
	out.sections = C.int(i.Sections)
	out.seekable = C.int(i.Seekable)
	return
}
コード例 #5
0
ファイル: sndfile.go プロジェクト: krig/gosndfile
/*The file read items functions fill the array pointed to by out with the requested number of items. The length of out must be a product of the number of channels or an error will occur.

It is important to note that the data type used by the calling program and the data format of the file do not need to be the same. For instance, it is possible to open a 16 bit PCM encoded WAV file and read the data into a slice of floats. The library seamlessly converts between the two formats on-the-fly. See Note 1.

Returns the number of items read. Unless the end of the file was reached during the read, the return value should equal the number of items requested. Attempts to read beyond the end of the file will not result in an error but will cause ReadItems to return less than the number of items requested or 0 if already at the end of the file.

out must be a slice or array of int, int16, int32, float32, or float64.

*/
func (f *File) ReadItems(out interface{}) (read int64, err error) {
	t := reflect.TypeOf(out)
	if t.Kind() != reflect.Array && t.Kind() != reflect.Slice {
		errors.New("You need to give me an array!")
	}

	v := reflect.ValueOf(out)
	l := v.Len()
	o := v.Slice(0, l)
	var n C.sf_count_t
	switch t.Elem().Kind() {
	case reflect.Int16:
		n = C.sf_read_short(f.s, (*C.short)(unsafe.Pointer(o.Index(0).Addr().Pointer())), C.sf_count_t(l))
	case reflect.Int32:
		n = C.sf_read_int(f.s, (*C.int)(unsafe.Pointer(o.Index(0).Addr().Pointer())), C.sf_count_t(l))
	case reflect.Float32:
		n = C.sf_read_float(f.s, (*C.float)(unsafe.Pointer(o.Index(0).Addr().Pointer())), C.sf_count_t(l))
	case reflect.Float64:
		n = C.sf_read_double(f.s, (*C.double)(unsafe.Pointer(o.Index(0).Addr().Pointer())), C.sf_count_t(l))
	case reflect.Int:
		switch t.Bits() {
		case 32:
			n = C.sf_read_int(f.s, (*C.int)(unsafe.Pointer(o.Index(0).Addr().Pointer())), C.sf_count_t(l))
		case 16: // doubtful
			n = C.sf_read_short(f.s, (*C.short)(unsafe.Pointer(o.Index(0).Addr().Pointer())), C.sf_count_t(l))
		default:
			err = errors.New("Unsupported type in read buffer, needs (u)int16, (u)int32, or float type")
		}
	default:
		err = errors.New("Unsupported type in read buffer, needs (u)int16, (u)int32, or float type")
	}
	if err != nil {
		read = -1
		return
	}

	read = int64(n)
	if read < 0 {
		err = errors.New(C.GoString(C.sf_strerror(f.s)))
	}
	return
}
コード例 #6
0
ファイル: sndfile.go プロジェクト: krig/gosndfile
//The file write frames function writes the data in the array or slice pointed to by the input argument to the file. The items parameter must be an integer product of the number of channels or an error will occur.
//
//It is important to note that the data type used by the calling program and the data format of the file do not need to be the same. For instance, it is possible to open a 16 bit PCM encoded WAV file and write the data from a []float32. The library seamlessly converts between the two formats on-the-fly.
//
//Returns the number of frames written (which should be the same as the length of the input parameter divided by the number of channels). err wil be nil except in case of failure
func (f *File) WriteFrames(in interface{}) (written int64, err error) {
	t := reflect.TypeOf(in)
	if t.Kind() != reflect.Array && t.Kind() != reflect.Slice {
		errors.New("You need to give me an array!")
	}

	v := reflect.ValueOf(in)
	l := v.Len()
	o := v.Slice(0, l)
	frames := l / int(f.Format.Channels)
	if frames < 1 {
		err = io.EOF
		return
	}
	var n C.sf_count_t
	p := unsafe.Pointer(o.Index(0).Addr().Pointer())
	switch t.Elem().Kind() {
	case reflect.Int16, reflect.Uint16:
		n = C.sf_writef_short(f.s, (*C.short)(p), C.sf_count_t(frames))
	case reflect.Int32, reflect.Uint32:
		n = C.sf_writef_int(f.s, (*C.int)(p), C.sf_count_t(frames))
	case reflect.Float32:
		n = C.sf_writef_float(f.s, (*C.float)(p), C.sf_count_t(frames))
	case reflect.Float64:
		n = C.sf_writef_double(f.s, (*C.double)(p), C.sf_count_t(frames))
	case reflect.Int, reflect.Uint:
		switch t.Bits() {
		case 32:
			n = C.sf_writef_int(f.s, (*C.int)(p), C.sf_count_t(frames))
		case 16: // doubtful
			n = C.sf_writef_short(f.s, (*C.short)(p), C.sf_count_t(frames))
		default:
			err = errors.New("Unsupported type in written buffer, needs (u)int16, (u)int32, or float type")
		}
	default:
		err = errors.New("Unsupported type in written buffer, needs (u)int16, (u)int32, or float type")
	}
	if err != nil {
		written = -1
		return
	}

	written = int64(n)
	if int(n) != frames {
		err = errors.New(C.GoString(C.sf_strerror(f.s)))
	}
	return
}