Coverage report: /home/ellis/comp/core/ffi/chromaprint/pkg.lisp

KindCoveredAll%
expression230 6.7
branch00nil
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; pkg.lisp --- low-level bindings to CHROMAPRINT
2
 
3
 ;;; Commentary:
4
 
5
 #|
6
 Chromaprint is a library for generating audio fingerprints, mainly to be used with the <a href="https://acoustid.org">AcoustID</a> service.
7
 
8
 It needs raw audio stream (16-bit signed int) on input. The audio can have any
9
 sampling rate and any number of channels. Typically, you would use some native
10
 library for decoding compressed audio files and feed the result into
11
 Chromaprint.  Audio fingerprints returned from the library can be represented
12
 either as base64-encoded strings or 32-bit integer arrays. The base64-encoded
13
 strings are usually what's used externally when you need to send the
14
 fingerprint to a service. You can't directly compare the fingerprints in such
15
 form.  The 32-bit integer arrays are also called "raw fingerprints" and they
16
 represent the internal structure of the fingerprints. If you want to compare
17
 two fingerprints yourself, you probably want them in this form.  |#
18
 ;;; Code:
19
 (defpackage :chromaprint
20
   (:use :cl :std :sb-alien)
21
   (:export :load-chromaprint
22
            :chromaprint-hash-fingerprint
23
            :chromaprint-decode-fingerprint
24
            :chromaprint-dealloc
25
            :chromaprint-algorithm
26
            :chromaprint-context
27
            :chromaprint-get-version
28
            :chromaprint-new
29
            :chromaprint-free
30
            :chromaprint-get-algorithm
31
            :chromaprint-set-option
32
            :chromaprint-get-num-channels
33
            :chromaprint-get-sample-rate
34
            :chromaprint-get-item-duration
35
            :chromaprint-get-item-duration-ms
36
            :chromaprint-get-delay
37
            :chromaprint-get-delay-ms
38
            :chromaprint-start
39
            :chromaprint-feed
40
            :chromaprint-finish
41
            :chromaprint-get-fingerprint
42
            :chromaprint-get-raw-fingerprint
43
            :chromaprint-get-raw-fingerprint-size
44
            :chromaprint-get-fingerprint-hash
45
            :chromaprint-clear-fingerprint
46
            :chromaprint-encode-fingerprint
47
            :with-chromaprint-ctx))
48
 
49
 (in-package :chromaprint)
50
 
51
 (define-alien-loader chromaprint "/usr/lib/")
52
 
53
 (define-alien-enum (chromaprint-algorithm int)
54
   :test1 0
55
   :test2 1
56
   :test3 2
57
   :test4 3
58
   :test5 4
59
   :default 1)
60
 
61
 (define-opaque chromaprint-context)
62
 ;; (define-opaque chromaprint-matcher-context)
63
 
64
 (defar chromaprint-get-version c-string)
65
 
66
 (defar chromaprint-new (* chromaprint-context) (algo int))
67
 
68
 (defar chromaprint-free void (ctx (* chromaprint-context)))
69
 
70
 (defar chromaprint-get-algorithm int (ctx (* chromaprint-context)))
71
 
72
 (defar chromaprint-set-option int (ctx (* chromaprint-context)) (name c-string) (value int))
73
 
74
 (defar chromaprint-get-num-channels int (ctx (* chromaprint-context)))
75
 
76
 (defar chromaprint-get-sample-rate int (ctx (* chromaprint-context)))
77
 
78
 (defar chromaprint-get-item-duration int (ctx (* chromaprint-context)))
79
 
80
 (defar chromaprint-get-item-duration-ms int (ctx (* chromaprint-context)))
81
 
82
 (defar chromaprint-get-delay int (ctx (* chromaprint-context)))
83
 
84
 (defar chromaprint-get-delay-ms int (ctx (* chromaprint-context)))
85
 
86
 (defar chromaprint-start int (ctx (* chromaprint-context)) (sample-rate int) (num-channels int))
87
 
88
 (defar chromaprint-feed int (ctx (* chromaprint-context)) (data (* short)) (size int))
89
 
90
 (defar chromaprint-finish int (ctx (* chromaprint-context)))
91
 
92
 (defar chromaprint-get-fingerprint int (ctx (* chromaprint-context)) (fingerprint (* c-string)))
93
 
94
 (defar chromaprint-get-raw-fingerprint int (ctx (* chromaprint-context)) (fingerprint (* (* unsigned-int))) (size (* int)))
95
 
96
 (defar chromaprint-get-raw-fingerprint-size int (ctx (* chromaprint-context)) (size (* int)))
97
 
98
 (defar chromaprint-get-fingerprint-hash int (ctx (* chromaprint-context)) (hash (* unsigned-int)))
99
 
100
 (defar chromaprint-clear-fingerprint int (ctx (* chromaprint-context)))
101
 
102
 (defar chromaprint-encode-fingerprint int 
103
   (fp (* unsigned-int)) 
104
   (size int)
105
   (algo int)
106
   (encoded-fp (* c-string))
107
   (encoded-size (* int))
108
   (base64 int))
109
 
110
 (defar chromaprint-decode-fingerprint int 
111
   (encoded-fp c-string)
112
   (encoded-size int)
113
   (fp (* (* unsigned)))
114
   (size (* (* int)))
115
   (algo (* int))
116
   (base64 int))
117
 
118
 (defar chromaprint-hash-fingerprint int
119
   (fp (* unsigned))
120
   (size int)
121
   (hash (* unsigned)))
122
 
123
 (defar chromaprint-dealloc void (ptr (* t)))
124
 
125
 ;;; Utils
126
 (defmacro with-chromaprint-ctx ((sym &key (algo :default)
127
                                           (samplerate 44100)
128
                                           (channels 2))
129
                                 &body body)
130
   `(let ((,sym (chromaprint-new (chromaprint-algorithm ,algo))))
131
      (unwind-protect (progn
132
                        (chromaprint-start ,sym ,samplerate ,channels)
133
                        ,@body)
134
        (chromaprint-free ,sym))))