1 #include <libusb-1.0/libusb.h>
12 // interface 1 altsetting 1 endpoint 01 (out) bits 16 channels 2 mps 192
13 // interface 2 altsetting 1 endpoint 83 (in) bits 16 channels 2 mps 192
15 int samples_captured
= 0;
16 int samples_played
= 0;
18 static struct libusb_context
*usbctx
;
19 static int omega_timeout
= 1000;
21 int epOUT
= 0x01, epIN
= 0x83;
26 libusb_set_debug(usbctx
, 3);
29 struct libusb_device_handle
*open_omega()
31 struct libusb_device_handle
*handle
;
32 handle
= libusb_open_device_with_vid_pid(usbctx
, 0x1210, 0x0002);
35 printf("Lexicon Omega not found after reset.\n");
38 if (libusb_set_configuration(handle
, 1))
44 if (libusb_claim_interface(handle
, 1))
46 if (libusb_claim_interface(handle
, 2))
48 if (libusb_claim_interface(handle
, 7))
50 if (libusb_set_interface_alt_setting(handle
, 1, 1))
52 if (libusb_set_interface_alt_setting(handle
, 2, 1))
62 #define EP_CONTROL_UNDEFINED 0
63 #define SAMPLING_FREQ_CONTROL 1
64 #define PITCH_CONTROL 2
78 int configure_omega(struct libusb_device_handle
*h
, int sample_rate
)
81 freq_data
[0] = sample_rate
& 0xFF;
82 freq_data
[1] = (sample_rate
& 0xFF00) >> 8;
83 freq_data
[2] = (sample_rate
& 0xFF0000) >> 16;
84 if (libusb_control_transfer(h
, 0x22, 0x01, 256, epOUT
, freq_data
, 3, omega_timeout
) != 3)
86 if (libusb_control_transfer(h
, 0x22, 0x01, 256, epIN
, freq_data
, 3, omega_timeout
) != 3)
88 // libusb_control_transfer(dev, 0x22, 0x01,
92 // 192 bytes = 1ms@48000 (48 samples)
94 #define NUM_PLAY_BUFS 2
95 #define PLAY_PACKET_COUNT 2
96 #define PLAY_PACKET_SIZE 192
98 static float phase
= 0;
99 static int phase2
= 0;
101 static int desync
= 0;
102 static int samples_sent
= 0;
104 static int srate
= 48000;
106 void play_callback(struct libusb_transfer
*transfer
)
109 //printf("Play Callback! %d %p status %d\n", (int)transfer->length, transfer->buffer, (int)transfer->status);
111 samples_played
+= transfer
->length
/ 4;
112 int nsamps
= srate
/ 1000;
113 if (desync
>= 1000 * transfer
->num_iso_packets
&& nsamps
< PLAY_PACKET_SIZE
/4)
115 // printf("desync = %d nsamps = %d!\n", desync, nsamps);
117 for (i
= 0; i
< transfer
->num_iso_packets
; i
++)
119 tlen
+= transfer
->iso_packet_desc
[i
].actual_length
;
120 if (transfer
->iso_packet_desc
[i
].status
)
121 printf("ISO error: index = %d i = %d status = %d\n", (int)transfer
->user_data
, i
, transfer
->iso_packet_desc
[i
].status
);
123 // printf("actual length = %d!\n", tlen);
125 transfer
->length
= nsamps
* transfer
->num_iso_packets
* 4;
126 libusb_set_iso_packet_lengths(transfer
, nsamps
* 4);
128 desync
+= transfer
->num_iso_packets
* srate
;
129 desync
-= tlen
/ 4 * 1000;
131 int16_t *data
= (int16_t*)transfer
->buffer
;
132 for (i
= 0; i
< transfer
->length
/ 4; i
++)
134 float v
= 16000 * sin(phase
);
135 //phase += (phase2 & 4096) ? 0.02 : 0.04;
136 phase
+= (phase2
& 16384) ? 0.04: 0.02;
137 //phase += 0.2 * cos(phase2 / 16384.0);
139 if (phase
> 2 * M_PI
)
143 data
[i
* 2 + 1] = vi
;
145 libusb_submit_transfer(transfer
);
148 void play_stuff(struct libusb_device_handle
*h
, int index
)
150 struct libusb_transfer
*t
;
152 int packets
= PLAY_PACKET_COUNT
;
153 t
= libusb_alloc_transfer(packets
);
154 int tsize
= srate
* 4 / 1000;
155 uint8_t *buf
= (uint8_t *)malloc(PLAY_PACKET_SIZE
*packets
);
158 for (i
= 0; i
< tsize
* packets
; i
++)
161 libusb_fill_iso_transfer(t
, h
, epOUT
, buf
, tsize
* packets
, packets
, play_callback
, (void *)index
, 20000);
162 libusb_set_iso_packet_lengths(t
, tsize
);
163 libusb_submit_transfer(t
);
166 #define NUM_RECORD_BUFS 2
167 #define NUM_REC_PACKETS 2
168 #define REC_PACKET_SIZE 192
170 static uint8_t *record_buffers
[NUM_RECORD_BUFS
];
171 // struct libusb_transfer *record_transfers[NUM_RECORD_BUFS];
175 void record_callback(struct libusb_transfer
*transfer
)
178 // printf("Record callback! %p index %d len %d\n", transfer, (int)transfer->user_data, transfer->length);
179 samples_captured
+= transfer
->length
/ 4;
182 int16_t *bufz
= (int16_t*)transfer
->buffer
;
183 int items
= transfer
->length
/ 4;
184 for (i
= 0; i
< items
; i
++)
186 int16_t *buf
= &bufz
[i
* 2];
187 if (fabs(buf
[0]) > avg
)
189 if (fabs(buf
[1]) > avg
)
193 printf("%12.6f dBFS\r", 6 * log(avg
/ 32767 / items
) / log(2.0));
194 libusb_submit_transfer(transfer
);
197 void record_stuff(struct libusb_device_handle
*h
, int index
)
200 struct libusb_transfer
*t
;
202 record_buffers
[index
] = (uint8_t*)malloc(NUM_REC_PACKETS
* REC_PACKET_SIZE
);
203 memset(record_buffers
[index
], 0, NUM_REC_PACKETS
* REC_PACKET_SIZE
);
205 t
= libusb_alloc_transfer(NUM_REC_PACKETS
);
206 libusb_fill_iso_transfer(t
, h
, epIN
, record_buffers
[index
], NUM_REC_PACKETS
* REC_PACKET_SIZE
, NUM_REC_PACKETS
, record_callback
, (void *)index
, 1000);
207 libusb_set_iso_packet_lengths(t
, REC_PACKET_SIZE
);
208 if (libusb_submit_transfer(t
))
212 printf("Record setup failed for index=%d\n", index
);
217 struct sched_param p
;
218 p
.sched_priority
= 10;
219 sched_setscheduler(0, SCHED_FIFO
, &p
);
221 struct timeval tv
= {
225 libusb_handle_events_timeout(usbctx
, &tv
);
229 int main(int argc
, char *argv
[])
231 struct libusb_device_handle
*h
;
238 printf("Lexicon Omega could not be opened.\n");
242 // 10: 4 3 3 - 16 bit
243 // 30: 2 2 1 2 2 2 1 - 24 bit
246 printf("Error = %d\n", configure_omega(h
, srate
));
248 for (i
= 0; i
< NUM_PLAY_BUFS
; i
++)
250 for (i
= 0; i
< NUM_RECORD_BUFS
; i
++)