Skip to content

Commit 99c74e0

Browse files
authored
Merge branch 'stakira:master' into JaPresampNullPriorityReplaceFix
2 parents 0b1735f + 6958d68 commit 99c74e0

File tree

7 files changed

+117
-26
lines changed

7 files changed

+117
-26
lines changed

OpenUtau.Core/Audio/MiniAudioOutput.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NAudio.Wave;
55
using NAudio.Wave.SampleProviders;
66
using OpenUtau.Core.Util;
7+
using Serilog;
78

89
namespace OpenUtau.Audio {
910
public class MiniAudioOutput : IAudioOutput, IDisposable {
@@ -40,28 +41,29 @@ public MiniAudioOutput() {
4041
private void UpdateDeviceList() {
4142
devices.Clear();
4243
unsafe {
43-
ou_audio_device_info_t* device_infos = stackalloc ou_audio_device_info_t[16];
44-
int count = ou_get_audio_device_infos(device_infos, 16);
44+
const int kMaxCount = 128;
45+
ou_audio_device_info_t* device_infos = stackalloc ou_audio_device_info_t[kMaxCount];
46+
int count = ou_get_audio_device_infos(device_infos, kMaxCount);
4547
if (count == 0) {
4648
throw new Exception("Failed to get any audio device info");
4749
}
48-
if (count > 16) {
49-
ou_free_audio_device_infos(device_infos, 16);
50-
count = ou_get_audio_device_infos(device_infos, count);
50+
if (count > kMaxCount) {
51+
Log.Warning($"More than {kMaxCount} audio devices found, only the first {kMaxCount} will be listed.");
52+
count = kMaxCount;
5153
}
5254
for (int i = 0; i < count; i++) {
5355
var guidData = new byte[16];
5456
fixed (byte* guidPtr = guidData) {
5557
*(ulong*)guidPtr = device_infos[i].api_id;
5658
*(ulong*)(guidPtr + 8) = device_infos[i].id;
5759
}
60+
string api = Marshal.PtrToStringUTF8(device_infos[i].api); // Should be ascii.
61+
string name = (OS.IsWindows() && api != "WASAPI")
62+
? Marshal.PtrToStringAnsi(device_infos[i].name)
63+
: Marshal.PtrToStringUTF8(device_infos[i].name);
5864
devices.Add(new AudioOutputDevice {
59-
name = OS.IsWindows()
60-
? Marshal.PtrToStringAnsi(device_infos[i].name)
61-
: Marshal.PtrToStringUTF8(device_infos[i].name),
62-
api = OS.IsWindows()
63-
? Marshal.PtrToStringAnsi(device_infos[i].api)
64-
: Marshal.PtrToStringUTF8(device_infos[i].api),
65+
name = name,
66+
api = api,
6567
deviceNumber = i,
6668
guid = new Guid(guidData),
6769
});
@@ -111,15 +113,15 @@ private unsafe void DataCallback(float* buffer, uint channels, uint frame_count)
111113
}
112114
int n = 0;
113115
if (sampleProvider != null) {
114-
n = sampleProvider.Read(temp, 0, temp.Length);
116+
n = sampleProvider.Read(temp, 0, samples);
115117
}
116118
if (n < samples) {
117119
Array.Fill(temp, 0, n, samples - n);
118120
}
119121
if (n == 0) {
120122
eof = true;
121123
}
122-
Marshal.Copy(temp, 0, (IntPtr)buffer, temp.Length);
124+
Marshal.Copy(temp, 0, (IntPtr)buffer, samples);
123125
currentTimeMs += n / channels * 1000.0 / sampleRate;
124126
}
125127

cpp/worldline/BUILD.bazel

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,28 @@ package(
22
default_visibility = ["//visibility:public"],
33
)
44

5+
cc_binary(
6+
name = "audio_debug",
7+
srcs = ["audio_debug.cc"],
8+
deps = [
9+
":audio_output_lib",
10+
"@absl//absl/debugging:failure_signal_handler",
11+
"@absl//absl/debugging:symbolize",
12+
"@absl//absl/flags:flag",
13+
"@absl//absl/flags:parse",
14+
"@absl//absl/log",
15+
"@absl//absl/log:check",
16+
"@absl//absl/log:initialize",
17+
"@absl//absl/strings",
18+
"@absl//absl/strings:string_view",
19+
"@miniaudio",
20+
"@xxhash",
21+
],
22+
)
23+
524
cc_library(
625
name = "audio_output_lib",
7-
srcs = ["audio_output.c"],
26+
srcs = ["audio_output.cc"],
827
hdrs = ["audio_output.h"],
928
deps = [
1029
"@miniaudio",

cpp/worldline/audio_debug.cc

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <cstdint>
2+
3+
#include "absl/debugging/failure_signal_handler.h"
4+
#include "absl/debugging/symbolize.h"
5+
#include "absl/flags/flag.h"
6+
#include "absl/flags/parse.h"
7+
#include "absl/log/globals.h"
8+
#include "absl/log/initialize.h"
9+
#include "absl/log/log.h"
10+
#include "absl/strings/escaping.h"
11+
#include "absl/strings/str_cat.h"
12+
#include "absl/strings/string_view.h"
13+
#include "miniaudio.h"
14+
#include "worldline/audio_output.h"
15+
#include "xxhash.h"
16+
17+
int main(int argc, char** argv) {
18+
absl::InitializeSymbolizer(argv[0]);
19+
absl::FailureSignalHandlerOptions options;
20+
absl::InstallFailureSignalHandler(options);
21+
22+
absl::InitializeLog();
23+
absl::SetStderrThreshold(absl::LogSeverity::kInfo);
24+
25+
absl::ParseCommandLine(argc, argv);
26+
27+
for (int i = 0; i < ma_backend_coreaudio; i++) {
28+
LOG(INFO) << "============";
29+
LOG(INFO) << "Trying backend: " << ma_get_backend_name((ma_backend)i);
30+
31+
ma_context context;
32+
ma_backend backends[1] = {(ma_backend)i};
33+
ma_result result = ma_context_init(backends, 1, NULL, &context);
34+
35+
if (result != MA_SUCCESS) {
36+
LOG(ERROR) << "Failed to initialize context";
37+
LOG(ERROR) << "Error: " << ma_result_description(result);
38+
continue;
39+
}
40+
41+
ma_device_info* playback_device_infos;
42+
ma_uint32 playback_device_count;
43+
ma_device_info* capture_device_infos;
44+
ma_uint32 capture_device_count;
45+
46+
result = ma_context_get_devices(
47+
&context, &playback_device_infos, &playback_device_count,
48+
&capture_device_infos, &capture_device_count);
49+
50+
if (result != MA_SUCCESS) {
51+
LOG(ERROR) << "Failed to get devices";
52+
LOG(ERROR) << "Error: " << ma_result_description(result);
53+
ma_context_uninit(&context);
54+
continue;
55+
}
56+
LOG(INFO) << "Playback device count: " << playback_device_count;
57+
58+
for (int j = 0; j < playback_device_count; j++) {
59+
LOG(INFO) << "------------";
60+
LOG(INFO) << "Device: #" << j;
61+
ma_device_info* info = &playback_device_infos[j];
62+
LOG(INFO) << "Device name: " << info->name;
63+
LOG(INFO) << "Device name bytes: "
64+
<< absl::BytesToHexString(std::string_view(info->name));
65+
uint64_t id = XXH64(&(info->id), sizeof(ma_device_id), 0);
66+
LOG(INFO) << "Device ID: " << absl::Hex(id);
67+
}
68+
}
69+
70+
LOG(INFO) << "============";
71+
LOG(INFO) << "Done";
72+
73+
return 0;
74+
}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ DLL_API int32_t ou_get_audio_device_infos(ou_audio_device_info_t* device_infos,
1515

1616
for (int i = 0; i < ma_backend_null; i++) {
1717
backends[0] = (ma_backend)i;
18-
if (i == 0 || i == (int)ma_backend_dsound) {
19-
backends[0] =
20-
(ma_backend)((int)ma_backend_dsound - i); // Swaps dsound to first.
21-
}
2218
ma_result result = ma_context_init(backends, 1, NULL, &context);
2319
if (result != MA_SUCCESS) {
2420
continue;
@@ -74,22 +70,22 @@ static void silence(float* buffer, uint32_t channels, uint32_t frame_count) {
7470
static void data_callback(ma_device* pDevice, void* pOutput, const void* pInput,
7571
ma_uint32 frameCount) {
7672
if (g_data_callback == NULL) {
77-
silence(pOutput, pDevice->playback.channels, frameCount);
73+
silence((float*)pOutput, pDevice->playback.channels, frameCount);
7874
} else {
79-
g_data_callback(pOutput, pDevice->playback.channels, frameCount);
75+
g_data_callback((float*)pOutput, pDevice->playback.channels, frameCount);
8076
}
8177
}
8278

8379
DLL_API ou_audio_context_t* ou_init_audio_device(
8480
uint32_t api_id, uint64_t id, ou_audio_data_callback_t callback) {
85-
ou_audio_context_t* result = malloc(sizeof(ou_audio_context_t));
81+
ou_audio_context_t* result = new ou_audio_context_t();
8682
if (result == NULL) {
8783
return NULL;
8884
}
8985

9086
ma_backend backends[1] = {(ma_backend)api_id};
9187
if (ma_context_init(backends, 1, NULL, &result->context) != MA_SUCCESS) {
92-
free(result);
88+
delete result;
9389
return NULL;
9490
}
9591

@@ -101,7 +97,7 @@ DLL_API ou_audio_context_t* ou_init_audio_device(
10197
&playback_device_count, &capture_device_infos,
10298
&capture_device_count) != MA_SUCCESS) {
10399
ma_context_uninit(&result->context);
104-
free(result);
100+
delete result;
105101
return NULL;
106102
}
107103

@@ -123,14 +119,14 @@ DLL_API ou_audio_context_t* ou_init_audio_device(
123119

124120
if (config.playback.pDeviceID == NULL) {
125121
ma_context_uninit(&result->context);
126-
free(result);
122+
delete result;
127123
return NULL;
128124
}
129125

130126
if (ma_device_init(&result->context, &config, &result->device) !=
131127
MA_SUCCESS) {
132128
ma_context_uninit(&result->context);
133-
free(result);
129+
delete result;
134130
return NULL;
135131
}
136132

@@ -143,7 +139,7 @@ DLL_API int ou_free_audio_device(ou_audio_context_t* context) {
143139
if (result != MA_SUCCESS) {
144140
return result;
145141
}
146-
free(context);
142+
delete context;
147143
return 0;
148144
}
149145

2.5 KB
Binary file not shown.
3.5 KB
Binary file not shown.
512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)