Skip to content

Commit 3c32b67

Browse files
committed
stupid ass fixes and resampling
1 parent bdac335 commit 3c32b67

3 files changed

Lines changed: 100 additions & 2 deletions

File tree

ImMilo/Program.Prompts.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,41 @@ public override void Show()
330330
}
331331
}
332332
}
333+
334+
private class IntPrompt : Prompt<int?>
335+
{
336+
private int value;
337+
338+
public IntPrompt(string message, string title, int defaultValue = 0)
339+
{
340+
Message = message;
341+
Title = title;
342+
value = defaultValue;
343+
}
344+
345+
public override void Show()
346+
{
347+
if (BeginModal())
348+
{
349+
if (ImGui.IsWindowAppearing())
350+
{
351+
ImGui.SetKeyboardFocusHere();
352+
}
353+
ImGui.InputInt(Message, ref value);
354+
355+
if (ImGui.Button("Ok"))
356+
{
357+
Complete(value);
358+
}
359+
ImGui.SameLine();
360+
if (ImGui.Button("Cancel"))
361+
{
362+
Complete(null);
363+
}
364+
ImGui.EndPopup();
365+
}
366+
}
367+
}
333368

334369
/// <summary>
335370
/// Shows a <see cref="Prompt{T}"/>, and waits for it to be completed by the user.
@@ -396,6 +431,18 @@ public static async Task<string> ShowChoosePrompt(string message, string title,
396431
{
397432
return await ShowGenericPrompt(new TextPrompt(inputLabel, title, defaultValue));
398433
}
434+
435+
/// <summary>
436+
/// Shows a number prompt, asking the user for a integer.
437+
/// </summary>
438+
/// <param name="inputLabel">Label shown next to the text box.</param>
439+
/// <param name="title">Window title.</param>
440+
/// <param name="defaultValue">Value first entered in the text box.</param>
441+
/// <returns></returns>
442+
public static async Task<int?> ShowIntPrompt(string inputLabel, string title, int defaultValue = 0)
443+
{
444+
return await ShowGenericPrompt(new IntPrompt(inputLabel, title, defaultValue));
445+
}
399446

400447
/// <summary>
401448
/// Shows a save prompt, allowing the user to modify <see cref="SaveSettings"/>.

ImMilo/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ private set
8686

8787
private static SearchWindow geomOwnerFinder = new("Geometry Owner Finder");
8888

89+
/// <summary>
90+
/// Doesn't actually detect FFMPEG yet, temporarily set to true.
91+
/// </summary>
8992
public static bool FFMpegFound = true;
9093

9194
static void Main(string[] args)

ImMilo/SamplePlayer.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,38 @@ public static void Draw(Sfx sfx)
210210
player.Render();
211211
}
212212

213+
public async void PromptResample()
214+
{
215+
var rate = await Program.ShowIntPrompt("New sample rate", "Resample Audio", (int)thisSample.sampleData.sampleRate);
216+
if (rate != null)
217+
{
218+
var inData = new List<byte>();
219+
for (int i = 0; i < sampleData.Length; i++)
220+
{
221+
var bytes = BitConverter.GetBytes(sampleData[i]);
222+
if (BitConverter.IsLittleEndian)
223+
{
224+
bytes = bytes.Reverse().ToArray();
225+
}
226+
inData.AddRange(bytes);
227+
}
228+
var inStream = new MemoryStream(inData.ToArray());
229+
var outStream = new MemoryStream();
230+
FFMpegArguments.FromPipeInput(new StreamPipeSource(inStream), options => options
231+
.WithAudioCodec("pcm_s16be").ForceFormat("s16be").WithAudioSamplingRate((int)thisSample.sampleData.sampleRate))
232+
.OutputToPipe(new StreamPipeSink(outStream), options => options
233+
.WithAudioCodec("pcm_s16be").ForceFormat("s16be").WithAudioSamplingRate(rate.Value).WithCustomArgument("-ac 1")).WithLogLevel(FFMpegLogLevel.Info).ProcessSynchronously();
234+
235+
236+
var data = outStream.ToArray();
237+
thisSample.sampleData.samples = [..data];
238+
thisSample.sampleData.sampleRate = (uint)rate;
239+
thisSample.sampleData.sampleCount = (uint)data.Length / 2;
240+
thisSample.sampleData.encoding = SynthSample.SampleData.Encoding.kBigEndPCM;
241+
LoadData();
242+
}
243+
}
244+
213245
public void Render()
214246
{
215247
ImGui.Button(FontAwesome5.EllipsisH, new Vector2(SamplePlayerHeight, SamplePlayerHeight));
@@ -247,17 +279,33 @@ public void Render()
247279
}
248280
}
249281

250-
if (ImGui.MenuItem(FontAwesome5.FileExport + " Export Audio File"))
282+
if (dataState == State.Loaded && ImGui.MenuItem(FontAwesome5.FileExport + " Export Audio File"))
251283
{
252284
var (cancelled, path) =
253285
TinyDialogs.SaveFileDialog("Select file to save", "", new FileFilter("WAV file", ["*.wav"]));
254286
if (!cancelled)
255287
{
256-
var memoryStream = new MemoryStream(thisSample.sampleData.samples.ToArray());
288+
var data = new List<byte>();
289+
for (int i = 0; i < sampleData.Length; i++)
290+
{
291+
var bytes = BitConverter.GetBytes(sampleData[i]);
292+
if (BitConverter.IsLittleEndian)
293+
{
294+
bytes = bytes.Reverse().ToArray();
295+
}
296+
data.AddRange(bytes);
297+
}
298+
var memoryStream = new MemoryStream(data.ToArray());
299+
257300
FFMpegArguments.FromPipeInput(new StreamPipeSource(memoryStream), options => options
258301
.WithAudioCodec("pcm_s16be").ForceFormat("s16be").WithAudioSamplingRate((int)thisSample.sampleData.sampleRate)).OutputToFile(path).ProcessSynchronously();
259302
}
260303
}
304+
305+
if (dataState == State.Loaded && ImGui.MenuItem(FontAwesome5.FileAudio + " Resample Audio"))
306+
{
307+
PromptResample();
308+
}
261309
}
262310
else
263311
{

0 commit comments

Comments
 (0)