active-call 0.3.119

A SIP/WebRTC voice agent
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
use super::{SynthesisClient, SynthesisEvent, SynthesisOption, SynthesisType};
use anyhow::{Context, Result, bail, ensure};
use async_trait::async_trait;
use base64::{Engine, prelude::BASE64_STANDARD};
use bytes::Bytes;
use futures::{StreamExt, stream::BoxStream};
use serde_json::{Value, json};
use std::{io::Cursor, time::Duration};
use tokio::sync::mpsc;

const ENDPOINT: &str = "https://api.60db.ai/tts-synthesize";
const MAX_RESPONSE_BYTES: usize = 32 * 1024 * 1024;
type Command = (String, Option<usize>, SynthesisOption);

pub struct SixtyDBTtsClient {
    option: SynthesisOption,
    streaming: bool,
    client: reqwest::Client,
    tx: Option<mpsc::UnboundedSender<Command>>,
}

impl SixtyDBTtsClient {
    pub fn create(streaming: bool, option: &SynthesisOption) -> Result<Box<dyn SynthesisClient>> {
        let mut option = option.clone();
        option.check_default();
        validate(&option)?;
        let client = reqwest::Client::builder()
            .timeout(Duration::from_secs(60))
            .redirect(reqwest::redirect::Policy::none())
            .build()?;
        Ok(Box::new(Self {
            option,
            streaming,
            client,
            tx: None,
        }))
    }
}

fn validate(option: &SynthesisOption) -> Result<()> {
    ensure!(
        option
            .secret_key
            .as_deref()
            .is_some_and(|s| !s.trim().is_empty()),
        "60db TTS requires an API key"
    );
    let voice = option
        .speaker
        .as_deref()
        .context("60db TTS requires a speaker from GET /voices")?;
    uuid::Uuid::parse_str(voice).context("60db TTS speaker must be a voice UUID")?;
    ensure!(
        matches!(option.samplerate.unwrap_or(16000), 16000 | 24000 | 48000),
        "60db TTS supports 16000, 24000 or 48000 Hz"
    );
    ensure!(
        matches!(option.codec.as_deref().unwrap_or("pcm"), "pcm" | "linear16"),
        "60db TTS requires PCM16 output"
    );
    let speed = option.speed.unwrap_or(1.0);
    ensure!(
        speed.is_finite() && (0.5..=2.0).contains(&speed),
        "60db TTS speed must be between 0.5 and 2.0"
    );
    let url = reqwest::Url::parse(option.endpoint.as_deref().unwrap_or(ENDPOINT))
        .context("Invalid 60db TTS endpoint")?;
    ensure!(
        matches!(url.scheme(), "http" | "https")
            && url.username().is_empty()
            && url.password().is_none(),
        "Invalid 60db TTS endpoint"
    );
    Ok(())
}

fn decode_audio(encoded: &str, rate: i32) -> Result<Bytes> {
    let mut bytes = BASE64_STANDARD
        .decode(encoded)
        .context("Invalid 60db audio base64")?;
    if bytes.first() == Some(&b'{') {
        let inner: Value = serde_json::from_slice(&bytes).context("Invalid 60db audio wrapper")?;
        let audio = inner
            .get("result")
            .unwrap_or(&inner)
            .get("audioContent")
            .and_then(Value::as_str)
            .context("Missing 60db wrapped audio")?;
        bytes = BASE64_STANDARD
            .decode(audio)
            .context("Invalid 60db wrapped audio base64")?;
    }
    if bytes.starts_with(b"RIFF") {
        ensure!(bytes.len() >= 12, "Invalid 60db WAV header");
        let declared = u32::from_le_bytes(bytes[4..8].try_into()?) as usize;
        ensure!(
            declared.checked_add(8) == Some(bytes.len()),
            "Invalid 60db WAV length"
        );
        let mut wav = hound::WavReader::new(Cursor::new(bytes)).context("Invalid 60db WAV")?;
        let spec = wav.spec();
        ensure!(
            spec.channels == 1
                && spec.bits_per_sample == 16
                && spec.sample_format == hound::SampleFormat::Int
                && spec.sample_rate == rate as u32,
            "60db WAV must match mono PCM16 at the configured sample rate"
        );
        bytes = wav
            .samples::<i16>()
            .collect::<std::result::Result<Vec<_>, _>>()?
            .into_iter()
            .flat_map(i16::to_le_bytes)
            .collect();
    }
    ensure!(
        !bytes.is_empty() && bytes.len() % 2 == 0,
        "60db returned invalid PCM16 audio"
    );
    ensure!(
        !bytes.starts_with(b"ID3") && !bytes.starts_with(b"OggS") && !bytes.starts_with(b"fLaC"),
        "60db returned compressed audio instead of PCM16"
    );
    Ok(Bytes::from(bytes))
}

