//Set the the Variable Bit Rate encoding quality. The encoding quality value should be between 0.0 (lowest quality) and 1.0 (highest quality). Untested. func (f *File) SetVbrQuality(q float64) (err error) { r := C.sf_command(f.s, C.SFC_SET_VBR_ENCODING_QUALITY, unsafe.Pointer(&q), 8) if r != 0 { err = errors.New(C.GoString(C.sf_strerror(f.s))) } return }
//The SetString() method sets the string data in a file. It returns nil on success and non-nil on error. func (f *File) SetString(in string, typ StringType) (err error) { s := C.CString(in) defer C.free(unsafe.Pointer(s)) if C.sf_set_string(f.s, C.int(typ), s) != 0 { err = errors.New(C.GoString(C.sf_strerror(f.s))) } return }
// Returns a slice full of integers detailing the position of each channel in the file. err will be non-nil on an actual error func (f *File) GetChannelMapInfo() (channels []int32, err error) { channels = make([]int32, f.Format.Channels) r := GenericCmd(f, C.SFC_GET_CHANNEL_MAP_INFO, unsafe.Pointer(&channels[0]), len(channels)*4) if r == C.SF_FALSE { err = errors.New(C.GoString(C.sf_strerror(f.s))) } return }
// Set the Broadcast Extension Chunk from WAV (and related) files. func (f *File) SetBroadcastInfo(bi *BroadcastInfo) (err error) { c := cFromBroadcast(bi) r := C.sf_command(f.s, C.SFC_SET_BROADCAST_INFO, unsafe.Pointer(c), C.int(unsafe.Sizeof(*c))) if r == C.SF_FALSE { err = errors.New(C.GoString(C.sf_strerror(f.s))) } return }
//Change the data start offset for files opened up as SF_FORMAT_RAW. libsndfile implements this but it appears to not do anything useful that you can't accomplish with seek, so consider this deprecated. func (f *File) SetRawStartOffset(count int64) (err error) { r := C.sf_command(f.s, C.SFC_SET_RAW_START_OFFSET, unsafe.Pointer(&count), 8) if r != 0 { err = errors.New(C.GoString(C.sf_strerror(f.s))) } return }
// Truncates a file to /count/ frames. After this command, both the read and the write pointer will be at the new end of the file. This command will fail (returning non-zero) if the requested truncate position is beyond the end of the file. func (f *File) Truncate(count int64) (err error) { r := C.sf_command(f.s, C.SFC_FILE_TRUNCATE, unsafe.Pointer(&count), 8) if r != 0 { err = errors.New(C.GoString(C.sf_strerror(f.s))) } return }
//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 }
func (f *File) SetChannelMapInfo(channels []int32) (err error) { if int32(len(channels)) != f.Format.Channels { err = errors.New("channel map passed in didn't match file channel count " + string(len(channels)) + " != " + string(f.Format.Channels)) } r := GenericCmd(f, C.SFC_SET_CHANNEL_MAP_INFO, unsafe.Pointer(&channels[0]), len(channels)*4) if r == C.SF_FALSE { err = errors.New(C.GoString(C.sf_strerror(f.s))) } return }
//Get the file offset and file length of a file enbedded within another larger file. //The value of the offset return value will be the offsets in bytes from the start of the outer file to the start of the embedded audio file. //The value of the length return value will be the length in bytes of the embedded file. // Untested. func (f *File) GetEmbeddedFileInfo() (offset, length int64, err error) { var s C.SF_EMBED_FILE_INFO r := C.sf_command(f.s, C.SFC_GET_EMBED_FILE_INFO, unsafe.Pointer(&s), C.int(unsafe.Sizeof(s))) if r != 0 { err = errors.New(C.GoString(C.sf_strerror(f.s))) } offset = int64(s.offset) length = int64(s.length) return }
// The close function closes the file, deallocates its internal buffers and returns a non-nil error value in case of error func (f *File) Close() (err error) { if C.sf_close(f.s) != 0 { err = errors.New(C.GoString(C.sf_strerror(f.s))) } if f.closeFd { nf := os.NewFile(f.fd, "") err = nf.Close() } runtime.SetFinalizer(f, nil) return }
//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 }
// This probably won't work on windows, because go uses handles instead of integer file descriptors on Windows. Unfortunately I have no way to test. // The mode and info arguments, and the return values, are the same as for Open(). // close_desc should be true if you want the library to close the file descriptor when you close the sndfile.File object func OpenFd(fd uintptr, mode Mode, info *Info, close_desc bool) (o *File, err error) { if info == nil { return nil, errors.New("nil pointer passed to open") } o = new(File) o.closeFd = close_desc o.fd = fd ci := info.toCinfo() o.s = C.sf_open_fd(C.int(fd), C.int(mode), ci, 0) // don't want libsndfile to close a Go file object from under us if o.s == nil { err = errors.New(C.GoString(C.sf_strerror(o.s))) } *info = fromCinfo(ci) o.Format = *info runtime.SetFinalizer(o, (*File).Close) return }
// returns a pointer to the file and a nil error if successful. In case of error, err will be non-nil. func Open(name string, mode Mode, info *Info) (o *File, err error) { if info == nil { return nil, errors.New("nil pointer passed to open") } o = new(File) c := C.CString(name) defer C.free(unsafe.Pointer(c)) ci := info.toCinfo() o.s = C.sf_open(c, C.int(mode), ci) if o.s == nil { err = errors.New(C.GoString(C.sf_strerror(o.s))) } *info = fromCinfo(ci) o.Format = *info runtime.SetFinalizer(o, (*File).Close) return }
// Opens a soundfile from a virtual file I/O context which is provided by the caller. This is usually used to interface libsndfile to a stream or buffer based system. Apart from the c and user_data parameters this function behaves like sf_open. // THIS PART OF THE PACKAGE IS EXPERIMENTAL. Don't use it yet. func OpenVirtual(v VirtualIo, mode Mode, info *Info) (f *File, err error) { c := C.virtualio() var vp virtualIo vp.v = &v vp.c = c f = new(File) ci := info.toCinfo() f.s = C.sf_open_virtual(c, C.int(mode), ci, unsafe.Pointer(&vp)) if f.s != nil { f.virtual = &vp f.Format = fromCinfo(ci) *info = f.Format } else { err = errors.New(C.GoString(C.sf_strerror(nil))) } runtime.SetFinalizer(f, (*File).Close) return }
/*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 }