MIDI File Read with nAudio Interpet Sustain Values? - c#

.NET Framework 4.6, C#.
Using nAudio to read a MIDI file to convert the below into a notation structure that shows a single column with 16th notes and multiple sustain values if the note needs to be a quarter/half/full. So the result would start to look like this to display "C6" as a quarter note:
C6
S
S
S
I don't nuderstand how to read the Velocity and NoteLength properties/fields to get note duration values. Can someone point me in the right direction?
/*
Format 0, Tracks 1, Delta Ticks Per Quarter Note 480
1:1:0 0 MidiChannel
00
1:1:0 0 SequenceTrackName Classic Electric Piano
1:1:0 0 TrackInstrumentName Classic Electric Piano
1:1:0 0 SetTempo 89bpm (666667)
1:1:0 0 TimeSignature 4/4 TicksInClick:24 32ndsInQuarterNote:8
1:1:0 0 KeySignature 0 0
1:1:0 0 SmpteOffset 33:0:0:0:0
1:1:0 0 NoteOn Ch: 1 C6 Vel:87 Len: 167
1:1:240 240 NoteOn Ch: 1 C6 Vel:69 Len: 199
1:2:0 480 NoteOn Ch: 1 G6 Vel:74 Len: 149
1:2:240 720 NoteOn Ch: 1 G6 Vel:49 Len: 191
1:3:0 960 NoteOn Ch: 1 A6 Vel:65 Len: 165
1:3:240 1200 NoteOn Ch: 1 A6 Vel:50 Len: 171
1:4:0 1440 NoteOn Ch: 1 G6 Vel:89 Len: 401
*/
var dede = midiFile.DeltaTicksPerQuarterNote;
var tempolist = new List<TempoEvent>();
foreach (MidiEvent note in midiFile.Events[0])
{
if ( note.CommandCode == MidiCommandCode.NoteOn )
{
var nono = (NoteOnEvent)note;
var noname = nono.NoteName;
var notime= nono.AbsoluteTime;
double realtime = ( note.AbsoluteTime / dede ) * tempo[ tempo.Count() - 1 ].Tempo;
var nonum = nono.NoteNumber;
var nolen = nono.NoteLength;
}
}
Thanks.

Related

How to decode hexadecimal number in IBM/360 column binary format

