Skip to content

Commit e01c44a

Browse files
committed
feat: concurrency
1 parent 4415faa commit e01c44a

2 files changed

Lines changed: 34 additions & 16 deletions

File tree

Controllers/AdminController.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,41 @@ await _context.Tracks
117117
sb.Append("except: ").Append(string.Join(" ", listFail));
118118
} else if (arg == "rebuild_loudness") {
119119
result = "ok";
120-
var listFail = new List<int>();
121-
var block = new ActionBlock<Track>(async (t) => {
120+
var block = new TransformBlock<Track, (int, TrackAudioInfo)>(async (t) => {
122121
try {
123122
var info = await _context.App.ConvertService.ComputeLoudnessMap(t);
124-
var oldInfo = await _context.TrackAudioInfos.AsNoTracking().Where(x => x.Id == t.id).FirstOrDefaultAsync();
125-
if (oldInfo == null) {
126-
_context.TrackAudioInfos.Add(info);
127-
} else {
128-
_context.TrackAudioInfos.Update(info);
129-
}
130-
await _context.SaveChangesAsync();
131-
} catch (Exception) {
132-
listFail.Add(t.id);
123+
return (t.id, info);
124+
} catch (Exception e) {
125+
Console.WriteLine(e);
126+
return (t.id, null);
133127
}
134128
}, new ExecutionDataflowBlockOptions{ MaxDegreeOfParallelism = Environment.ProcessorCount / 2 });
135129
await _context.Tracks
136130
.Include(t => t.fileRecord)
137131
.ForEachAsync((t) => {
138132
block.Post(t);
139133
});
134+
var listFail = new List<int>();
135+
await foreach (var (id, info) in block.ReceiveAllAsync()) {
136+
if (info == null) {
137+
listFail.Add(id);
138+
continue;
139+
}
140+
try {
141+
var oldInfo = await _context.TrackAudioInfos.AsNoTracking().Where(x => x.Id == id).FirstOrDefaultAsync();
142+
if (oldInfo == null) {
143+
_context.TrackAudioInfos.Add(info);
144+
} else {
145+
_context.TrackAudioInfos.Update(info);
146+
}
147+
await _context.SaveChangesAsync();
148+
} catch (Exception e) {
149+
Console.WriteLine(e);
150+
listFail.Add(id);
151+
}
152+
}
140153
block.Complete();
141-
await block.Completion;
142-
sb.Append("failed id list: ").Append(string.Join(" ", listFail));
154+
await _context.SaveChangesAsync();
143155
}
144156
} catch (Exception ex) {
145157
result = "error";

Services/ConvertService.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,15 @@ public async Task<TrackAudioInfo> ComputeLoudnessMap(Track track) {
257257
} while (haveRead < 4 * 1024);
258258
if (haveRead == 0) break;
259259
double sum = 0;
260-
for (var i = 0; i < haveRead; i++) {
261-
var x = (sbyte)buffer[i];
262-
sum += x * x;
260+
for (var i = 0; i + 3 < haveRead; i += 4) {
261+
var x0 = (sbyte)buffer[i + 0];
262+
var x1 = (sbyte)buffer[i + 1];
263+
var x2 = (sbyte)buffer[i + 2];
264+
var x3 = (sbyte)buffer[i + 3];
265+
sum += x0 * x0;
266+
sum += x1 * x1;
267+
sum += x2 * x2;
268+
sum += x3 * x3;
263269
}
264270
var rms = Math.Sqrt(sum / haveRead);
265271
ms.WriteByte((byte)rms);

0 commit comments

Comments
 (0)