Distribution_Service/CC_SDK/Include/Module/Multimedia/CCAudio.h

199 lines
4.9 KiB
C
Raw Normal View History

2025-11-11 17:46:19 +08:00
#ifndef CCAudio_H
#define CCAudio_H
#pragma once
#include "basic/CC.h"
#include "portaudio.h"
/**
*
*/
struct AudioDeviceInfo {
std::string name;
unsigned int index;
unsigned int Channels;
PaDeviceInfo * deviceInfo = nullptr;
};
/**
*
*/
enum AudioType
{
Capture,
Render
};
/**
*
*/
typedef PaStream AudioStream;
typedef PaStreamParameters AudioParameter;
/**
* CCMultimedia命名空间
*/
namespace CTL::Multimedia {
/**
*
* @return truefalse
*/
bool Init();
/**
*
*/
void Release();
struct StreamParameters {
PaDeviceIndex device = 0;
int channelCount = 1;
PaSampleFormat sampleFormat = paInt16;
PaTime suggestedLatency{};
void *hostApiSpecificStreamInfo = nullptr;
};
struct StreamInfo {
PaStreamCallback *streamCallback = nullptr;
double sampleRate = 48000;
unsigned long framesPerBuffer = 2048;
PaStreamFlags streamFlags = paNoFlag;
void *userData = nullptr;
};
enum Format {
F32 = paFloat32,
I16 = paInt16,
I24 = paInt24,
I32 = paInt32,
U8 = paUInt8,
I8 = paInt8,
S8_U8 = paCustomFormat,
};
/**
* CCAudio类
*/
class Audio
{
// 计算两个字符串的Levenshtein距离
static int CalculateLevenshteinDistance(const std::string& s1, const std::string& s2);
// 检查两个设备名称是否相似
static bool IsDeviceNameSimilar(const std::string& name1, const std::string& name2, int threshold = 3);
public:
Audio() = default;
/**
*
* @param type Capture或Render
*/
explicit Audio(AudioType type);
/**
*
*/
~Audio();
/**
*
* @param info
* @return truefalse
*/
bool SetStream(const StreamInfo* info);
/**
*
* @param info
* @return truefalse
*/
bool Start(const StreamInfo* info = nullptr);
/**
*
* @return truefalse
*/
bool Stop();
/**
*
* @param buffer
* @param frames
* @return truefalse
*/
bool ReadStream(void *buffer,unsigned long frames);
/**
*
* @param buffer
* @param frames
* @return truefalse
*/
bool WriteStream(void *buffer,unsigned long frames);
/**
*
* @return
*/
String GetStreamError();
/**
*
* @param Type Capture或Render
*/
void SetMode(AudioType Type);
/**
*
* @param ms 50
*/
void SetSleep(long ms = 50);
/**
*
* @param parameters
*/
void SetStreamParameters(const StreamParameters* parameters);
/**
*
* @return
*/
static unsigned int GetDeviceCount();
/**
*
* @param Type Capture或Render
* @return
*/
static int GetDefaultDevice(AudioType Type);
/**
*
* @param type Capture或Render
* @return
*/
static std::vector<AudioDeviceInfo> GetDevices(AudioType type);
/**
*
* @param type Capture或Render
* @return
*/
static std::vector<AudioDeviceInfo> GetDeviceAll(AudioType type);
/**
*
* @param info
* @return StreamParameters
*/
static StreamParameters GetParameters(const AudioDeviceInfo& info);
private:
PaStream *Stream{}; // 录音流和播放流
PaStreamParameters Parameters{};
AudioType Type = Render;
bool BassStart = false;
PaError error_{};
};
}
#endif