fn parse_record(record: &[u8], rate: i32) -> Result<Option<Bytes>> {
    let value: Value = serde_json::from_slice(record).context("Invalid 60db JSON response")?;
    ensure!(
        value.get("success") != Some(&Value::Bool(false))
            && value.get("type").and_then(Value::as_str) != Some("error")
            && value.get("error").is_none_or(Value::is_null),
        "60db TTS synthesis failed"
    );
    let result = value
        .get("backendResponse")
        .or_else(|| value.get("result"))
        .unwrap_or(&value);
    ensure!(
        result.get("success") != Some(&Value::Bool(false))
            && result.get("error").is_none_or(Value::is_null),
        "60db TTS synthesis failed"
    );
    for metadata in [&value, result] {
        if let Some(actual) = metadata.get("sample_rate").and_then(Value::as_i64) {
            ensure!(
                actual == rate as i64,
                "60db returned an unexpected sample rate"
            );
        }
        for key in ["encoding", "output_format"] {
            if let Some(encoding) = metadata.get(key).and_then(Value::as_str) {
                ensure!(
                    matches!(
                        encoding.to_ascii_lowercase().as_str(),
                        "linear16" | "pcm" | "pcm16" | "wav"
                    ),
                    "60db returned an unsupported audio format"
                );
            }
        }
    }
    match result
        .get("audioContent")
        .or_else(|| result.get("audio_base64"))
    {
        Some(Value::String(audio)) if !audio.is_empty() => Ok(Some(decode_audio(audio, rate)?)),
        Some(Value::String(_)) | None => Ok(None),
        _ => bail!("Invalid 60db audio field"),
    }
}

fn check(condition: bool, message: &str) -> Result<()> {
    ensure!(condition, "{}", message);
    Ok(())
}

