changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/ffi/sndfile/pkg.lisp

changeset 698: 96958d3eb5b0
parent: 38e9c3be2392
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 22:04:59 -0400
permissions: -rw-r--r--
description: fixes
1 ;;; sndfile.lisp --- low-level bindings to SNDFILE
2 
3 ;;; Commentary:
4 
5 ;; see http://www.mega-nerd.com/libsndfile/api.html
6 
7 ;;; Code:
8 (defpackage :sndfile
9  (:use :cl :std :sb-alien)
10  (:export :sf-version-string))
11 
12 (in-package :sndfile)
13 
14 (define-alien-loader "sndfile" t "/usr/lib/")
15 
16 (eval-always
17  (define-opaque sndfile t)
18  (define-opaque sf-chunk-iterator t))
19 
20 (define-alien-type sf-count long)
21 
22 (define-alien-type sf-info
23  (struct sf-info
24  (frames sf-count)
25  (samplerate int)
26  (channels int)
27  (format int)
28  (sections int)
29  (seekable int)))
30 
31 (define-alien-type sf-format-info
32  (struct sf-format-info
33  (format int)
34  (name c-string)
35  (extension c-string)))
36 
37 ;; SF_SEEK_*
38 (define-alien-enum (sf-seek-mode int)
39  :set 0
40  :cur 1
41  :end 2)
42 
43 ;; SFD_*
44 (define-alien-enum (sf-dither int)
45  :default-level 0
46  :custom-level #x40000000
47  :no-dither 500
48  :white 501
49  :triangular-pdf 502)
50 
51 (define-alien-type sf-dither-info
52  (struct sf-dither-info
53  (type int)
54  (level double)
55  (name c-string)))
56 
57 (define-alien-type sf-embed-file-info
58  (struct sf-embed-file-info
59  (offset sf-count)
60  (length sf-count)))
61 
62 (define-alien-type sf-cue-point
63  (struct sf-cue-point
64  (indx int)
65  (position unsigned-int)
66  (fcc-chunk int)
67  (chunk-start int)
68  (block-start int)
69  (sample-offset unsigned-int)
70  (name (array char 256))))
71 
72 (define-alien-enum (sf-loop int)
73  :none 800
74  :forward 801
75  :backward 802
76  :alternating 803)
77 
78 (define-alien-type sf-instrument
79  (struct sf-instrument
80  (gain int)
81  (detune char)
82  (basenote char)
83  (velocity-lo char)
84  (velocity-hi char)
85  (key-lo char)
86  (key-hi char)
87  (loop-count int)
88  (loops (array
89  (struct nil
90  (mode int)
91  (start unsigned-int)
92  (end unsigned-int)
93  (count unsigned-int))
94  16))))
95 
96 (define-alien-type sf-loop-info
97  (struct sf-loop-info
98  (time-sig-num short)
99  (time-sig-den short)
100  (loop-mode int)
101  (num-beats int)
102  (bpm float)
103  (root-key int)
104  (future (array int 6))))
105 
106 (define-alien-enum (sf-format int)
107  :wav #x010000 ; Microsoft WAV format (little endian default).
108  :aiff #x020000 ; Apple/SGI AIFF format (big endian).
109  :au #x030000 ; Sun/NeXT AU format (big endian).
110  :raw #x040000 ; RAW PCM data.
111  :paf #x050000 ; Ensoniq PARIS file format.
112  :svx #x060000 ; Amiga IFF / SVX8 / SV16 format.
113  :nist #x070000 ; Sphere NIST format.
114  :voc #x080000 ; VOC files.
115  :ircam #x0A0000 ; Berkeley/IRCAM/CARL
116  :w64 #x0B0000 ; Sonic Foundry's 64 bit RIFF/WAV
117  :mat4 #x0C0000 ; Matlab (tm) V4.2 / GNU Octave 2.0
118  :mat5 #x0D0000 ; Matlab (tm) V5.0 / GNU Octave 2.1
119  :pvf #x0E0000 ; Portable Voice Format
120  :xi #x0F0000 ; Fasttracker 2 Extended Instrument
121  :htk #x100000 ; HMM Tool Kit format
122  :sds #x110000 ; Midi Sample Dump Standard
123  :avr #x120000 ; Audio Visual Research
124  :wavex #x130000 ; MS WAVE with WAVEFORMATEX
125  :sd2 #x160000 ; Sound Designer 2
126  :flac #x170000 ; FLAC lossless file format
127  :caf #x180000) ; Core Audio File format
128 
129 (define-alien-enum (sf-format-subtype int)
130  ;; subtypes
131  :pcm-s8 #x0001 ; Signed 8 bit data
132  :pcm-16 #x0002 ; Signed 16 bit data
133  :pcm-24 #x0003 ; Signed 24 bit data
134  :pcm-32 #x0004 ; Signed 32 bit data
135  :pcm-u8 #x0005 ; Unsigned 8 bit data (WAV and RAW only)
136 
137  :float #x0006 ; 32 bit float data
138  :double #x0007 ; 64 bit float data
139 
140  :ulaw #x0010 ; U-Law encoded.
141  :alaw #x0011 ; A-Law encoded.
142  :ima-adpcm #x0012 ; IMA ADPCM.
143  :ms-adpcm #x0013 ; Microsoft ADPCM.
144 
145  :gsm610 #x0020 ; GSM 6.10 encoding.
146  :vox-adpcm #x0021 ; OKI / Dialogix ADPCM
147 
148  :g721-32 #x0030 ; 32kbs G721 ADPCM encoding.
149  :g723-24 #x0031 ; 24kbs G723 ADPCM encoding.
150  :g723-40 #x0032 ; 40kbs G723 ADPCM encoding.
151  :-dwvw-12 #x0040 ; 12 bit Delta Width Variable Word encoding.
152  :dwvw-16 #x0041 ; 16 bit Delta Width Variable Word encoding.
153  :dwvw-24 #x0042 ; 24 bit Delta Width Variable Word encoding.
154  :dwvw-n #x0043 ; N bit Delta Width Variable Word encoding.
155 
156  :dpcm-8 #x0050 ; 8 bit differential PCM (XI only)
157  :dpcm-16 #x0051) ; 16 bit differential PCM (XI only)
158 
159 (defun decode-bitflags (value flag-names)
160  (loop for symbol in flag-names
161  as flag-value = (symbol-value symbol)
162  when (= flag-value (logand value flag-value))
163  collect symbol))
164 
165 (defun match-field (value flag-names)
166  (loop for symbol in flag-names
167  when (= value (symbol-value symbol))
168  return symbol))
169 
170 ;;;; Endian-ness options.
171 (define-alien-enum (sf-endian int)
172  :file #x00000000 ; Default file endian-ness.
173  :little #x10000000 ; Force little endian-ness.
174  :big #x20000000 ; Force big endian-ness.
175  :cpu #x30000000) ; Force CPU endian-ness.
176 
177 
178 (define-alien-enum (sf-format-mask int)
179  :sub #x0000FFFF
180  :type #x0FFF0000
181  :end #x30000000)
182 
183 (define-alien-enum (sf-str int)
184  :title 1
185  :copyright 2
186  :software 3
187  :artist 4
188  :comment 5
189  :date 6)
190 
191 ;;;; Public error numbers
192 (define-alien-enum (sf-err int)
193  :no-error 0
194  :unrecognized-format 1
195  :system 2
196  :malformed-file 3
197  :unsupported-encoding 4)
198 
199 ;;;; SF commands
200 (define-alien-enum (sf-command-op int)
201  :get-lib-version #x1000
202  :get-log-info #x1001
203  :get-current-sf-info #x1002
204  :get-norm-double #x1010
205  :get-norm-float #x1011
206  :set-norm-double #x1012
207  :set-norm-float #x1013
208  :set-scale-float-int-read #x1014
209  :set-scale-int-float-write #x1015
210  :get-simple-format-count #x1020
211  :get-simple-format #x1021
212  :get-format-info #x1028
213  :get-format-major-count #x1030
214  :get-format-major #x1031
215  :get-format-subtype-count #x1032
216  :get-format-subtype #x1033
217  :calc-signal-max #x1040
218  :calc-norm-signal-max #x1041
219  :calc-max-all-channels #x1042
220  :calc-norm-max-all-channels #x1043
221  :get-signal-max #x1044
222  :get-max-all-channels #x1045
223  :set-add-peak-chunk #x1050
224  :update-header-now #x1060
225  :set-update-header-auto #x1061
226  :file-truncate #x1080
227  :set-raw-start-offset #x1090
228  ;; /* Commands reserved for dithering, which is not implemented. */
229  :set-dither-on-write #x10A0
230  :set-dither-on-read #x10A1
231  :get-dither-info-count #x10A2
232  :get-dither-info #x10A3
233  :get-embed-file-info #x10B0
234  :set-clipping #x10C0
235  :get-clipping #x10C1
236  :get-cue-count #x10CD
237  :get-cue #x10CE
238  :set-cue #x10CF
239  :get-instrument #x10D0
240  :set-instrument #x10D1
241  :get-loop-info #x10E0
242  :get-broadcast-info #x10F0
243  :set-broadcast-info #x10F1
244  :get-channel-map-info #x1100
245  :set-channel-map-info #x1101
246  :raw-data-needs-endswap #x1110
247  ;; /* Support for Wavex Ambisonics Format */
248  :wavex-set-ambisonic #x1200
249  :wavex-get-ambisonic #x1201
250  ;; RF64 files can be set so that on-close, writable files
251  ;; that have less than 4GB of data in them are converted to
252  ;; RIFF/WAV, as per EBU recommendations.
253  :rf64-auto-downgrade #x1210
254  :set-vbr-encoding-quality #x1300
255  :set-compression-level #x1301
256  ;; /* Ogg format commands */
257  :set-ogg-page-latency-ms #x1302
258  :set-ogg-page-latency #x1303
259  :get-ogg-stream-serialno #x1306
260  :get-bitrate-mode #x1304
261  :set-bitrate-mode #x1305
262  ;; /* Cart Chunk support */
263  :set-cart-info #x1400
264  :get-cart-info #x1401
265  ;; /* Opus files original samplerate metadata */
266  :set-original-samplerate #x1500
267  :get-original-samplerate #x1501
268  ;; /* Following commands for testing only. */
269  :test-ieee-float-replace #x6001
270  ;; These SFC_SET_ADD_* values are deprecated and will
271  ;; disappear at some time in the future. They are
272  ;; guaranteed to be here up to and including version 1.0.8
273  ;; to avoid breakage of existing software. They currently
274  ;; do nothing and will continue to do nothing.
275  :set-add-header-pad-chunk #x1051
276  :set-add-dither-on-write #x1070
277  :set-add-dither-on-read #x1071)
278 
279 ;;; Functions
280 (define-alien-routine sf-open (* sndfile) (path c-string) (mode int) (sfinfo (* sf-info)))
281 (define-alien-routine sf-open-fd (* sndfile) (fd int) (mode int) (sfinfo (* sf-info)))
282 (define-alien-routine sf-error int (sndfile (* sndfile)))
283 (define-alien-routine sf-strerror c-string (sndfile (* sndfile)))
284 (define-alien-routine sf-error-number c-string (errnum int))
285 (define-alien-routine sf-perror int (sndfile (* sndfile)))
286 (define-alien-routine sf-error-str int (sndfile (* sndfile)) (str c-string) (len size-t))
287 (define-alien-routine sf-command int (sndfile (* sndfile)) (command int) (data (* t)) (datasize int))
288 (define-alien-routine sf-format-check int (info (* sf-info)))
289 (define-alien-routine sf-seek sf-count (sndfile (* sndfile)) (frames sf-count) (whence int))
290 
291 (define-alien-routine sf-set-string int (sndfile (* sndfile)) (str-type int) (str c-string))
292 (define-alien-routine sf-get-string c-string (sndfile (* sndfile)) (str-type int))
293 (define-alien-routine sf-version-string c-string)
294 (define-alien-routine sf-current-byterate int (sndfile (* sndfile)))
295 (define-alien-routine sf-read-raw sf-count (sndfile (* sndfile)) (ptr (* t)) (bytes sf-count))
296 (define-alien-routine sf-write-raw sf-count (sndfile (* sndfile)) (ptr (* t)) (bytes sf-count))
297 ;; ...
298 (define-alien-routine sf-close int (sndfile (* sndfile)))
299 
300 (define-alien-routine sf-write-sync void
301  (sndfile (* sndfile)))
302 
303 (define-alien-type sf-chunk-info
304  (struct sf-chunk-info
305  (id (array char 64))
306  (id-size unsigned)
307  (datalen unsigned)
308  (data (* t))))
309 
310 (define-alien-routine sf-set-chunk int
311  (sndfile (* sndfile))
312  (chunk-info (* sf-chunk-info)))
313 
314 (define-alien-routine sf-get-chunk-iterator (* sf-chunk-iterator)
315  (sndfile (* sndfile))
316  (chunk-info (* sf-chunk-info)))
317 
318 (define-alien-routine sf-next-chunk-iterator (* sf-chunk-iterator)
319  (iterator (* sf-chunk-iterator)))
320 
321 (define-alien-routine sf-get-chunk-size int
322  (it (* sf-chunk-iterator))
323  (chunk-info (* sf-chunk-info)))
324 
325 (define-alien-routine sf-get-chunk-data int
326  (it (* sf-chunk-iterator))
327  (chunk-info (* sf-chunk-info)))