vorbisfile.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. /*
  5. Package ov implements the Go bindings of a subset of the functions of the Ogg Vorbis File C library.
  6. It also implements a loader so the library can be dynamically loaded.
  7. The libvorbisfile C API reference is at: https://xiph.org/vorbis/doc/vorbisfile/reference.html
  8. */
  9. package ov
  10. // #include <stdlib.h>
  11. // #include "vorbis/vorbisfile.h"
  12. // #include "loader.h"
  13. import "C"
  14. import (
  15. "fmt"
  16. "unsafe"
  17. )
  18. // File type encapsulates a pointer to C allocated OggVorbis_File structure
  19. type File struct {
  20. vf *C.OggVorbis_File
  21. }
  22. type VorbisInfo struct {
  23. Version int
  24. Channels int
  25. Rate int
  26. BitrateUpper int
  27. BitrateNominal int
  28. BitrateLower int
  29. BitrateWindow int
  30. }
  31. const (
  32. Eread = C.OV_EREAD
  33. Efault = C.OV_EFAULT
  34. Eimpl = C.OV_EIMPL
  35. Einval = C.OV_EINVAL
  36. EnotVorbis = C.OV_ENOTVORBIS
  37. EbadHeader = C.OV_EBADHEADER
  38. Eversion = C.OV_EVERSION
  39. EnotAudio = C.OV_ENOTAUDIO
  40. EbadPacket = C.OV_EBADPACKET
  41. EbadLink = C.OV_EBADLINK
  42. EnoSeek = C.OV_ENOSEEK
  43. )
  44. // Maps ogg vorbis error codes to string
  45. var errCodes = map[C.int]string{
  46. C.OV_EREAD: "Eread",
  47. C.OV_EFAULT: "Efault",
  48. C.OV_EIMPL: "Eimpl",
  49. C.OV_EINVAL: "Einval",
  50. C.OV_ENOTVORBIS: "EnotVorbis",
  51. C.OV_EVERSION: "Eversion",
  52. C.OV_ENOTAUDIO: "EnotAudio",
  53. C.OV_EBADPACKET: "EbadPacket",
  54. C.OV_EBADLINK: "EbadLink",
  55. C.OV_ENOSEEK: "EnoSeek",
  56. }
  57. // Flag indicating if library has been loaded
  58. var loaded = false
  59. // Load tries to load dinamically the libvorbisfile shared library/dll.
  60. // Most of the functions of this package can only be called only
  61. // after the library was successfully loaded.
  62. func Load() error {
  63. // Checks if already loaded
  64. if loaded {
  65. return nil
  66. }
  67. // Loads libvorbisfile
  68. cres := C.vorbisfile_load()
  69. if cres == 0 {
  70. loaded = true
  71. return nil
  72. }
  73. return fmt.Errorf("Error loading libvorbisfile shared library/dll")
  74. }
  75. // IsLoaded returns if library has been loaded succesfully
  76. func IsLoaded() bool {
  77. return loaded
  78. }
  79. // Fopen opens an ogg vorbis file for decoding
  80. // Returns an opaque pointer to the internal decode structure and an error
  81. func Fopen(path string) (*File, error) {
  82. checkLoaded()
  83. // Allocates pointer to vorbisfile structure using C memory
  84. var f File
  85. f.vf = (*C.OggVorbis_File)(C.malloc(C.size_t(unsafe.Sizeof(C.OggVorbis_File{}))))
  86. fmt.Printf("SIZE:%v\n", unsafe.Sizeof(C.OggVorbis_File{}))
  87. cpath := C.CString(path)
  88. defer C.free(unsafe.Pointer(cpath))
  89. cerr := C.ov_fopen(cpath, f.vf)
  90. if cerr == 0 {
  91. return &f, nil
  92. }
  93. return nil, fmt.Errorf("Error:%s from Fopen", errCodes[cerr])
  94. }
  95. // Clear clears the decoded buffers and closes the file
  96. func Clear(f *File) error {
  97. checkLoaded()
  98. cerr := C.ov_clear(f.vf)
  99. if cerr == 0 {
  100. C.free(unsafe.Pointer(f.vf))
  101. f.vf = nil
  102. return nil
  103. }
  104. return fmt.Errorf("Error:%s from Clear", errCodes[cerr])
  105. }
  106. // Read decodes next data from the file updating the specified buffer contents and
  107. // returns the number of bytes read, the number of current logical bitstream and an error
  108. func Read(f *File, buffer unsafe.Pointer, length int, bigendianp bool, word int, sgned bool) (int, int, error) {
  109. checkLoaded()
  110. var cbigendianp C.int = 0
  111. var csgned C.int = 0
  112. var bitstream C.int
  113. if bigendianp {
  114. cbigendianp = 1
  115. }
  116. if sgned {
  117. csgned = 1
  118. }
  119. cres := C.ov_read(f.vf, (*C.char)(buffer), C.int(length), cbigendianp, C.int(word), csgned, &bitstream)
  120. if cres < 0 {
  121. return 0, 0, fmt.Errorf("Error:%s from Read()", errCodes[C.int(cres)])
  122. }
  123. return int(cres), int(bitstream), nil
  124. }
  125. // Info updates the specified VorbisInfo structure with contains basic
  126. // information about the audio in a vorbis stream
  127. func Info(f *File, link int, info *VorbisInfo) error {
  128. checkLoaded()
  129. vi := C.ov_info(f.vf, C.int(link))
  130. if vi == nil {
  131. return fmt.Errorf("Error returned from 'ov_info'")
  132. }
  133. info.Version = int(vi.version)
  134. info.Channels = int(vi.channels)
  135. info.Rate = int(vi.rate)
  136. info.BitrateUpper = int(vi.bitrate_upper)
  137. info.BitrateNominal = int(vi.bitrate_nominal)
  138. info.BitrateLower = int(vi.bitrate_lower)
  139. info.BitrateWindow = int(vi.bitrate_window)
  140. return nil
  141. }
  142. // Seekable returns indication whether or not the bitstream is seekable
  143. func Seekable(f *File) bool {
  144. checkLoaded()
  145. cres := C.ov_seekable(f.vf)
  146. if cres == 0 {
  147. return false
  148. }
  149. return true
  150. }
  151. // Seek seeks to the offset specified (in number pcm samples) within the physical bitstream.
  152. // This function only works for seekable streams.
  153. // Updates everything needed within the decoder, so you can immediately call Read()
  154. // and get data from the newly seeked to position.
  155. func PcmSeek(f *File, pos int64) error {
  156. checkLoaded()
  157. cres := C.ov_pcm_seek(f.vf, C.ogg_int64_t(pos))
  158. if cres == 0 {
  159. return nil
  160. }
  161. return fmt.Errorf("Error:%s from 'ov_pcm_seek()'", errCodes[C.int(cres)])
  162. }
  163. // PcmTotal returns the total number of pcm samples of the physical bitstream or a specified logical bit stream.
  164. // To retrieve the total pcm samples for the entire physical bitstream, the 'link' parameter should be set to -1
  165. func PcmTotal(f *File, i int) (int64, error) {
  166. checkLoaded()
  167. cres := C.ov_pcm_total(f.vf, C.int(i))
  168. if cres < 0 {
  169. return 0, fmt.Errorf("Error:%s from 'ov_pcm_total()'", errCodes[C.int(cres)])
  170. }
  171. return int64(cres), nil
  172. }
  173. // TimeTotal returns the total time in seconds of the physical bitstream or a specified logical bitstream
  174. // To retrieve the time total for the entire physical bitstream, 'i' should be set to -1.
  175. func TimeTotal(f *File, i int) (float64, error) {
  176. checkLoaded()
  177. cres := C.ov_time_total(f.vf, C.int(i))
  178. if cres < 0 {
  179. return 0, fmt.Errorf("Error:%s from 'ov_time_total()'", errCodes[C.int(cres)])
  180. }
  181. return float64(cres), nil
  182. }
  183. // TimeTell returns the current decoding offset in seconds.
  184. func TimeTell(f *File) (float64, error) {
  185. checkLoaded()
  186. cres := C.ov_time_tell(f.vf)
  187. if cres < 0 {
  188. return 0, fmt.Errorf("Error:%s from 'ov_time_total()'", errCodes[C.int(cres)])
  189. }
  190. return float64(cres), nil
  191. }
  192. func checkLoaded() {
  193. if !loaded {
  194. panic("libvorbisfile shared library/dll was not loaded")
  195. }
  196. }