fn audio_stream(
    client: reqwest::Client,
    option: SynthesisOption,
    text: String,
) -> BoxStream<'static, Result<SynthesisEvent>> {
    Box::pin(async_stream::try_stream! {
        validate(&option)?;
        check(!text.trim().is_empty() && text.chars().count() <= 5000, "60db TTS text must contain 1 to 5000 characters")?;
        let rate = option.samplerate.unwrap_or(16000);
        let mut payload = json!({
            "text": text,
            "voice_id": option.speaker,
            "audio_config": {"audio_encoding": "LINEAR16", "sample_rate_hertz": rate},
            "speed": option.speed.unwrap_or(1.0),
            "timestamp_type": "NONE"
        });
        if let Some(language) = &option.language { payload["target_language"] = json!(language); }
        let response = client.post(option.endpoint.as_deref().unwrap_or(ENDPOINT))
            .bearer_auth(option.secret_key.as_deref().unwrap_or_default())
            .json(&payload).send().await.map_err(|_| anyhow::anyhow!("60db TTS request failed"))?;
        check(response.status().is_success(), &format!("60db TTS returned HTTP {}", response.status().as_u16()))?;
        let content_type = response.headers().get(reqwest::header::CONTENT_TYPE)
            .and_then(|v| v.to_str().ok()).unwrap_or("").to_ascii_lowercase();
        let ndjson = content_type.contains("ndjson") || content_type.contains("jsonl");
        let binary = content_type.contains("audio/wav") || content_type.contains("audio/x-wav") || content_type.contains("audio/pcm") || content_type.contains("application/octet-stream");
        check(ndjson || binary || content_type.contains("json"), "60db TTS returned an unsupported content type")?;
        let mut stream = response.bytes_stream();
        let mut pending = Vec::new();
        let mut total = 0usize;
        let mut emitted = false;
        while let Some(chunk) = stream.next().await {
            let chunk = chunk.map_err(|_| anyhow::anyhow!("60db TTS response interrupted"))?;
            total = total.checked_add(chunk.len()).context("60db response size overflow")?;
            check(total <= MAX_RESPONSE_BYTES, "60db TTS response exceeds size limit")?;
            pending.extend_from_slice(&chunk);
            if ndjson {
                while let Some(end) = pending.iter().position(|b| *b == b'\n') {
                    let record: Vec<_> = pending.drain(..=end).collect();
                    if record.iter().all(u8::is_ascii_whitespace) { continue; }
                    if let Some(audio) = parse_record(&record, rate)? {
                        emitted = true;
                        yield SynthesisEvent::AudioChunk(audio);
                    }
                }
            }
        }
        if binary {
            check(!pending.is_empty(), "60db TTS returned no audio")?;
            let audio = decode_audio(&BASE64_STANDARD.encode(&pending), rate)?;
            emitted = true;
            yield SynthesisEvent::AudioChunk(audio);
        } else {
            let audio = if pending.iter().all(u8::is_ascii_whitespace) {
                None
            } else {
                parse_record(&pending, rate)?
            };
            if let Some(audio) = audio {
                emitted = true;
                yield SynthesisEvent::AudioChunk(audio);
            }
        }
        check(emitted, "60db TTS returned no audio")?;
    })
}

#[async_trait]
impl SynthesisClient for SixtyDBTtsClient {
    fn provider(&self) -> SynthesisType {
        SynthesisType::SixtyDB
    }