I have a message:
x2400\x1100\x2001\x1020\x2100\x0900\x2008\x2012\x0900\x1001\x2001\x1010\x2001\x0900\x0802\x0812\x1200\x2010\x0802\x1004\x0820\x1010\x2100\x2002\x1012
It's in IBM column binary format. I read some documentation, but can't do by yourself.
https://www.masswerk.at/keypunch/?q=Mr.%20Donald%20F.%20Draper,%20104%20WAVERLY%20PLACE,%20APT%203R,%20NEW%20YORK,%20NY
The decoded message is:
ALIMCTF(TRINITY'KEYPUNCH)
In order to decode the message, the following must be considered:
Each character is identified by two bytes, e.g. \x2400 corresponds to A.
In a first step both bytes have to be decoded separately. For this purpose the IBM/360 Column Binary format (cbf) from the posted link (Advanced Usage section) has to be used. Each bit is assigned to a specific position, e.g. if byte 1 has the value 0x20, then that corresponds to bit 5 and thus to position Y. Analogously for byte 2, e.g. if byte 2 has the value 0x12 = 0x10 + 0x02, then this corresponds to bits 4 and 1 and thus to positions 5 and 8. In total, this results in the positions Y, 5 and 8 or Y58 for short.
In a second step, the character determined by the positions must be identified. To do this, the IBM 029 keypunch from the posted link (Usage section) must be used. E.g. the positions Y, 5 and 8 determine the character (.
If this is done for the entire message, the following table results:
Message Byte1, Byte2 Byte1, Byte2, Position Character
hex hex cbf cbf IBM 029
\x2400 24 00 Y1 0 Y1 A
\x1100 11 00 X3 0 X3 L
\x2001 20 01 Y 9 Y9 I
\x1020 10 20 X 4 X4 M
\x2100 21 00 Y3 0 Y3 C
\x0900 09 00 03 0 03 T
\x2008 20 08 Y 6 Y6 F
\x2012 20 12 Y 58 Y58 (
\x0900 09 00 03 0 03 T
\x1001 10 01 X 9 X9 R
\x2001 20 01 Y 9 Y9 I
\x1010 10 10 X 5 X5 N
\x2001 20 01 Y 9 Y9 I
\x0900 09 00 03 0 03 T
\x0802 08 02 0 8 08 Y
\x0812 08 12 0 58 58 '
\x1200 12 00 X2 0 X2 K
\x2010 20 10 Y 5 Y5 E
\x0802 08 02 0 8 08 Y
\x1004 10 04 X 7 X7 P
\x0820 08 20 0 4 04 U
\x1010 10 10 X 5 X5 N
\x2100 21 00 Y3 0 Y3 C
\x2002 20 02 Y 8 Y8 H
\x1012 10 12 X 58 X58 )
where the decoded message is in the last column (read from top to bottom).

Inconsistent DataGridViewColumn width when AutoSizeMode set to Fill

I created a simple Windows Form App to replicate this problem. I created a DataGridView1 and docked it onto Form1.
The DataGridView has 8 columns and each of them has this:
this.Column1.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
this.Column1.FillWeight = 100F;
Whenever I resize the form, all the columns should be resized to equal width. But that is not the case, sometimes, some of the columns are smaller than the rest.
I printed out the column width when Form1 ResizeEnds and get the following results after 10 times of resizing:
private void Form1_ResizeEnd(object sender, EventArgs e)
{
foreach (DataGridViewColumn c in dataGridView1.Columns)
{
Console.Write(c.Width + " ");
}
Console.WriteLine();
}
Output:
67 67 67 67 66 67 67 67
41 40 41 41 42 41 42 40
144 145 144 146 145 145 145 143
65 57 64 65 64 66 66 65
27 26 26 26 26 27 27 23
167 166 166 166 166 167 167 163
49 50 49 50 47 49 51 51
98 98 97 99 95 97 100 99
34 25 33 34 33 34 34 35
186 177 185 186 185 186 186 187
Its very frustrating to have this inconsistency and I hope there is a way to solve this. Thank you.
A screenshot of what the DataGridView looks like after resizing it multiple times:
Use a fixed font such as courier.
Pad the numbers with spaces so they have an equal length:
"888"
" 88"
" 8"
Don't set .AutoSizeMode and use this code (sorry, this is VB.NET but I think you have no problem translate it in C#):
Private Sub YourForm_ResizeEnd(sender As Object, e As System.EventArgs) Handles Me.ResizeEnd
Dim intRowHeadersWidthDefault As Integer = 41 'this is a default value; you can save it on form load
Dim intVerticalScrollbarWidth As Integer = 0
If Me.DataGridView1.Controls.OfType(Of VScrollBar)()(0).Visible Then
intVerticalScrollbarWidth = SystemInformation.VerticalScrollBarWidth
End If
Dim intBorderWidth As Integer = 0
If Me.DataGridView1.BorderStyle <> BorderStyle.None Then
intBorderWidth = 2
End If
Dim intSpaceToFill As Integer = Me.DataGridView1.Width - intRowHeadersWidthDefault - intVerticalScrollbarWidth - intBorderWidth
Dim intColumnWidth As Integer = intSpaceToFill / 8
For i As Integer = 0 To Me.DataGridView1.ColumnCount - 1
Me.DataGridView1.Columns(i).Width = intColumnWidth
Next i
Me.DataGridView1.RowHeadersWidth = intRowHeadersWidthDefault + intSpaceToFill - (intColumnWidth * 8)
End Sub
Basically this code force a column width to an integer and set row headers width to cover eventually remainder from space division.

How can I squeeze singleton dimension after getting a SubArray?

I have a two dimensional array 'v' representing a list of vectors
And I have a three dimensional array 'a' that represents a list of matrices
ILArray v = counter(2, 3);
ILArray a = counter(2, 3, 3);
Now I want to take a subarray of both and do a matrix multiply:
ILArray av = multiply(a[1,full,full], v[1,full]);
It will complain that the dimensions do not match and this is because the dimensions of a[1,full,full] is (1,3,3) and of v[1,full] is (1,3). In other words, the subarrays contain a singleton dimension. In matlab I can remove these singleton dimensions with squeeze(). But how can I do that in ILNumerics?
I just discovered that if the singleton dimension are trailing, then they are automagically removed by ILNumerics, so this works:
ILArray v = counter(3, 2);
ILArray a = counter(3, 3, 2);
ILArray av = multiply(a[full,full, 1], v[full, 1]);
Also, a[full,full,1] has dimension (3,3) and not (3,3,1).
Does that mean that ILNumerics does not support non-trailing singleton dimensions? In other words, does not support strided arrays in calculations but only contiguous ones? That would be rather restrictive to say the least.
Thanks,
Luc
Shouldn’t a simple reshape do the trick?
ILArray<int> I = ILMath.counter<int>(1,1,ILMath.size(5,4,3));
I
<Int32> [5,4,3]
[0]: (:,:,0)
[1]: 1 6 11 16
[2]: 2 7 12 17
[3]: 3 8 13 18
[4]: 4 9 14 19
[5]: 5 10 15 20
[6]: (:,:,1)
[7]: 21 26 31 36
[8]: 22 27 32 37
[9]: 23 28 33 38
[10]: 24 29 34 39
[11]: 25 30 35 40
[12]: (:,:,2)
[13]: 41 46 51 56
[14]: 42 47 52 57
[15]: 43 48 53 58
[16]: 44 49 54 59
[17]: 45 50 55 60
I["0;:;:"]
<Int32> [1,4,3]
[0]: (:,:,0)
[1]: 1 6 11 16
[2]: (:,:,1)
[3]: 21 26 31 36
[4]: (:,:,2)
[5]: 41 46 51 56
ILMath.reshape(I["0;:;:"],4,3)
<Int32> [4,3]
[0]: 1 21 41
[1]: 6 26 46
[2]: 11 31 51
[3]: 16 36 56
Having a squeeze function would be more convenient. You may open a feature request on the bugtracker in order to have it implemented in one of the next releases: http://ilnumerics.net/mantis

response.BinaryWrite problems (Text file contains PDF encoding)

So I have something very strange happening in my Asp.Net Website.
I have 2 seperate pages with the same code to download files stored in a SQL database.
One page is an admin version of the page where you can delete and upload files and also see the files ID.
The user end version of this page only displays the download button and the filename. The ID is hidden and used as a DataKey in the grid to select the file from the database.
Both pages work perfectly in development. But when I switched to our production staging server the user end version of the page will turn a simple one line text file into a bunch of gargly-goop PDF encoding when saving and opening it, while the admin version of the page still functions properly. PDF's download and view normal and word documents do the same thing that the text files do.
Here is the code behind for the admin download section:
protected void btnSaveAttachment_Click(object sender, EventArgs e)
{
GridViewRow selectedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
string AttachmentID = "";
string AttachmentName = "";
if (selectedRow.Cells[1].Text != null && selectedRow.Cells[2].Text != null)
{
AttachmentID = selectedRow.Cells[1].Text;
AttachmentName = selectedRow.Cells[2].Text;
}
byte[] objData = Utility.SaveAttachmentBytes(AttachmentID);
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ClearHeaders();
response.AddHeader("Cache-Control", " no-store, no-cache ");
response.AddHeader("Content-Disposition", "attachment; filename=" + AttachmentName + ";");
response.BinaryWrite(objData);
response.Flush();
if (response != null)
{
response.End();
}
}
Here is the code behind for the user download section:
protected void btnSaveAttachment_Click(object sender, EventArgs e)
{
GridViewRow selectedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
//string AttachmentID = selectedRow.Cells[1].Text;
string AttachmentID = grdAttachments.DataKeys[0].Value.ToString();
string AttachmentName = "";
if (selectedRow.Cells[1].Text != null)
{
AttachmentName = selectedRow.Cells[1].Text;
}
byte[] objData = Utility.SaveAttachmentBytes(AttachmentID);
HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ClearHeaders();
response.AddHeader("Cache-Control", " no-store, no-cache ");
response.AddHeader("Content-Disposition", "attachment; filename=" + AttachmentName + ";");
response.BinaryWrite(objData);
response.Flush();
//File.Delete(SavePath);
if (response != null)
{
response.End();
}
}
I am at a point where I just dont know what the heck is going on. The only other difference in the pages front end code is the user page load some text from the database into a label and has a telerik textbox control. (I have tried stripping out all other items on the page but still files other than PDF try to encode as a PDF.)
Here is what a 1 line text file looks like when saving and opening it.
%PDF-1.4
%âãÏÓ
9 0 obj<</H[516 160]/Linearized 1/E 5419/L 14363/N 2/O 12/T 14137>>
endobj
xref
9 11
0000000016 00000 n
0000000676 00000 n
0000000516 00000 n
0000000753 00000 n
0000000881 00000 n
0000000976 00000 n
0000001511 00000 n
0000001903 00000 n
0000002142 00000 n
0000002387 00000 n
0000002463 00000 n
trailer
<</Size 20/Prev 14127/Root 10 0 R/Info 8 0 R/ID[<3d8f2faf909b30f75011a461bd4aff97><b62071c85b58ab4d8f0b23af1811506f>]>>
startxref
0
%%EOF
11 0 obj<</Length 82/Filter/FlateDecode/L 90/S 53>>stream
xÚb```f``
‘BVœÀ cf`aàXÀàΰ…Ql
HT ÈAPÌÀàÃÀÃì ,³Ñ†Ë`%·Hˆ…Aý!fb€ O{Ì
endstream
endobj
10 0 obj<</Pages 6 0 R/Type/Catalog/PageLabels 4 0 R/Metadata 7 0 R>>
endobj
12 0 obj<</Contents 19 0 R/Type/Page/Parent 6 0 R/Rotate 0/MediaBox[0 0 612 792]/CropBox[0 0 612 792]/Resources 13 0 R>>
endobj
13 0 obj<</Font<</TT2 14 0 R/TT4 15 0 R>>/ProcSet[/PDF/Text]/ExtGState<</GS1 18 0 R>>>>
endobj
14 0 obj<</Type/Font/Encoding/WinAnsiEncoding/BaseFont/TimesNewRomanPSMT/FirstChar 32/LastChar 150/Subtype/TrueType/FontDescriptor 16 0 R/Widths[250 0 0 0 0 833 778 0 333 333 0 564 250 333 250 278 500 500 500 500 500 500 500 500 500 500 278 0 0 0 0 0 0 722 667 667 722 611 556 722 0 333 389 0 611 889 722 722 556 722 667 556 611 722 722 944 722 722 0 0 0 0 0 0 0 444 500 444 500 444 333 500 500 278 278 500 278 778 500 500 500 500 333 389 278 500 500 722 500 500 444 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 500]>>
endobj
15 0 obj<</Type/Font/Encoding/WinAnsiEncoding/BaseFont/TimesNewRomanPS-BoldMT/FirstChar 32/LastChar 121/Subtype/TrueType/FontDescriptor 17 0 R/Widths[250 0 0 0 0 0 0 0 0 0 0 0 0 0 250 0 500 500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 722 722 0 0 0 0 389 0 0 0 0 0 778 0 0 0 556 667 722 0 0 0 0 0 0 0 0 0 0 0 500 0 444 0 444 333 500 0 278 0 0 0 833 556 0 0 0 444 389 333 556 500 722 0 500]>>
endobj
16 0 obj<</Type/FontDescriptor/FontBBox[-568 -307 2028 1007]/FontName/TimesNewRomanPSMT/Flags 34/StemV 82/CapHeight 656/XHeight 0/Ascent 891/Descent -216/ItalicAngle 0/FontFamily(Times New Roman)/FontStretch/Normal/FontWeight 400>>
endobj
17 0 obj<</Type/FontDescriptor/FontBBox[-558 -307 2034 1026]/FontName/TimesNewRomanPS-BoldMT/Flags 34/StemV 136/CapHeight 656/XHeight 0/Ascent 891/Descent -216/ItalicAngle 0/FontFamily(Times New Roman)/FontStretch/Normal/FontWeight 700>>
endobj
18 0 obj<</Type/ExtGState/SA false/OP false/SM 0.02/op false/OPM 1>>
endobj
etc....................
endstream
endobj
4 0 obj<</Nums[0 5 0 R]>>
endobj
5 0 obj<</S/D>>
endobj
6 0 obj<</Count 2/Kids[12 0 R 1 0 R]/Type/Pages>>
endobj
7 0 obj<</Length 3339/Type/Metadata/Subtype/XML>>stream
<?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?>
<?adobe-xap-filters esc="CRLF"?>
<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='XMP toolkit 2.9.1-13, framework 1.6'>
<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:iX='http://ns.adobe.com/iX/1.0/'>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:pdf='http://ns.adobe.com/pdf/1.3/' pdf:Producer='Acrobat Distiller 6.0 (Windows)'></rdf:Description>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:xap='http://ns.adobe.com/xap/1.0/' xap:CreatorTool='PScript5.dll Version 5.2' xap:ModifyDate='2005-06-10T14:07:36-04:00' xap:CreateDate='2005-06-10T14:07:36-04:00'></rdf:Description>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:xapMM='http://ns.adobe.com/xap/1.0/mm/' xapMM:DocumentID='uuid:2aee5402-c607-44cd-a815-8ad3a0bf0a56'/>
<rdf:Description rdf:about='uuid:d5ef0fdf-fd89-4be0-a57d-fcab92aa8d2b' xmlns:dc='http://purl.org/dc/elements/1.1/' dc:format='application/pdf'><dc:title><rdf:Alt><rdf:li xml:lang='x-default'>Microsoft Word - 1 - DTOD Overview.doc</rdf:li></rdf:Alt></dc:title><dc:creator><rdf:Seq><rdf:li>AttardA</rdf:li></rdf:Seq></dc:creator></rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='w'?>
endstream
endobj
8 0 obj<</ModDate(D:20050610140736-04'00')/CreationDate(D:20050610140736-04'00')/Title(Microsoft Word - 1 - DTOD Overview.doc)/Creator(PScript5.dll Version 5.2)/Producer(Acrobat Distiller 6.0 \(Windows\))/Author(AttardA)>>
endobj
xref
0 9
0000000000 65535 f
0000005419 00000 n
0000005544 00000 n
0000005638 00000 n
0000010369 00000 n
0000010402 00000 n
0000010425 00000 n
0000010482 00000 n
0000013897 00000 n
trailer
<</Size 9>>
startxref
116
%%EOF
RESOLVED
The issue was the DataKeys[0] was always selecting the 0 item in the array of keys. I changed that to:
string AttachmentID = grdAttachments.DataKeys[selectedRow.RowIndex].Value.ToString();
And now all is working fine! Wish I would have debugged closer into the attachmentID!
It just so happened that there was 2 documents that were the same and I though I was downloading the correct document.
In your admin section:
string AttachmentID = selectedRow.Cells[1].Text;
In your user section:
string AttachmentID = grdAttachments.DataKeys[0].Value.ToString();
I would have to assume that the user section assignment is not doing what you expect it to, since the logic isn't the same for both. Verify what AttachmentID is set to in each case, perhaps via logging.

Include conditional checking in query with LINQ to SQL

Suggestion either in C# or VB.NET are welcome.
Table relationship:
Student 1:N TimeSheet (FK StudentId)
TimeSheet 1:N TimeRecord (FK TimeSheetId)
Dim query = From s In db.Students _
Let pair = (From ts In db.TimeSheets _
Join tr In db.TimeRecords On tr.TimeSheetId Equals ts.TimeSheetId _
Where ts.IsArchive = False And ts.IsCompleted = False _
Group By key = New With {ts.TimeSheetId, ts.StudentId} Into TotalHour = Sum(tr.BonusHour)) _
From part In pair _
Where part.key.StudentId = s.StudentId _
Select New With {.StudentId = s.StudentId, .AssignedId = s.AssignedId,.TotalTime = part.TotalHour}
Here's the result of the query:
734 -- 159 : 9 hrs 35 mm 28 sec
2655 -- 160 : 93 hrs 33 mm 50 sec
1566 -- 161 : 37 hrs 23 mm 53 sec
3114 -- 162 : 25 hrs 0 mm 21 sec
Wanted result of query:
733 -- 158 : 0 hr 0mm 0 sec
734 -- 159 : 9 hrs 35 mm 28 sec
736 -- 169 : 0 hrs 0mm 0sec
2655 -- 160 : 93 hrs 33 mm 50 sec
1566 -- 161 : 37 hrs 23 mm 53 sec
3114 -- 162 : 25 hrs 0 mm 21 sec
2165 -- 189 : 0 hr 0 mm 21 sec
There are some TimeSheet that have no TimeRecord, which I need to select as well. How can I select all of them to make selection like above wanted result? I'm thinking of how I can include some condtion
checking in the query to see if this TimeSheet has no TimeRecord then no need to Sum(tr.BonusHour) just assign TotalHour to zero. I don't know it's right way to go.
Any sugestion is welcome.
You can try doing something like this with the Sum (C#):
Sum(tr.BonusHour ?? 0)
which would be the same as
Sum(tr.BonusHour != null ? tr.BonusHour : 0)
I am not sure what type your BonusHour has, so you would use zero-correspondent object of this type instead of the 0 in the sample.

Categories

Resources