Skip to content

Commit

Permalink
Merge pull request #2 from koen1711/select-recorder
Browse files Browse the repository at this point in the history
feat: add system to select the input of the program
  • Loading branch information
koen1711 authored Jul 12, 2024
2 parents 8a1deb3 + b06a171 commit cd04859
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 12 deletions.
2 changes: 1 addition & 1 deletion libs/json
Submodule json updated from 8c391e to f50897
4 changes: 2 additions & 2 deletions misc/install-systemd-service.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Type=simple
Restart=always
RestartSec=1
User=$USER
ExecStart=$CURRENT_DIR/Recorder
ExecStart=sh 'cd $CURRENT_DIR && ./Recorder'
[Install]
WantedBy=multi-user.target" > /etc/systemd/system/$SERVICE_NAME.service

Expand All @@ -49,4 +49,4 @@ systemctl start $SERVICE_NAME

# check the status of the service
systemctl status $SERVICE_NAME
```

78 changes: 73 additions & 5 deletions src/recording/audio/audioRecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uin
(void)pOutput;
}


ma_context context;

int AudioRecorder::init() {
if (channels.empty()) {
Expand All @@ -153,13 +153,12 @@ int AudioRecorder::init() {
}
}

//encoders.encoders = (ma_encoder*)malloc(channelCount * sizeof(ma_encoder));
encoders.encoders.resize(channelCount);
for (int i = 0; i < channelCount; ++i) {
encoders.encoders[i] = {};
}

encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, outputFormat, 1, 48000); // Ensure the sample rate is 96000
encoderConfig = ma_encoder_config_init(ma_encoding_format_wav, outputFormat, 1, 48000);

ma_channel* channels = (ma_channel*)malloc(channelCount * sizeof(ma_channel));
for (int i = 0; i < channelCount; ++i) {
Expand All @@ -170,20 +169,53 @@ int AudioRecorder::init() {
deviceConfig.periodSizeInMilliseconds = 1;
deviceConfig.capture.format = outputFormat;
deviceConfig.capture.channels = channelCount;
deviceConfig.sampleRate = encoderConfig.sampleRate; // Ensure the sample rate matches the encoder
deviceConfig.sampleRate = encoderConfig.sampleRate;
deviceConfig.dataCallback = data_callback;
deviceConfig.capture.pChannelMap = channels;
deviceConfig.pUserData = &encoders;

result = ma_device_init(NULL, &deviceConfig, &device);
// Enumerate devices
ma_device_info* pCaptureInfos;
ma_uint32 captureCount;


result = ma_context_get_devices(&context, NULL, NULL, &pCaptureInfos, &captureCount);
if (result != MA_SUCCESS) {
printf("Failed to enumerate devices.\n");
ma_context_uninit(&context);
return -4;
}

// Select a specific device (e.g., by name)
int selectedDeviceIndex = -1;

for (ma_uint32 i = 0; i < captureCount; ++i) {
if (strcmp(pCaptureInfos[i].name, recorderDeviceName.c_str()) == 0) {
selectedDeviceIndex = i;
break;
}
}

if (selectedDeviceIndex == -1) {
printf("Desired device not found.\n");
ma_context_uninit(&context);
return -5;
}

deviceConfig.capture.pDeviceID = &pCaptureInfos[selectedDeviceIndex].id;

result = ma_device_init(&context, &deviceConfig, &device);
if (result != MA_SUCCESS) {
printf("Failed to initialize capture device.\n");
ma_context_uninit(&context);
return -1;
}


result = ma_device_start(&device);
if (result != MA_SUCCESS) {
ma_device_uninit(&device);
ma_context_uninit(&context);
printf("Failed to start device.\n");
return -2;
}
Expand Down Expand Up @@ -264,6 +296,10 @@ nlohmann::json AudioRecorder::queryConfiguration() {
"title": "Recording Settings",
"type": "body",
"questions": {
"recorderDeviceName": {
"title": "Recording Device Name",
"type": "select"
},
"audioFormat": {
"type": "select",
"title": "The format to save the audio recordings in",
Expand Down Expand Up @@ -307,6 +343,22 @@ nlohmann::json AudioRecorder::queryConfiguration() {
j["section"]["questions"]["recording"]["questions"]["recordingPathPrefix"]["value"] = recordingPathPrefix;
j["section"]["questions"]["recording"]["questions"]["channelCount"]["value"] = channelCount;

// poplate recording device name
ma_device_info* pCaptureInfos;
ma_uint32 captureCount;
result = ma_context_get_devices(&context, NULL, NULL, &pCaptureInfos, &captureCount);
if (result != MA_SUCCESS) {
printf("Failed to enumerate devices.\n");
ma_context_uninit(&context);
return j;
}
j["section"]["questions"]["recording"]["questions"]["recorderDeviceName"]["options"] = {};
for (ma_uint32 i = 0; i < captureCount; ++i) {
j["section"]["questions"]["recording"]["questions"]["recorderDeviceName"]["options"][i] = pCaptureInfos[i].name;
}
j["section"]["questions"]["recording"]["questions"]["recorderDeviceName"]["value"] = recorderDeviceName;


for (auto format : supportedFormats) {
j["section"]["questions"]["recording"]["questions"]["audioFormat"]["options"][format] = ma_get_format_name(format);
}
Expand Down Expand Up @@ -341,6 +393,10 @@ int AudioRecorder::configure(nlohmann::json config) {
break;
}
}
} else if (config.contains("recorderDeviceName")) {
recorderDeviceName = config["recorderDeviceName"];
this->deinit();
this->init();
} else if (config.contains("filePrefix")) {
filePrefix = config["filePrefix"];
} else if (config.contains("recordingPathPrefix")) {
Expand Down Expand Up @@ -507,4 +563,16 @@ AudioRecorder::AudioRecorder(uWS::SSLApp *app, uWS::Loop *loop) : BaseRecorder(a
recorder = this;
pGlobalApp = app;
pGlobalLoop = loop;

result = ma_context_init(NULL, 0, NULL, &context);
if (result != MA_SUCCESS) {
printf("Failed to initialize context.\n");
}
if (recorderDeviceName == "") {
ma_device_info* pCaptureInfos;
ma_uint32 captureCount;

result = ma_context_get_devices(&context, NULL, NULL, &pCaptureInfos, &captureCount);
recorderDeviceName = pCaptureInfos[0].name;
}
}
1 change: 1 addition & 0 deletions src/recording/audio/audioRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AudioRecorder: public BaseRecorder {
EncoderArray encoders{};
ma_device_config deviceConfig{};
ma_device device{};
std::string recorderDeviceName;
public:
explicit AudioRecorder(uWS::SSLApp *app, uWS::Loop *loop);

Expand Down
2 changes: 1 addition & 1 deletion ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Audio Recorder</title>
<title>Recorder</title>
</head>
<body>
<div id="root"></div>
Expand Down
1 change: 0 additions & 1 deletion ui/public/vite.svg

This file was deleted.

2 changes: 1 addition & 1 deletion ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Navbar } from "./components/Navbar/Navbar.tsx";
import {DeviceInfo} from "./components/device-info/DeviceInfo.tsx";
import {AccordionList} from "@tremor/react";
import React from "react";
import {RecorderContext, RecorderProvider} from "./providers/RecorderProvider.tsx";
import {RecorderContext} from "./providers/RecorderProvider.tsx";
import {AudioInfo} from "./components/audio-info/AudioInfo.tsx";
import {AudioStream} from "./components/audio-stream/AudioStream.tsx";
import {Route, BrowserRouter as Router, Routes} from "react-router-dom";
Expand Down
1 change: 0 additions & 1 deletion ui/src/assets/react.svg

This file was deleted.

2 changes: 2 additions & 0 deletions ui/src/components/Navbar/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const BodySection = memo(({ section, renderConfig, stack, configMistake }) => (

const SelectInput = memo(({ section, stack, updateConfig, configMistake }) => {
const [value, setValue] = useState(section.value);
console.log(section)

const handleChange = (e) => {
setValue(e);
Expand Down Expand Up @@ -131,6 +132,7 @@ const StringInputField = memo(({ section, stack, updateConfig, configMistake })
);
});

// @ts-ignore
const RenderConfig = ({ section, stack, updateConfig, configMistake }) => {
const renderConfig = useCallback((section, stack, stackKey, configMistake) => {
const newStack = [...stack, stackKey];
Expand Down

0 comments on commit cd04859

Please sign in to comment.