vorbisfile.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package ov implements the Go bindings of a subset of the functions of the Ogg Vorbis File C library.
  5. // The libvorbisfile C API reference is at: https://xiph.org/vorbis/doc/vorbisfile/reference.html
  6. package ov
  7. // #cgo darwin CFLAGS: -DGO_DARWIN -I/usr/include/vorbis -I/usr/local/include/vorbis
  8. // #cgo freebsd CFLAGS: -DGO_FREEBSD -I/usr/include/vorbis -I/usr/local/include/vorbis
  9. // #cgo linux CFLAGS: -DGO_LINUX -I/usr/include/vorbis
  10. // #cgo windows CFLAGS: -DGO_WINDOWS -I${SRCDIR}/../windows/libvorbis-1.3.5/include/vorbis -I${SRCDIR}/../windows/libogg-1.3.3/include
  11. // #cgo darwin LDFLAGS: -L/usr/lib -L/usr/local/lib -lvorbisfile
  12. // #cgo freebsd LDFLAGS: -L/usr/lib -L/usr/local/lib -lvorbisfile
  13. // #cgo linux LDFLAGS: -lvorbisfile
  14. // #cgo windows LDFLAGS: -L${SRCDIR}/../windows/bin -llibvorbisfile
  15. // #include <stdlib.h>
  16. // #include "vorbisfile.h"
  17. import "C"
  18. import (
  19. "fmt"
  20. "unsafe"
  21. )
  22. // File type encapsulates a pointer to C allocated OggVorbis_File structure
  23. type File struct {
  24. vf *C.OggVorbis_File
  25. }
  26. type VorbisInfo struct {
  27. Version int
  28. Channels int
  29. Rate int
  30. BitrateUpper int
  31. BitrateNominal int
  32. BitrateLower int
  33. BitrateWindow int
  34. }
  35. const (
  36. Eread = C.OV_EREAD
  37. Efault = C.OV_EFAULT
  38. Eimpl = C.OV_EIMPL
  39. Einval = C.OV_EINVAL
  40. EnotVorbis = C.OV_ENOTVORBIS
  41. EbadHeader = C.OV_EBADHEADER
  42. Eversion = C.OV_EVERSION
  43. EnotAudio = C.OV_ENOTAUDIO
  44. EbadPacket = C.OV_EBADPACKET
  45. EbadLink = C.OV_EBADLINK
  46. EnoSeek = C.OV_ENOSEEK
  47. )
  48. // Maps ogg vorbis error codes to string
  49. var errCodes = map[C.int]string{
  50. C.OV_EREAD: "Eread",
  51. C.OV_EFAULT: "Efault",
  52. C.OV_EIMPL: "Eimpl",
  53. C.OV_EINVAL: "Einval",
  54. C.OV_ENOTVORBIS: "EnotVorbis",
  55. C.OV_EVERSION: "Eversion",
  56. C.OV_ENOTAUDIO: "EnotAudio",
  57. C.OV_EBADPACKET: "EbadPacket",
  58. C.OV_EBADLINK: "EbadLink",
  59. C.OV_ENOSEEK: "EnoSeek",
  60. }
  61. // Fopen opens an ogg vorbis file for decoding
  62. // Returns an opaque pointer to the internal decode structure and an error
  63. func Fopen(path string) (*File, error) {
  64. // Allocates pointer to vorbisfile structure using C memory
  65. var f File
  66. f.vf = (*C.OggVorbis_File)(C.malloc(C.size_t(unsafe.Sizeof(C.OggVorbis_File{}))))
  67. cpath := C.CString(path)
  68. defer C.free(unsafe.Pointer(cpath))
  69. cerr := C.ov_fopen(cpath, f.vf)
  70. if cerr == 0 {
  71. return &f, nil
  72. }
  73. return nil, fmt.Errorf("Error:%s from Fopen", errCodes[cerr])
  74. }
  75. // Clear clears the decoded buffers and closes the file
  76. func Clear(f *File) error {
  77. cerr := C.ov_clear(f.vf)
  78. if cerr == 0 {
  79. C.free(unsafe.Pointer(f.vf))
  80. f.vf = nil
  81. return nil
  82. }
  83. return fmt.Errorf("Error:%s from Clear", errCodes[cerr])
  84. }
  85. // Read decodes next data from the file updating the specified buffer contents and
  86. // returns the number of bytes read, the number of current logical bitstream and an error
  87. func Read(f *File, buffer unsafe.Pointer, length int, bigendianp bool, word int, sgned bool) (int, int, error) {
  88. var cbigendianp C.int = 0
  89. var csgned C.int = 0
  90. var bitstream C.int
  91. if bigendianp {
  92. cbigendianp = 1
  93. }
  94. if sgned {
  95. csgned = 1
  96. }
  97. cres := C.ov_read(f.vf, (*C.char)(buffer), C.int(length), cbigendianp, C.int(word), csgned, &bitstream)
  98. if cres < 0 {
  99. return 0, 0, fmt.Errorf("Error:%s from Read()", errCodes[C.int(cres)])
  100. }
  101. return int(cres), int(bitstream), nil
  102. }
  103. // Info updates the specified VorbisInfo structure with contains basic
  104. // information about the audio in a vorbis stream
  105. func Info(f *File, link int, info *VorbisInfo) error {
  106. vi := C.ov_info(f.vf, C.int(link))
  107. if vi == nil {
  108. return fmt.Errorf("Error returned from 'ov_info'")
  109. }
  110. info.Version = int(vi.version)
  111. info.Channels = int(vi.channels)
  112. info.Rate = int(vi.rate)
  113. info.BitrateUpper = int(vi.bitrate_upper)
  114. info.BitrateNominal = int(vi.bitrate_nominal)
  115. info.BitrateLower = int(vi.bitrate_lower)
  116. info.BitrateWindow = int(vi.bitrate_window)
  117. return nil
  118. }
  119. // Seekable returns indication whether or not the bitstream is seekable
  120. func Seekable(f *File) bool {
  121. cres := C.ov_seekable(f.vf)
  122. if cres == 0 {
  123. return false
  124. }
  125. return true
  126. }
  127. // Seek seeks to the offset specified (in number pcm samples) within the physical bitstream.
  128. // This function only works for seekable streams.
  129. // Updates everything needed within the decoder, so you can immediately call Read()
  130. // and get data from the newly seeked to position.
  131. func PcmSeek(f *File, pos int64) error {
  132. cres := C.ov_pcm_seek(f.vf, C.ogg_int64_t(pos))
  133. if cres == 0 {
  134. return nil
  135. }
  136. return fmt.Errorf("Error:%s from 'ov_pcm_seek()'", errCodes[C.int(cres)])
  137. }
  138. // PcmTotal returns the total number of pcm samples of the physical bitstream or a specified logical bit stream.
  139. // To retrieve the total pcm samples for the entire physical bitstream, the 'link' parameter should be set to -1
  140. func PcmTotal(f *File, i int) (int64, error) {
  141. cres := C.ov_pcm_total(f.vf, C.int(i))
  142. if cres < 0 {
  143. return 0, fmt.Errorf("Error:%s from 'ov_pcm_total()'", errCodes[C.int(cres)])
  144. }
  145. return int64(cres), nil
  146. }
  147. // TimeTotal returns the total time in seconds of the physical bitstream or a specified logical bitstream
  148. // To retrieve the time total for the entire physical bitstream, 'i' should be set to -1.
  149. func TimeTotal(f *File, i int) (float64, error) {
  150. cres := C.ov_time_total(f.vf, C.int(i))
  151. if cres < 0 {
  152. return 0, fmt.Errorf("Error:%s from 'ov_time_total()'", errCodes[C.int(cres)])
  153. }
  154. return float64(cres), nil
  155. }
  156. // TimeTell returns the current decoding offset in seconds.
  157. func TimeTell(f *File) (float64, error) {
  158. cres := C.ov_time_tell(f.vf)
  159. if cres < 0 {
  160. return 0, fmt.Errorf("Error:%s from 'ov_time_total()'", errCodes[C.int(cres)])
  161. }
  162. return float64(cres), nil
  163. }