Android-cuttlefish cvd tool
audio_generator.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cstddef>
20#include <cstdint>
21#include <random>
22#include <span>
23#include <string>
24#include <vector>
25
28
29namespace cuttlefish {
30namespace virtualtuner {
31
32inline constexpr size_t kSampleRate = 48000;
33inline constexpr size_t kChannels = 2;
34inline constexpr size_t kBytesPerSample = sizeof(int16_t);
35inline constexpr size_t kFrameSizeBytes = kChannels * kBytesPerSample;
36
37inline constexpr size_t kChunkFrames = 4096;
38inline constexpr size_t kChunkSizeBytes = kChunkFrames * kFrameSizeBytes;
39
40// Largest station WAV file the daemon reads, about 5.8 minutes of 48 kHz
41// 16-bit stereo audio. The whole file is held in memory while tuned.
42inline constexpr uint64_t kMaxWavFileBytes = uint64_t{64} << 20;
43
44// Tone that identifies the tuned station, so that stations can be told apart
45// by ear and by FFT. AM: 300 Hz at 530 kHz plus 0.5 Hz per kHz (300..885 Hz).
46// FM: 1000 Hz at 87.5 MHz plus 10 Hz per 100 kHz (1000..3050 Hz); each HD
47// subchannel above HD1 adds 1 kHz (HD1 simulcasts the analog program). DAB:
48// 5000 Hz at 174 MHz plus 20 Hz per MHz (5000..6320 Hz). Returns 0 when no
49// station is broadcasting on the tuned frequency, in which case only white
50// noise is played.
51double StationToneHz(const TunerStateSnapshot& snapshot);
52
53// Reads and parses 16-bit signed little-endian PCM samples at kSampleRate Hz
54// from a mono or kChannels-channel RIFF/WAVE file on the host.
55Result<std::vector<int16_t>> ParseWavBytes(std::span<const uint8_t> wav_bytes);
56Result<void> ValidateWavFile(const std::string& wav_path);
57Result<std::vector<int16_t>> LoadWavFile(const std::string& wav_path);
58
60 public:
62
63 // Fills `samples` with silence while the tuner is stopped, white noise when
64 // no station is broadcasting on the tuned frequency, and the station audio
65 // (looping `snapshot.station_wav_path` if set, or the station tone)
66 // otherwise.
67 void GenerateChunk(std::span<int16_t> samples,
68 const TunerStateSnapshot& snapshot);
69
70 private:
71 std::minstd_rand rng_;
72 std::string active_wav_path_;
73 std::vector<int16_t> active_wav_samples_;
74 size_t wav_cursor_ = 0;
75 // Kept across chunks so the tone stays continuous.
76 double phase_ = 0.0;
77};
78
79} // namespace virtualtuner
80} // namespace cuttlefish
Definition: audio_generator.h:59
double phase_
Definition: audio_generator.h:76
AudioGenerator()
Definition: audio_generator.cpp:273
void GenerateChunk(std::span< int16_t > samples, const TunerStateSnapshot &snapshot)
Definition: audio_generator.cpp:275
size_t wav_cursor_
Definition: audio_generator.h:74
std::minstd_rand rng_
Definition: audio_generator.h:71
std::vector< int16_t > active_wav_samples_
Definition: audio_generator.h:73
std::string active_wav_path_
Definition: audio_generator.h:72
double StationToneHz(const TunerStateSnapshot &snapshot)
Definition: audio_generator.cpp:203
constexpr size_t kChunkSizeBytes
Definition: audio_generator.h:38
constexpr size_t kBytesPerSample
Definition: audio_generator.h:34
constexpr size_t kFrameSizeBytes
Definition: audio_generator.h:35
Result< std::vector< int16_t > > LoadWavFile(const std::string &wav_path)
Definition: audio_generator.cpp:265
Result< std::vector< int16_t > > ParseWavBytes(std::span< const uint8_t > wav_bytes)
Definition: audio_generator.cpp:227
constexpr size_t kChunkFrames
Definition: audio_generator.h:37
constexpr uint64_t kMaxWavFileBytes
Definition: audio_generator.h:42
constexpr size_t kChannels
Definition: audio_generator.h:33
Result< void > ValidateWavFile(const std::string &wav_path)
Definition: audio_generator.cpp:255
constexpr size_t kSampleRate
Definition: audio_generator.h:32
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30