const int HEADERS_TO_VERIFY = 5; var headers = new List(HEADERS_TO_VERIFY); MP3Header header = null; //MP3Header is a class for the headers' properties. while (headers.Count < HEADERS_TO_VERIFY) { header = ParseHeader(); if (header == null) { headers.Clear(); Source.Position = ++pos; continue; } header.Position = Source.Position - 4; //Source is the MP3 bitstream. headers.Add(header); Debug.WriteLine($"Found MPEG {header.Version} layer {header.Layer} at {Source.Position}."); Source.Position += header.SizeData - 4; } private MP3Header ParseHeader() { //A frame starts with 11 set bits. The 12th bit should also always be set. if (Source.Read(12).Any(bit => !bit)) return null; //Read 12 bits from the bitstream. //Parse the four bytes into a frame header. var header = new MP3Header(); header.Version = Source.Read() ? 1 : 2; //Read 1 bit from the bitstream. header.Layer = 4 - Source.Read(2).ToInt(); header.IsCRCProtected = Source.Read(); if (!header.Layer.Between(1, 3)) return null; header.SetKilobitsSecond(Source.Read(4).ToInt()); header.SetSamplesSecond(Source.Read(2).ToInt()); header.IsPadded = Source.Read(); Source.Read(); //Private bit header.Channeling = (MP3Channeling)Source.Read(2).ToInt(); header.IsMSStereo = Source.Read(); header.IsIntensityStereo = Source.Read(); Source.Read(2); //Copyright bit, is original bit header.Emphasis = (MP3Emphasis)Source.Read(2).ToInt(); //Verify the header is good. if (header.Emphasis == MP3Emphasis.Reserved || header.SamplesSecond <= 0 || header.KilobitsSecond <= 0) return null; return header; }
14 May 2016
MP3 - Step 1 - Find the first header
An MP3 frame header starts with 11 set bits, and the 12th should also always be set. But finding 12 set bits doesn't garantee you found a frame header, so to make sure, you should verify several succeeding headers as well.
01 May 2016
Windows 10 - Start menu and apps not working
From time to time, for some reason, the Windows 10 start menu doesn't work, and apps don't launch.
Weird as it is, it can be fixed by running a PowerShell command:
- Right-click the task bar and select Task Management;
- Select File > Execute New Task;
- Type in "PowerShell", select the Administrator option and click OK;
- When PowerShell is ready, enter the following command and press enter:
Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"} - Restart the PC;
Labels:
Windows,
Windows Store App
01 March 2016
.NET - Creating an async-capable background task
In the old days we used BackgroundWorkers, but these days we want to create tasks we can run with the async keyword.
Here is an example for a task that will return a boolean:
public async TaskYou can now await this method and make it asynchronous:Test() { bool result = false; Exception error = null; await Task.Run(() => { try { result = someSlowComponent.getBooleanResult(); } catch (Exception ex) { error = ex; } }); if (error != null) throw error; else return result; }
public async bool GetValue()
{
bool result = await Test();
//Do more things with the result.
return result;
}
28 December 2015
C++ - Write a number to a file.
For debugging a C++ DLL, I needed a way to write the value of a numeric variable to a file. This is how:
Includes needed
#include <sstream> #include <fstream>
Convert the number
You can convert the number to a string using a stringstream.std::stringstream convert; convert << 305.24;
Output to a file
The result of the following is a text file at the DLL's location containing the number.std::ofstream out("Output.txt");
out << "Value: ";
out << convert.str();
out.close();
27 August 2015
JS - Convert a (nested) JSON object to HTML
For testing purposes I wanted to display the JSON retrieved from an API in a HTML page. I used Fiddler for that, but after some security was included that became impossible. The following Javascript function converts the JSON to a ordered list tree:
function jsonToHtml(data) {
var result = "<ol>";
for (var key in data) {
if (typeof (data[key]) == 'object' && data[key] != null) {
result += "<li><span class='key'>" + key + "</span>:<ul>";
result += jsonToHtml(data[key]);
result += '</ul></li>';
}
else {
result += "<li><span class='key'>" + key + '</span>: ';
result += '<span class="string">' + data[key] + '</span></li>';
}
};
result += '</ol>';
return (result);
}
05 August 2015
.NET - What is a GUID?
I found out something interesting today. I always thought a GUID was a string, but as it turns out, it's 128 bits of binary data that is represented in hexadecimal numbers, making it appear like a string. That makes the question of whether a GUID is case-insensitive irrelevant. It would also be better to store GUID's in a binary field rather than a text field.
21 July 2015
Windows Store App - Saving data on suspending
In my app I have a method WriteBinaryDataFile, that is called when the app is suspended. First it writes Started writing to the output window, then does it's work, and finishes by outputting Finished writing. Interestingly, Started writing appeared, but Finished writing didn't, exept when I terminated the app manually.
It turned out that the method needs to be async, and had to be awaited. The code is quite simple:
public async Task WriteBinaryDataFile()
{
//Do what needs to be done.
}
private async void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
await MainPage.WriteBinaryDataFile(); //Await and in the middle.
deferral.Complete();
}
Subscribe to:
Posts (Atom)