changelog shortlog graph tags branches changeset files revisions annotate raw help

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

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