Skip to content

Commit

Permalink
fix filelock
Browse files Browse the repository at this point in the history
  • Loading branch information
KedamaOvO committed Mar 22, 2019
1 parent d96fe8e commit f379aab
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 11 deletions.
1 change: 1 addition & 0 deletions OsuDataDistributeRestful.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
11 changes: 8 additions & 3 deletions Server/Api/OrtdpApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public ActionResult GetAudioFile()

if (File.Exists(filename))
{
var fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
var fs = OpenFile(filename);

return new ActionResult(fs)
{
Expand All @@ -99,7 +99,7 @@ public ActionResult GetBackgroundFile()

if (File.Exists(filename))
{
var fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
var fs = OpenFile(filename);
string ext = Path.GetExtension(filename);

return new ActionResult(fs)
Expand All @@ -120,7 +120,7 @@ public ActionResult GetVideoFile()

if (File.Exists(filename))
{
var fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
var fs = OpenFile(filename);
string ext = Path.GetExtension(filename);

return new ActionResult(fs)
Expand Down Expand Up @@ -180,6 +180,11 @@ public object GetPlayingTime()

#region tools

private Stream OpenFile(string filename)
{
return new MemoryStream(File.ReadAllBytes(filename));
}

private object MakeProvideDatas(ProvideDataMask mask, Func<ProvideData, object> selector)
{
bool isTourney = ortdp.TourneyListenerManagersCount != 0;
Expand Down
2 changes: 1 addition & 1 deletion Server/Api/RtppdApis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public object GetPPFormat()

[Route("/hitCountFormat")]
public object GetHitCountFormat()
=> new {format = StringFormatter.GetHitCountFormatter().Format};
=> new {format = StringFormatter.GetHitCountFormatter().Format };

[Route("/formated/pp")]
public object GetFormatedPP()
Expand Down
11 changes: 11 additions & 0 deletions app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
198 changes: 198 additions & 0 deletions html/visualizer-wave.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Visualizer</title>
<style>
#thefile {
position: fixed;
top: 10px;
left: 10px;
z-index: 100;
}

#canvas {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
}

audio {
position: fixed;
left: 10px;
bottom: 10px;
width: calc(100% - 20px);
}
</style>

<script src="js/ODDR.js"></script>
<script src="js/ORTDP.js"></script>
<script>
(async function () {
const MAX_HEIGTH = window.innerHeight - 20;
let context = new AudioContext();
let play = false;

let _url = new URL(window.location.href);
let has_sound = _url.searchParams.get('sound') || "false";
let host = _url.searchParams.get('host') || "localhost";
let port = _url.searchParams.get('port') || "10800";
let lineWidth = Number.parseInt(_url.searchParams.get('lineWidth') || "5");

let current_audio_buffer = null;
let src = context.createBufferSource();
let analyser = context.createAnalyser();
analyser.smoothingTimeConstant = 0.75;
src.connect(analyser);

let oddr = new ODDR(host,port);
let ortdp = new ORTDP(oddr);

//链接音频输出设备
if (has_sound === "true")
analyser.connect(context.destination);

let last_mods = { timeRate: 1.0 };
let last_mp3 = "";
let audio_url = null;
let timer = 0;

//play and seek andio
function restart_audio(buf, time, timeRate) {
if (buf === null) return;
if (time < 0) return;
src.disconnect(analyser);

src = context.createBufferSource();
src.buffer = buf;
src.playbackRate.value = timeRate;
src.connect(analyser);

src.start(0, time);
timer = time;
play = true;
}

//play timer
setInterval(function () {
if (play === true)
timer += 0.026 * last_mods.timeRate;
}, Math.round(26 * last_mods.timeRate));

//http get loop
async function get_loop() {
let time = (await ortdp.getPlayingTime()).time / 1000.0;
let beatmap = await ortdp.getBeatmapInfo();
let mods = await ortdp.getPlayingModsAt(0);

let folder = beatmap.folder;
let mp3 = beatmap.audioFilename;
mp3 = folder + '\\' + mp3;

if (folder !== "" && mp3 !== "") {
if (last_mp3 != mp3) {
if (play === true) {
src.stop(0);
play = false;
}

let audio_data = await ortdp.getBeatmapAudio("arraybuffer");
let buf = await context.decodeAudioData(audio_data);
current_audio_buffer = buf;
restart_audio(buf, time, 1.0);
}
}
if (Math.abs(timer - time) > 0.2)
restart_audio(current_audio_buffer, time+0.1, mods.timeRate);

if (last_mods.timeRate != mods.timeRate)
restart_audio(current_audio_buffer, time, mods.timeRate);

last_mp3 = mp3;
last_mods = mods;
setTimeout(get_loop, 100);
}

//canvas initialize
async function play_audio() {
var canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext("2d");

analyser.fftSize = 16384;
const SKIP = 64;
var bufferLength = analyser.fftSize;
console.log(bufferLength);


var dataArray = new Uint8Array(bufferLength);

var WIDTH = canvas.width;
var HEIGHT = canvas.height;
var STEP = WIDTH / bufferLength;

var barHeight;

//render
function renderFrame() {
let x = 0;

analyser.getByteTimeDomainData(dataArray);

ctx.fillStyle = "#000";
ctx.fillRect(0, 0, WIDTH, HEIGHT);
ctx.lineWidth = lineWidth;

ctx.strokeStyle = "#33aacc";
ctx.beginPath();
ctx.moveTo(0,dataArray[0] / 255.0 * MAX_HEIGTH);
x += STEP;

for (let i = 1; i < bufferLength; i+=SKIP) {
let val1 = 0;
let val2 = 0;

for(let j=0;j<SKIP/2;j++){
val1 += dataArray[i+j] / 255.0;
}
for(let j=0;j<SKIP/2;j++){
val2 += dataArray[i + SKIP/2 +j] / 255.0;
}
val1 /= (SKIP/2);
val2 /= (SKIP/2);

let r = 128 + Math.round((128 * (i / bufferLength)));
let g = Math.round(val1 * val2 * 230 - (25 * (i / bufferLength)));
let b = 203;

ctx.lineTo(x + STEP, val2 * MAX_HEIGTH);

x += STEP * SKIP;
}
ctx.stroke();

requestAnimationFrame(renderFrame);
}
renderFrame();
};

window.onload = function () {
get_loop();
play_audio();
}
})();
</script>
</head>

<body>
<div id="content">
<canvas id="canvas"></canvas>
</div>
</body>
</html>

19 changes: 12 additions & 7 deletions html/visualizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@
restart_audio(buf, time, 1.0);
}
}
if (Math.abs(timer - time) > 0.5)
restart_audio(current_audio_buffer, time, mods.timeRate);
if (Math.abs(timer - time) > 0.2)
restart_audio(current_audio_buffer, time+0.1, mods.timeRate);

if (last_mods.timeRate != mods.timeRate)
restart_audio(current_audio_buffer, time, mods.timeRate);
Expand All @@ -123,7 +123,7 @@
canvas.height = window.innerHeight;
var ctx = canvas.getContext("2d");

analyser.fftSize = 512;
analyser.fftSize = 2048;

var bufferLength = analyser.frequencyBinCount;
console.log(bufferLength);
Expand All @@ -133,7 +133,7 @@
var WIDTH = canvas.width;
var HEIGHT = canvas.height;

var barWidth = (WIDTH / bufferLength) * 2.5;
var barWidth = (WIDTH / bufferLength) * 2.5 * 4;
var barHeight;
var x = 0;

Expand All @@ -146,8 +146,13 @@
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, WIDTH, HEIGHT);

for (let i = 0; i < bufferLength; i++) {
let val = dataArray[i] / 255.0;
for (let i = 0; i < bufferLength; i+=4) {
let val = 0

for(let j=0;j<4;j++){
val += dataArray[i+j] / 255.0;
}
val /= 4;
barHeight = val * val * val;

let r = 128 + Math.round((128 * (i / bufferLength)));
Expand All @@ -161,7 +166,7 @@

ctx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);

x += barWidth + 1;
x += barWidth;
}

requestAnimationFrame(renderFrame);
Expand Down

0 comments on commit f379aab

Please sign in to comment.