    async fn start(
        &mut self,
    ) -> Result<BoxStream<'static, (Option<usize>, Result<SynthesisEvent>)>> {
        ensure!(self.tx.is_none(), "60db TTS client is already started");
        let (tx, mut rx) = mpsc::unbounded_channel::<Command>();
        self.tx = Some(tx);
        let client = self.client.clone();
        let streaming = self.streaming;
        Ok(Box::pin(async_stream::stream! {
            let mut failed = false;
            while let Some((text, seq, option)) = rx.recv().await {
                let mut audio = audio_stream(client.clone(), option, text);
                let mut command_failed = false;
                while let Some(event) = audio.next().await {
                    if event.is_err() { command_failed = true; failed = true; }
                    yield (seq, event);
                }
                if !streaming && !command_failed { yield (seq, Ok(SynthesisEvent::Finished)); }
            }
            if streaming && !failed { yield (None, Ok(SynthesisEvent::Finished)); }
        }))
    }

    async fn synthesize(
        &mut self,
        text: &str,
        cmd_seq: Option<usize>,
        option: Option<SynthesisOption>,
    ) -> Result<()> {
        let option = self.option.merge_with(option);
        validate(&option)?;
        ensure!(
            option.samplerate.unwrap_or(16000) == self.option.samplerate.unwrap_or(16000),
            "60db TTS sample rate cannot change within a track"
        );
        self.tx
            .as_ref()
            .context("60db TTS client is not started")?
            .send((text.to_owned(), cmd_seq, option))
            .map_err(|_| anyhow::anyhow!("60db TTS stream closed"))?;
        Ok(())
    }

    async fn stop(&mut self) -> Result<()> {
        // End-of-stream closes input; accepted requests drain until the output stream is dropped.
        self.tx.take();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::{
        Mock, MockServer, ResponseTemplate,
        matchers::{body_json, header, method, path},
    };

    const VOICE: &str = "038cf0d1-eef8-45a6-81b0-99c5e57a33d2";

    fn option(endpoint: String) -> SynthesisOption {
        SynthesisOption {
            provider: Some(SynthesisType::SixtyDB),
            secret_key: Some("local-test-token".into()),
            speaker: Some(VOICE.into()),
            endpoint: Some(endpoint),
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn http_contract_order_and_stream_completion() {
        let server = MockServer::start().await;
        for text in ["First.", "Second."] {
            Mock::given(method("POST"))
                .and(path("/tts-synthesize"))
                .and(header("authorization", "Bearer local-test-token"))
                .and(body_json(json!({"text": text, "voice_id": VOICE,
                    "audio_config": {"audio_encoding": "LINEAR16", "sample_rate_hertz": 16000},
                    "speed": 1.0, "timestamp_type": "NONE"})))
                .respond_with(ResponseTemplate::new(200).set_body_json(json!({
                    "success": true, "audio_base64": BASE64_STANDARD.encode([1, 0, 2, 0]),
                    "sample_rate": 16000, "encoding": "LINEAR16"
                })))
                .expect(1)
                .mount(&server)
                .await;
        }
        let mut client =
            SixtyDBTtsClient::create(true, &option(format!("{}/tts-synthesize", server.uri())))
                .unwrap();
        let mut events = client.start().await.unwrap();
        client.synthesize("First.", None, None).await.unwrap();
        client.synthesize("Second.", None, None).await.unwrap();
        client.stop().await.unwrap();
        let mut audio_count = 0;
        let mut finish_count = 0;
        while let Some((seq, event)) = events.next().await {
            assert_eq!(seq, None);
            match event.unwrap() {
                SynthesisEvent::AudioChunk(bytes) => {
                    assert_eq!(bytes.as_ref(), &[1, 0, 2, 0]);
                    audio_count += 1;
                }
                SynthesisEvent::Finished => {
                    assert_eq!(audio_count, 2);
                    finish_count += 1;
                }
                _ => panic!("unexpected subtitles"),
            }
        }
        assert_eq!((audio_count, finish_count), (2, 1));
    }

    #[tokio::test]
    async fn ndjson_wrapped_audio_and_command_sequences() {
        let server = MockServer::start().await;
        let wrapped = BASE64_STANDARD.encode(
            serde_json::to_vec(
                &json!({"result": {"audioContent": BASE64_STANDARD.encode([1, 0])}}),
            )
            .unwrap(),
        );
        let body = format!(
            "{}\n{}",
            json!({"type":"meta"}),
            json!({"result":{"audioContent":wrapped}})
        );
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(200).set_body_raw(body, "application/x-ndjson"))
            .expect(2)
            .mount(&server)
            .await;
        let mut client = SixtyDBTtsClient::create(false, &option(server.uri())).unwrap();
        let mut events = client.start().await.unwrap();
        client.synthesize("First.", Some(0), None).await.unwrap();
        client.synthesize("Second.", Some(1), None).await.unwrap();
        client.stop().await.unwrap();
        let mut completed = Vec::new();
        let mut audio_count = 0;
        while let Some((seq, event)) = events.next().await {
            match event.unwrap() {
                SynthesisEvent::AudioChunk(bytes) => {
                    assert_eq!(bytes.as_ref(), &[1, 0]);
                    assert_eq!(seq, Some(audio_count));
                    audio_count += 1;
                }
                SynthesisEvent::Finished => completed.push(seq),
                _ => panic!("unexpected event"),
            }
        }
        assert_eq!(completed, [Some(0), Some(1)]);
    }

    #[test]
    fn reject_errors_invalid_audio_and_wrong_rate() {
        for record in [
            json!({"success":false,"message":"private text"}),
            json!({"type":"error"}),
            json!({"backendResponse":{"success":false}}),
            json!({"audio_base64":"%%%"}),
            json!({"audio_base64":BASE64_STANDARD.encode([1])}),
            json!({"audio_base64":BASE64_STANDARD.encode([1,0]),"sample_rate":24000}),
            json!({"audio_base64":BASE64_STANDARD.encode([1,0]),"encoding":"mp3"}),
        ] {
            let error = parse_record(&serde_json::to_vec(&record).unwrap(), 16000).unwrap_err();
            assert!(!error.to_string().contains("private text"));
        }
        assert!(parse_record(b"not JSON", 16000).is_err());
    }

    #[test]
    fn wav_decode_and_provider_serialization() {
        let mut buffer = Cursor::new(Vec::new());
        {
            let mut writer = hound::WavWriter::new(
                &mut buffer,
                hound::WavSpec {
                    channels: 1,
                    sample_rate: 16000,
                    bits_per_sample: 16,
                    sample_format: hound::SampleFormat::Int,
                },
            )
            .unwrap();
            writer.write_sample(123i16).unwrap();
            writer.write_sample(-456i16).unwrap();
            writer.finalize().unwrap();
        }
        let encoded = BASE64_STANDARD.encode(buffer.into_inner());
        assert_eq!(
            decode_audio(&encoded, 16000).unwrap().as_ref(),
            &[123, 0, 56, 254]
        );
        assert!(decode_audio(&encoded, 24000).is_err());
        let mut trailing = BASE64_STANDARD.decode(&encoded).unwrap();
        trailing.extend([0, 0]);
        assert!(decode_audio(&BASE64_STANDARD.encode(trailing), 16000).is_err());
        let provider: SynthesisType = serde_json::from_str("\"60db\"").unwrap();
        assert_eq!(provider, SynthesisType::SixtyDB);
        assert_eq!(serde_json::to_string(&provider).unwrap(), "\"60db\"");
    }

    #[tokio::test]
    async fn engine_registration_and_validation() {
        let server = MockServer::start().await;
        let config = option(server.uri());
        let mut client = crate::media::engine::StreamEngine::default()
            .create_tts_client(false, &config)
            .await
            .unwrap();
        assert_eq!(client.provider(), SynthesisType::SixtyDB);
        let events = client.start().await.unwrap();
        let mut invalid = config.clone();
        invalid.samplerate = Some(24000);
        assert!(
            client
                .synthesize("text", Some(0), Some(invalid))
                .await
                .is_err()
        );
        drop(events);
        assert!(client.synthesize("text", Some(0), None).await.is_err());
        for rate in [0, 8000, -1] {
            let mut invalid = config.clone();
            invalid.samplerate = Some(rate);
            assert!(SixtyDBTtsClient::create(false, &invalid).is_err());
        }
    }
}

#[cfg(test)]
mod transport_tests {
    use super::*;
    use wiremock::{Mock, MockServer, ResponseTemplate, matchers::method};

    fn option(endpoint: String) -> SynthesisOption {
        SynthesisOption {
            provider: Some(SynthesisType::SixtyDB),
            secret_key: Some("local-test-token".into()),
            speaker: Some("038cf0d1-eef8-45a6-81b0-99c5e57a33d2".into()),
            endpoint: Some(endpoint),
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn http_errors_and_empty_response_never_finish_successfully() {
        for response in [
            ResponseTemplate::new(401).set_body_string("local-test-token"),
            ResponseTemplate::new(200).set_body_json(json!({"success":true})),
            ResponseTemplate::new(200)
                .set_body_raw("{\"type\":\"error\"}\n", "application/x-ndjson"),
        ] {
            let server = MockServer::start().await;
            Mock::given(method("POST"))
                .respond_with(response)
                .expect(1)
                .mount(&server)
                .await;
            let mut client = SixtyDBTtsClient::create(false, &option(server.uri())).unwrap();
            let mut events = client.start().await.unwrap();
            client.synthesize("text", Some(0), None).await.unwrap();
            client.stop().await.unwrap();
            let mut errors = 0;
            while let Some((_, event)) = events.next().await {
                let error = event.unwrap_err();
                assert!(!error.to_string().contains("local-test-token"));
                errors += 1;
            }
            assert_eq!(errors, 1);
        }
    }

    #[tokio::test]
    async fn fragmented_ndjson_and_inflight_cancellation_close_the_socket() {
        use tokio::{
            io::{AsyncReadExt, AsyncWriteExt},
            net::TcpListener,
        };
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let endpoint = format!("http://{}", listener.local_addr().unwrap());
        let server = tokio::spawn(async move {
            let (mut socket, _) = listener.accept().await.unwrap();
            let mut request = Vec::new();
            let mut buffer = [0; 1024];
            loop {
                let size = socket.read(&mut buffer).await.unwrap();
                assert!(size > 0);
                request.extend_from_slice(&buffer[..size]);
                if let Some(end) = request.windows(4).position(|w| w == b"\r\n\r\n") {
                    let headers = String::from_utf8_lossy(&request[..end]).to_ascii_lowercase();
                    let length: usize = headers
                        .lines()
                        .find_map(|line| line.strip_prefix("content-length:"))
                        .unwrap()
                        .trim()
                        .parse()
                        .unwrap();
                    if request.len() >= end + 4 + length {
                        break;
                    }
                }
            }
            socket.write_all(b"HTTP/1.1 200 OK\r\nContent-Type: application/x-ndjson\r\nContent-Length: 5000\r\n\r\n{\"result\":{\"audio").await.unwrap();
            tokio::time::sleep(Duration::from_millis(20)).await;
            socket
                .write_all(b"Content\":\"AQACAA==\"}}\n")
                .await
                .unwrap();
            let size = tokio::time::timeout(Duration::from_secs(3), socket.read(&mut buffer))
                .await
                .unwrap()
                .unwrap();
            assert_eq!(
                size, 0,
                "dropping the response must close its incomplete HTTP body"
            );
        });
        let mut client = SixtyDBTtsClient::create(true, &option(endpoint)).unwrap();
        let mut events = client.start().await.unwrap();
        client.synthesize("text", None, None).await.unwrap();
        client.stop().await.unwrap();
        let event = tokio::time::timeout(Duration::from_secs(3), events.next())
            .await
            .unwrap()
            .unwrap()
            .1
            .unwrap();
        match event {
            SynthesisEvent::AudioChunk(bytes) => assert_eq!(bytes.as_ref(), &[1, 0, 2, 0]),
            _ => panic!("expected PCM"),
        }
        drop(events);
        server.await.unwrap();
    }

    #[tokio::test]
    async fn binary_pcm_response() {
        let server = MockServer::start().await;
        Mock::given(method("POST"))
            .respond_with(ResponseTemplate::new(200).set_body_raw(vec![1, 0, 2, 0], "audio/pcm"))
            .expect(1)
            .mount(&server)
            .await;
        let mut client = SixtyDBTtsClient::create(false, &option(server.uri())).unwrap();
        let mut events = client.start().await.unwrap();
        client.synthesize("text", Some(0), None).await.unwrap();
        client.stop().await.unwrap();
        match events.next().await.unwrap().1.unwrap() {
            SynthesisEvent::AudioChunk(bytes) => assert_eq!(bytes.as_ref(), &[1, 0, 2, 0]),
            _ => panic!("expected PCM"),
        }
        assert!(matches!(
            events.next().await.unwrap().1.unwrap(),
            SynthesisEvent::Finished
        ));
        assert!(events.next().await.is_none());
    }
}