Quantcast
Channel: NAudio
Viewing all 5831 articles
Browse latest View live

New Post: SoundFont presets

$
0
0
Incidentally for those interested, I figured out how to do this.
First, you don't go through the Presets but rather the Instruments.
You have to go through the Zones and get their Generators, and access the Generators that are of GeneratorType.SampleID.
Those Generators will have an associated SampleHeader, from which you get the offsets for that Generator in the SampleData.

At least that's how I managed to do it...

New Post: MeteringSampleProvider and AsioOut

$
0
0
Ah, Thanks! That solved it for Asio.

Moved my UI update code from the StreamVolume event handler to a different Sub called "UpdateMeters" and changed the event handler code to:
        MaxSampleValues = e.MaxSampleValues  'To use in updating Sub
        If Not Dispatcher.CheckAccess() Then
            Dispatcher.BeginInvoke(DispatcherPriority.Background, Sub() UpdateMeters())
        End If
Works fine, thx.

New Post: AcmInterop and AcmFormatSuggestFlags ??

$
0
0
Hi,

I create project for converting file and when I want use AcmInterop and AcmFormatSuggestFlags visual studio say me not exist. But in NAudio.xml file is exist but when I try NAudio.Wave.Compression I have just

AcmDriver
AcmDriverDetailsSupportFlags
AcmFormt
AcmFormatEnumFlags
AcmFormatTags
AcmStream
WaveFilter

Where are the another options ???

New Post: Right ear is still playing

$
0
0
I wrote some code to the class sound would only play out of one earphone/speaker but is it playing out of both at the same time... why ?
here is my code
public override int Read(byte[] buffer, int offset, int sampleCount)
    {
        if (position == 0 && onetimeflag == false)
        {
            n2 = 0;
            onetimeflag = true;
        }
        if (n2 >= Bufferlength && stopflag == false)
        {
            Dispose();
            return 0;
        }

        float temp1;
        for (int i = 0; i < (sampleCount / 4); i++)
        {
            if (Frequency_switch == true)
            {
                temp1 = (float)(Amplitude * Math.Sin(Math.PI * Frequency * n2 / 44100D));
                Frequency_switch = false;
            }
            else
            {

                temp1 = (float)(Amplitude2 * Math.Sin((Math.PI * Frequency2 * n2) / 44100D));
                Frequency_switch = true;
            }
            byte[] bytes = BitConverter.GetBytes(temp1);
            buffer[i * 4 + 0] = bytes[0];
            buffer[i * 4 + 1] = bytes[1];
            buffer[i * 4 + 2] = bytes[2];
            buffer[i * 4 + 3] = bytes[3];
            tempSample++;
            n2++;
        }
        return sampleCount;}} }

I set Amplitude2 to 0 so it should only play from one earphone right ?
my waveform is set up like this
public override WaveFormat WaveFormat { get { return WaveFormat.CreateIeeeFloatWaveFormat(44100, 2); } }

New Post: Right ear is still playing

$
0
0
From the looks of things, regardless of Frequency_switch, that is regardless of Amplitude or Amplitude2, you're still assigning it to all of your bytes (left and right), so you'll still get the sound in left&right.

Assuming 16-bit stereo, you would need to set buffer[i * 4 + 0] and buffer[i * 4 + 1] to your Amplitude bytes, and then buffer[i * 4 + 2] and buffer[i * 4 + 3] to zero, if you want sound out of the left (or opposite, if you want it out of the right side).

Or if this is 32-bit, assign your Amplitude to all 4 bytes, and then the next +4, +5, +6 and +7 to zero.

New Post: Right ear is still playing

$
0
0
ok temp1 is a float value....

so I think you need to set the next 4 bytes to zero (+4, +5, +6 +7)
and change i < (sampleCount / 4) to i < (sampleCount / 8)

New Post: Right ear is still playing

$
0
0
Msav,

I am doing that now. I fill up all 4 indexes in the buffer. so the 1st 4 have sound and the next 4 are zero but I am still here sound from both earphone.

New Post: Right ear is still playing

$
0
0
Amplitude2 is set to zero, so when you Frequency_switch = false the values are 0. and when Frequency_switch = true, there are real values ..

are am i missing something ?

New Post: Right ear is still playing

$
0
0
it is 32 bit per sample and this is how the buffer looks :
```
  • buffer {byte[52920]} byte[]
    [0] 0   byte
    [1] 0   byte
    [2] 0   byte
    [3] 0   byte
    [4] 93  byte
    [5] 21  byte
    [6] 82  byte
    [7] 60  byte
    [8] 0   byte
    [9] 0   byte
    [10]    0   byte
    [11]    0   byte
    [12]    46  byte
    [13]    135 byte
    [14]    29  byte
    [15]    61  byte
    [16]    0   byte
    [17]    0   byte
    [18]    0   byte
    [19]    0   byte
    [20]    63  byte
    [21]    55  byte
    [22]    131 byte
    [23]    61  byte
    [24]    0   byte
    [25]    0   byte
    [26]    0   byte
    [27]    0   byte
    [28]    208 byte
    [29]    148 byte
    [30]    183 byte
    [31]    61  byte
    [32]    0   byte
    [33]    0   byte
    [34]    0   byte
    [35]    0   byte
    [36]    121 byte
    [37]    211 byte
    [38]    235 byte
    [39]    61  byte
    [40]    0   byte
    [41]    0   byte
    [42]    0   byte
    [43]    0   byte
    [44]    55  byte
    [45]    245 byte
    [46]    15  byte
    [47]    62  byte
    [48]    0   byte
    [49]    0   byte
    [50]    0   byte
    [51]    0   byte
    [52]    116 byte
    [53]    232 byte
    [54]    41  byte
    [55]    62  byte
    [56]    0   byte
    [57]    0   byte
    [58]    0   byte
    [59]    0   byte
    [60]    22  byte
    [61]    191 byte
    [62]    67  byte
    [63]    62  byte
    [64]    0   byte
    [65]    0   byte
    [66]    0   byte
    [67]    0   byte
    [68]    196 byte
    [69]    116 byte
    [70]    93  byte
    [71]    62  byte
    [72]    0   byte
    [73]    0   byte
    [74]    0   byte
    [75]    0   byte
    [76]    41  byte
    [77]    5   byte
    [78]    119 byte
    [79]    62  byte
    [80]    0   byte
    [81]    0   byte
    [82]    0   byte
    [83]    0   byte
    [84]    251 byte
    [85]    53  byte
    [86]    136 byte
    [87]    62  byte
    [88]    0   byte
    [89]    0   byte
    [90]    0   byte
    [91]    0   byte
    [92]    115 byte
    [93]    210 byte
    [94]    148 byte
    [95]    62  byte
    [96]    0   byte
    [97]    0   byte
    [98]    0   byte
    [99]    0   byte
    [100]   221 byte
    [101]   85  byte
    [102]   161 byte
    [103]   62  byte
    [104]   0   byte
    [105]   0   byte
    [106]   0   byte
    [107]   0   byte
    [108]   29  byte
    [109]   190 byte
    [110]   173 byte
    [111]   62  byte
    [112]   0   byte
    [113]   0   byte
    [114]   0   byte
    [115]   0   byte
    [116]   29  byte
    [117]   9   byte
    [118]   186 byte
    [119]   62  byte
    [120]   0   byte
    [121]   0   byte
    [122]   0   byte
    [123]   0   byte
    [124]   203 byte
    [125]   52  byte
    [126]   198 byte
    [127]   62  byte
    [128]   0   byte
    [129]   0   byte
    [130]   0   byte
    [131]   0   byte
    [132]   25  byte
    [133]   63  byte
    [134]   210 byte
    [135]   62  byte
    [136]   0   byte
    [137]   0   byte
    [138]   0   byte
    [139]   0   byte
    [140]   2   byte
    [141]   38  byte
    [142]   222 byte
    [143]   62  byte
    [144]   0   byte
    [145]   0   byte
    [146]   0   byte
    [147]   0   byte
    [148]   133 byte
    [149]   231 byte
    [150]   233 byte
    [151]   62  byte
    [152]   0   byte
    [153]   0   byte
    [154]   0   byte
    [155]   0   byte
    [156]   165 byte
    [157]   129 byte
    [158]   245 byte
    [159]   62  byte
    [160]   0   byte
    [161]   0   byte
    [162]   0   byte
    [163]   0   byte
    [164]   56  byte
    [165]   121 byte
    [166]   0   byte
    [167]   63  byte
    [168]   0   byte
    [169]   0   byte
    [170]   0   byte
    [171]   0   byte
    [172]   253 byte
    [173]   27  byte
    [174]   6   byte
    [175]   63  byte
    [176]   0   byte
    [177]   0   byte
    [178]   0   byte
    [179]   0   byte
    [180]   45  byte
    [181]   168 byte
    [182]   11  byte
    [183]   63  byte
    [184]   0   byte
    [185]   0   byte
    [186]   0   byte
    [187]   0   byte
    [188]   218 byte
    [189]   28  byte
    [190]   17  byte
    [191]   63  byte
    [192]   0   byte
    [193]   0   byte
    [194]   0   byte
    [195]   0   byte
    [196]   24  byte
    [197]   121 byte
    [198]   22  byte
    [199]   63  byte
    [200]   0   byte
    [201]   0   byte
    [202]   0   byte
    [203]   0   byte
    [204]   1   byte
    [205]   188 byte
    [206]   27  byte
    [207]   63  byte
    [208]   0   byte
    [209]   0   byte
    [210]   0   byte
    [211]   0   byte
    [212]   178 byte
    [213]   228 byte
    [214]   32  byte
    [215]   63  byte
    [216]   0   byte
    [217]   0   byte
    [218]   0   byte
    [219]   0   byte
    [220]   76  byte
    [221]   242 byte
    [222]   37  byte
    [223]   63  byte
    [224]   0   byte
    [225]   0   byte
    [226]   0   byte
    [227]   0   byte
    [228]   245 byte
    [229]   227 byte
    [230]   42  byte
    [231]   63  byte
    [232]   0   byte
    [233]   0   byte
    [234]   0   byte
    [235]   0   byte
    [236]   218 byte
    [237]   184 byte
    [238]   47  byte
    [239]   63  byte
    [240]   0   byte
    [241]   0   byte
    [242]   0   byte
    [243]   0   byte
    [244]   40  byte
    [245]   112 byte
    [246]   52  byte
    [247]   63  byte
    [248]   0   byte
    [249]   0   byte
    [250]   0   byte
    [251]   0   byte
    [252]   22  byte
    [253]   9   byte
    [254]   57  byte
    [255]   63  byte
    [256]   0   byte
    [257]   0   byte
    [258]   0   byte
    [259]   0   byte
    [260]   220 byte
    [261]   130 byte
    [262]   61  byte
    [263]   63  byte
    [264]   0   byte
    [265]   0   byte
    [266]   0   byte
    [267]   0   byte
    [268]   186 byte
    [269]   220 byte
    [270]   65  byte
    [271]   63  byte
    [272]   0   byte
    [273]   0   byte
    [274]   0   byte
    [275]   0   byte
    [276]   245 byte
    [277]   21  byte
    [278]   70  byte
    [279]   63  byte
    [280]   0   byte
    [281]   0   byte
    [282]   0   byte
    [283]   0   byte
    [284]   214 byte
    [285]   45  byte
    [286]   74  byte
    [287]   63  byte
    [288]   0   byte
    [289]   0   byte
    [290]   0   byte
    [291]   0   byte
    [292]   173 byte
    [293]   35  byte
    [294]   78  byte
    [295]   63  byte
    [296]   0   byte
    [297]   0   byte
    [298]   0   byte
    [299]   0   byte
    [300]   207 byte
    [301]   246 byte
    [302]   81  byte
    [303]   63  byte
    [304]   0   byte
    [305]   0   byte
    [306]   0   byte
    [307]   0   byte
    [308]   152 byte
    [309]   166 byte
    [310]   85  byte
    [311]   63  byte
    [312]   0   byte
    [313]   0   byte
    [314]   0   byte
    [315]   0   byte
    [316]   104 byte
    [317]   50  byte
    [318]   89  byte
    [319]   63  byte
    [320]   0   byte
    [321]   0   byte
    [322]   0   byte
    [323]   0   byte
    [324]   167 byte
    [325]   153 byte
    [326]   92  byte
    [327]   63  byte
    [328]   0   byte
    [329]   0   byte
    [330]   0   byte
    [331]   0   byte
    [332]   194 byte
    [333]   219 byte
    [334]   95  byte
    [335]   63  byte
    [336]   0   byte
    [337]   0   byte
    [338]   0   byte
    [339]   0   byte
    [340]   44  byte
    [341]   248 byte
    [342]   98  byte
    [343]   63  byte
    [344]   0   byte
    [345]   0   byte
    [346]   0   byte
    [347]   0   byte
    [348]   96  byte
    [349]   238 byte
    [350]   101 byte
    [351]   63  byte
    [352]   0   byte
    [353]   0   byte
    [354]   0   byte
    [355]   0   byte
    [356]   222 byte
    [357]   189 byte
    [358]   104 byte
    [359]   63  byte
    [360]   0   byte
    [361]   0   byte
    [362]   0   byte
    [363]   0   byte
    [364]   44  byte
    [365]   102 byte
    [366]   107 byte
    [367]   63  byte
    [368]   0   byte
    [369]   0   byte
    [370]   0   byte
    [371]   0   byte
    [372]   217 byte
    [373]   230 byte
    [374]   109 byte
    [375]   63  byte
    [376]   0   byte
    [377]   0   byte
    [378]   0   byte
    [379]   0   byte
    [380]   119 byte
    [381]   63  byte
    [382]   112 byte
    [383]   63  byte
    [384]   0   byte
    [385]   0   byte
    [386]   0   byte
    [387]   0   byte
    [388]   163 byte
    [389]   111 byte
    [390]   114 byte
    [391]   63  byte
    [392]   0   byte
    [393]   0   byte
    [394]   0   byte
    [395]   0   byte
    [396]   254 byte
    [397]   118 byte
    [398]   116 byte
    [399]   63  byte
    [400]   0   byte
    [401]   0   byte
    [402]   0   byte
    [403]   0   byte
    [404]   48  byte
    [405]   85  byte
    [406]   118 byte
    [407]   63  byte
    [408]   0   byte
    [409]   0   byte
    [410]   0   byte
    [411]   0   byte
    [412]   234 byte
    [413]   9   byte
    [414]   120 byte
    [415]   63  byte
    [416]   0   byte
    [417]   0   byte
    [418]   0   byte
    [419]   0   byte
    [420]   224 byte
    [421]   148 byte
    [422]   121 byte
    [423]   63  byte
    [424]   0   byte
    [425]   0   byte
    [426]   0   byte
    [427]   0   byte
    [428]   210 byte
    [429]   245 byte
    [430]   122 byte
    [431]   63  byte
    [432]   0   byte
    [433]   0   byte
    [434]   0   byte
    [435]   0   byte
    [436]   130 byte
    [437]   44  byte
    [438]   124 byte
    [439]   63  byte
    [440]   0   byte
    [441]   0   byte
    [442]   0   byte
    [443]   0   byte
    [444]   190 byte
    [445]   56  byte
    [446]   125 byte
    [447]   63  byte
    [448]   0   byte
    [449]   0   byte
    [450]   0   byte
    [451]   0   byte
    [452]   88  byte
    [453]   26  byte
    [454]   126 byte
    [455]   63  byte
    [456]   0   byte
    [457]   0   byte
    [458]   0   byte
    [459]   0   byte
    [460]   42  byte
    [461]   209 byte
    [462]   126 byte
    [463]   63  byte
    [464]   0   byte
    [465]   0   byte
    [466]   0   byte
    [467]   0   byte
    [468]   22  byte
    [469]   93  byte
    [470]   127 byte
    [471]   63  byte
    [472]   0   byte
    [473]   0   byte
    [474]   0   byte
    [475]   0   byte
    [476]   2   byte
    [477]   190 byte
    [478]   127 byte
    [479]   63  byte
    [480]   0   byte
    [481]   0   byte
    [482]   0   byte
    [483]   0   byte
    [484]   225 byte
    [485]   243 byte
    [486]   127 byte
    [487]   63  byte
    [488]   0   byte
    [489]   0   byte
    [490]   0   byte
    [491]   0   byte
    [492]   167 byte
    [493]   254 byte
    [494]   127 byte
    [495]   63  byte
    [496]   0   byte
    [497]   0   byte
    [498]   0   byte
    [499]   0   byte
    [500]   84  byte
    [501]   222 byte
    [502]   127 byte
    [503]   63  byte
    [504]   0   byte
    [505]   0   byte
    [506]   0   byte
    [507]   0   byte
    [508]   237 byte
    [509]   146 byte
    [510]   127 byte
    [511]   63  byte
    [512]   0   byte
    [513]   0   byte
    [514]   0   byte
    [515]   0   byte
    [516]   126 byte
    [517]   28  byte
    [518]   127 byte
    [519]   63  byte
    [520]   0   byte
    [521]   0   byte
    [522]   0   byte
    [523]   0   byte
    [524]   28  byte
    [525]   123 byte
    [526]   126 byte
    [527]   63  byte
    [528]   0   byte
    [529]   0   byte
    [530]   0   byte
    [531]   0   byte
    [532]   226 byte
    [533]   174 byte
    [534]   125 byte
    [535]   63  byte
    [536]   0   byte
    [537]   0   byte
    [538]   0   byte
    [539]   0   byte
    [540]   242 byte
    [541]   183 byte
    [542]   124 byte
    [543]   63  byte
    [544]   0   byte
    [545]   0   byte
    [546]   0   byte
    [547]   0   byte
    [548]   117 byte
    [549]   150 byte
    [550]   123 byte
    [551]   63  byte
    [552]   0   byte
    [553]   0   byte
    [554]   0   byte
    [555]   0   byte
    [556]   157 byte
    [557]   74  byte
    [558]   122 byte
    [559]   63  byte
    [560]   0   byte
    [561]   0   byte
    [562]   0   byte
    [563]   0   byte
    [564]   162 byte
    [565]   212 byte
    [566]   120 byte
    [567]   63  byte
    [568]   0   byte
    [569]   0   byte
    [570]   0   byte
    [571]   0   byte
    [572]   193 byte
    [573]   52  byte
    [574]   119 byte
    [575]   63  byte
    [576]   0   byte
    [577]   0   byte
    [578]   0   byte
    [579]   0   byte
    [580]   66  byte
    [581]   107 byte
    [582]   117 byte
    [583]   63  byte
    [584]   0   byte
    [585]   0   byte
    [586]   0   byte
    [587]   0   byte
    [588]   113 byte
    [589]   120 byte
    [590]   115 byte
    [591]   63  byte
    [592]   0   byte
    [593]   0   byte
    [594]   0   byte
    [595]   0   byte
    [596]   162 byte
    [597]   92  byte
    [598]   113 byte
    [599]   63  byte
    [600]   0   byte
    [601]   0   byte
    [602]   0   byte
    [603]   0   byte
    [604]   48  byte
    [605]   24  byte
    [606]   111 byte
    [607]   63  byte
    [608]   0   byte
    [609]   0   byte
    [610]   0   byte
    [611]   0   byte
    [612]   125 byte
    [613]   171 byte
    [614]   108 byte
    [615]   63  byte
    [616]   0   byte
    [617]   0   byte
    [618]   0   byte
    [619]   0   byte
    [620]   242 byte
    [621]   22  byte
    [622]   106 byte
    [623]   63  byte
    [624]   0   byte
    [625]   0   byte
    [626]   0   byte
    [627]   0   byte
    [628]   253 byte
    [629]   90  byte
    [630]   103 byte
    [631]   63  byte
    [632]   0   byte
    
that should be right right ?
why is it not working ?

New Post: Right ear is still playing

$
0
0
ok I just tried this:

but It did not play anything at all:
 public override int Read(byte[] buffer, int offset, int sampleCount)
        {
            if (position == 0 && onetimeflag == false)
            {
                n2 = 0;
                onetimeflag = true;
            }
            if (n2 >= Bufferlength && stopflag == false)
            {
                Dispose();
                return 0;
            }

            float temp1;
            for (int i = 0; i < (sampleCount / 2); i++)
            {
                if (Frequency_switch == true)
                {
                    temp1 = (float)(Amplitude * Math.Sin(Math.PI * Frequency * n2 / 44100D));
                    Frequency_switch = false;
                }
                else
                {

                    temp1 = (float)(Amplitude2 * Math.Sin((Math.PI * Frequency2 * n2) / 44100D));
                    Frequency_switch = true;
                }
                byte[] bytes = BitConverter.GetBytes(temp1);
                buffer[i * 2 + 0] = bytes[0];
                buffer[i * 2 + 1] = bytes[1];
                //buffer[i * 4 + 2] = bytes[2];
                //buffer[i * 4 + 3] = bytes[3];
                tempSample++;
                n2++;
            }
            return sampleCount;}} }

New Post: Right ear is still playing

$
0
0
ok I tried everything and it is not working
what I am missing ?

I even trying have 8 of the zero in a roll and it is not working...
I tried making using channel..

idk what to try now

New Post: Right ear is still playing

$
0
0
I was thinking something more like this:
for (int i = 0; i < (sampleCount / 8); i++)
...
and
 byte[] bytes = BitConverter.GetBytes(temp1);
            buffer[i * 8 + 0] = bytes[0];
            buffer[i * 8 + 1] = bytes[1];
            buffer[i * 8 + 2] = bytes[2];
            buffer[i * 8 + 3] = bytes[3];
            buffer[i * 8 + 4] = 0;
            buffer[i * 8 + 5] = 0;
            buffer[i * 8 + 6] = 0;
            buffer[i * 8 + 7] = 0;
(since there's 4 bytes for each sample, left and right, so you have to progress by 8 each time)

New Post: Right ear is still playing

$
0
0
try that and it did not work..

here is how the buffer looked :

```
  • this {Hearing_Test.binaural_beats} Hearing_Test.binaural_beats
  • buffer {byte[52920]} byte[]
    [0] 0   byte
    [1] 0   byte
    [2] 0   byte
    [3] 0   byte
    [4] 0   byte
    [5] 0   byte
    [6] 0   byte
    [7] 0   byte
    [8] 93  byte
    [9] 21  byte
    [10]    82  byte
    [11]    60  byte
    [12]    0   byte
    [13]    0   byte
    [14]    0   byte
    [15]    0   byte
    [16]    0   byte
    [17]    0   byte
    [18]    0   byte
    [19]    0   byte
    [20]    0   byte
    [21]    0   byte
    [22]    0   byte
    [23]    0   byte
    [24]    46  byte
    [25]    135 byte
    [26]    29  byte
    [27]    61  byte
    [28]    0   byte
    [29]    0   byte
    [30]    0   byte
    [31]    0   byte
    [32]    0   byte
    [33]    0   byte
    [34]    0   byte
    [35]    0   byte
    [36]    0   byte
    [37]    0   byte
    [38]    0   byte
    [39]    0   byte
    [40]    63  byte
    [41]    55  byte
    [42]    131 byte
    [43]    61  byte
    [44]    0   byte
    [45]    0   byte
    [46]    0   byte
    [47]    0   byte
    [48]    0   byte
    [49]    0   byte
    [50]    0   byte
    [51]    0   byte
    [52]    0   byte
    [53]    0   byte
    [54]    0   byte
    [55]    0   byte
    [56]    208 byte
    [57]    148 byte
    [58]    183 byte
    [59]    61  byte
    [60]    0   byte
    [61]    0   byte
    [62]    0   byte
    [63]    0   byte
    [64]    0   byte
    [65]    0   byte
    [66]    0   byte
    [67]    0   byte
    [68]    0   byte
    [69]    0   byte
    [70]    0   byte
    [71]    0   byte
    [72]    121 byte
    [73]    211 byte
    [74]    235 byte
    [75]    61  byte
    [76]    0   byte
    [77]    0   byte
    [78]    0   byte
    [79]    0   byte
    [80]    0   byte
    [81]    0   byte
    [82]    0   byte
    [83]    0   byte
    [84]    0   byte
    [85]    0   byte
    [86]    0   byte
    [87]    0   byte
    [88]    55  byte
    [89]    245 byte
    [90]    15  byte
    [91]    62  byte
    [92]    0   byte
    [93]    0   byte
    [94]    0   byte
    [95]    0   byte
    [96]    0   byte
    [97]    0   byte
    [98]    0   byte
    [99]    0   byte
    [100]   0   byte
    [101]   0   byte
    [102]   0   byte
    [103]   0   byte
    [104]   116 byte
    [105]   232 byte
    [106]   41  byte
    [107]   62  byte
    [108]   0   byte
    [109]   0   byte
    [110]   0   byte
    [111]   0   byte
    [112]   0   byte
    [113]   0   byte
    [114]   0   byte
    [115]   0   byte
    [116]   0   byte
    [117]   0   byte
    [118]   0   byte
    [119]   0   byte
    [120]   22  byte
    [121]   191 byte
    [122]   67  byte
    [123]   62  byte
    [124]   0   byte
    [125]   0   byte
    [126]   0   byte
    [127]   0   byte
    [128]   0   byte
    [129]   0   byte
    [130]   0   byte
    [131]   0   byte
    [132]   0   byte
    [133]   0   byte
    [134]   0   byte
    [135]   0   byte
    [136]   196 byte
    [137]   116 byte
    [138]   93  byte
    [139]   62  byte
    [140]   0   byte
    [141]   0   byte
    [142]   0   byte
    [143]   0   byte
    [144]   0   byte
    [145]   0   byte
    [146]   0   byte
    [147]   0   byte
    [148]   0   byte
    [149]   0   byte
    [150]   0   byte
    [151]   0   byte
    [152]   41  byte
    [153]   5   byte
    [154]   119 byte
    [155]   62  byte
    [156]   0   byte
    [157]   0   byte
    [158]   0   byte
    [159]   0   byte
    [160]   0   byte
    [161]   0   byte
    [162]   0   byte
    [163]   0   byte
    [164]   0   byte
    [165]   0   byte
    [166]   0   byte
    [167]   0   byte
    [168]   251 byte
    [169]   53  byte
    [170]   136 byte
    [171]   62  byte
    [172]   0   byte
    [173]   0   byte
    [174]   0   byte
    [175]   0   byte
    [176]   0   byte
    [177]   0   byte
    [178]   0   byte
    [179]   0   byte
    [180]   0   byte
    [181]   0   byte
    [182]   0   byte
    [183]   0   byte
    [184]   115 byte
    [185]   210 byte
    [186]   148 byte
    [187]   62  byte
    [188]   0   byte
    [189]   0   byte
    [190]   0   byte
    [191]   0   byte
    [192]   0   byte
    [193]   0   byte
    [194]   0   byte
    [195]   0   byte
    [196]   0   byte
    [197]   0   byte
    [198]   0   byte
    [199]   0   byte
    [200]   221 byte
    [201]   85  byte
    [202]   161 byte
    [203]   62  byte
    [204]   0   byte
    [205]   0   byte
    [206]   0   byte
    [207]   0   byte
    [208]   0   byte
    [209]   0   byte
    [210]   0   byte
    [211]   0   byte
    [212]   0   byte
    [213]   0   byte
    [214]   0   byte
    [215]   0   byte
    [216]   29  byte
    [217]   190 byte
    [218]   173 byte
    [219]   62  byte
    [220]   0   byte
    [221]   0   byte
    [222]   0   byte
    [223]   0   byte
    [224]   0   byte
    [225]   0   byte
    [226]   0   byte
    [227]   0   byte
    [228]   0   byte
    [229]   0   byte
    [230]   0   byte
    [231]   0   byte
    [232]   29  byte
    [233]   9   byte
    [234]   186 byte
    [235]   62  byte
    [236]   0   byte
    [237]   0   byte
    [238]   0   byte
    [239]   0   byte
    [240]   0   byte
    [241]   0   byte
    [242]   0   byte
    [243]   0   byte
    [244]   0   byte
    [245]   0   byte
    [246]   0   byte
    [247]   0   byte
    [248]   203 byte
    [249]   52  byte
    [250]   198 byte
    [251]   62  byte
    [252]   0   byte
    [253]   0   byte
    [254]   0   byte
    [255]   0   byte
    [256]   0   byte
    [257]   0   byte
    [258]   0   byte
    [259]   0   byte
    [260]   0   byte
    [261]   0   byte
    [262]   0   byte
    [263]   0   byte
    [264]   25  byte
    [265]   63  byte
    [266]   210 byte
    [267]   62  byte
    [268]   0   byte
    [269]   0   byte
    [270]   0   byte
    [271]   0   byte
    [272]   0   byte
    [273]   0   byte
    [274]   0   byte
    [275]   0   byte
    [276]   0   byte
    [277]   0   byte
    [278]   0   byte
    [279]   0   byte
    [280]   2   byte
    [281]   38  byte
    [282]   222 byte
    [283]   62  byte
    [284]   0   byte
    [285]   0   byte
    [286]   0   byte
    [287]   0   byte
    [288]   0   byte
    [289]   0   byte
    [290]   0   byte
    [291]   0   byte
    [292]   0   byte
    [293]   0   byte
    [294]   0   byte
    [295]   0   byte
    [296]   133 byte
    [297]   231 byte
    [298]   233 byte
    [299]   62  byte
    [300]   0   byte
    [301]   0   byte
    [302]   0   byte
    [303]   0   byte
    [304]   0   byte
    [305]   0   byte
    [306]   0   byte
    [307]   0   byte
    [308]   0   byte
    [309]   0   byte
    [310]   0   byte
    [311]   0   byte
    [312]   165 byte
    [313]   129 byte
    [314]   245 byte
    [315]   62  byte
    [316]   0   byte
    [317]   0   byte
    [318]   0   byte
    [319]   0   byte
    [320]   0   byte
    [321]   0   byte
    [322]   0   byte
    [323]   0   byte
    [324]   0   byte
    [325]   0   byte
    [326]   0   byte
    [327]   0   byte
    [328]   56  byte
    [329]   121 byte
    [330]   0   byte
    [331]   63  byte
    [332]   0   byte
    [333]   0   byte
    [334]   0   byte
    [335]   0   byte
    [336]   0   byte
    [337]   0   byte
    [338]   0   byte
    [339]   0   byte
    [340]   0   byte
    [341]   0   byte
    [342]   0   byte
    [343]   0   byte
    [344]   253 byte
    [345]   27  byte
    [346]   6   byte
    [347]   63  byte
    [348]   0   byte
    [349]   0   byte
    [350]   0   byte
    [351]   0   byte
    [352]   0   byte
    [353]   0   byte
    [354]   0   byte
    [355]   0   byte
    [356]   0   byte
    [357]   0   byte
    [358]   0   byte
    [359]   0   byte
    [360]   45  byte
    [361]   168 byte
    [362]   11  byte
    [363]   63  byte
    [364]   0   byte
    [365]   0   byte
    [366]   0   byte
    [367]   0   byte
    [368]   0   byte
    [369]   0   byte
    [370]   0   byte
    [371]   0   byte
    [372]   0   byte
    [373]   0   byte
    [374]   0   byte
    [375]   0   byte
    [376]   218 byte
    [377]   28  byte
    [378]   17  byte
    [379]   63  byte
    [380]   0   byte
    [381]   0   byte
    [382]   0   byte
    [383]   0   byte
    [384]   0   byte
    [385]   0   byte
    [386]   0   byte
    [387]   0   byte
    [388]   0   byte
    [389]   0   byte
    [390]   0   byte
    [391]   0   byte
    [392]   24  byte
    [393]   121 byte
    [394]   22  byte
    [395]   63  byte
    [396]   0   byte
    [397]   0   byte
    [398]   0   byte
    [399]   0   byte
    [400]   0   byte
    [401]   0   byte
    [402]   0   byte
    [403]   0   byte
    [404]   0   byte
    [405]   0   byte
    [406]   0   byte
    [407]   0   byte
    [408]   1   byte
    [409]   188 byte
    [410]   27  byte
    [411]   63  byte
    [412]   0   byte
    [413]   0   byte
    [414]   0   byte
    [415]   0   byte
    [416]   0   byte
    [417]   0   byte
    [418]   0   byte
    [419]   0   byte
    [420]   0   byte
    [421]   0   byte
    [422]   0   byte
    [423]   0   byte
    [424]   178 byte
    [425]   228 byte
    [426]   32  byte
    [427]   63  byte
    [428]   0   byte
    [429]   0   byte
    [430]   0   byte
    [431]   0   byte
    [432]   0   byte
    [433]   0   byte
    [434]   0   byte
    [435]   0   byte
    [436]   0   byte
    [437]   0   byte
    [438]   0   byte
    [439]   0   byte
    [440]   76  byte
    [441]   242 byte
    [442]   37  byte
    [443]   63  byte
    [444]   0   byte
    [445]   0   byte
    [446]   0   byte
    [447]   0   byte
    [448]   0   byte
    [449]   0   byte
    [450]   0   byte
    [451]   0   byte
    [452]   0   byte
    [453]   0   byte
    [454]   0   byte
    [455]   0   byte
    [456]   245 byte
    [457]   227 byte
    [458]   42  byte
    [459]   63  byte
    [460]   0   byte
    [461]   0   byte
    [462]   0   byte
    [463]   0   byte
    [464]   0   byte
    [465]   0   byte
    [466]   0   byte
    [467]   0   byte
    [468]   0   byte
    [469]   0   byte
    [470]   0   byte
    [471]   0   byte
    [472]   218 byte
    [473]   184 byte
    [474]   47  byte
    [475]   63  byte
    [476]   0   byte
    [477]   0   byte
    [478]   0   byte
    [479]   0   byte
    [480]   0   byte
    [481]   0   byte
    [482]   0   byte
    [483]   0   byte
    [484]   0   byte
    [485]   0   byte
    [486]   0   byte
    [487]   0   byte
    [488]   40  byte
    [489]   112 byte
    [490]   52  byte
    [491]   63  byte
    [492]   0   byte
    [493]   0   byte
    [494]   0   byte
    [495]   0   byte
    [496]   0   byte
    [497]   0   byte
    [498]   0   byte
    [499]   0   byte
    [500]   0   byte
    [501]   0   byte
    [502]   0   byte
    [503]   0   byte
    [504]   22  byte
    [505]   9   byte
    [506]   57  byte
    [507]   63  byte
    [508]   0   byte
    [509]   0   byte
    [510]   0   byte
    [511]   0   byte
    [512]   0   byte
    [513]   0   byte
    [514]   0   byte
    [515]   0   byte
    [516]   0   byte
    [517]   0   byte
    [518]   0   byte
    [519]   0   byte
    [520]   220 byte
    [521]   130 byte
    [522]   61  byte
    [523]   63  byte
    [524]   0   byte
    [525]   0   byte
    [526]   0   byte
    [527]   0   byte
    [528]   0   byte
    [529]   0   byte
    [530]   0   byte
    [531]   0   byte
    [532]   0   byte
    [533]   0   byte
    [534]   0   byte
    [535]   0   byte
    [536]   186 byte
    [537]   220 byte
    [538]   65  byte
    [539]   63  byte
    [540]   0   byte
    [541]   0   byte
    [542]   0   byte
    [543]   0   byte
    [544]   0   byte
    [545]   0   byte
    [546]   0   byte
    [547]   0   byte
    [548]   0   byte
    [549]   0   byte
    [550]   0   byte
    [551]   0   byte
    [552]   245 byte
    [553]   21  byte
    [554]   70  byte
    [555]   63  byte
    [556]   0   byte
    [557]   0   byte
    [558]   0   byte
    [559]   0   byte
    [560]   0   byte
    [561]   0   byte
    [562]   0   byte
    [563]   0   byte
    [564]   0   byte
    [565]   0   byte
    [566]   0   byte
    [567]   0   byte
    [568]   214 byte
    [569]   45  byte
    [570]   74  byte
    [571]   63  byte
    [572]   0   byte
    [573]   0   byte
    [574]   0   byte
    [575]   0   byte
    [576]   0   byte
    [577]   0   byte
    [578]   0   byte
    [579]   0   byte
    [580]   0   byte
    [581]   0   byte
    [582]   0   byte
    [583]   0   byte
    [584]   173 byte
    [585]   35  byte
    [586]   78  byte
    [587]   63  byte
    [588]   0   byte
    [589]   0   byte
    [590]   0   byte
    [591]   0   byte
    [592]   0   byte
    [593]   0   byte
    [594]   0   byte
    [595]   0   byte
    [596]   0   byte
    [597]   0   byte
    [598]   0   byte
    [599]   0   byte
    [600]   207 byte
    [601]   246 byte
    [602]   81  byte
    [603]   63  byte
    [604]   0   byte
    [605]   0   byte
    [606]   0   byte
    [607]   0   byte
    [608]   0   byte
    [609]   0   byte
    [610]   0   byte
    [611]   0   byte
    [612]   0   byte
    [613]   0   byte
    [614]   0   byte
    [615]   0   byte
    [616]   152 byte
    [617]   166 byte
    [618]   85  byte
    [619]   63  byte
    [620]   0   byte
    [621]   0   byte
    [622]   0   byte
    [623]   0   byte
    [624]   0   byte
    [625]   0   byte
    [626]   0   byte
    [627]   0   byte
    [628]   0   byte
    [629]   0   byte
    [630]   0   byte
    [631]   0   byte
    [632]   104 byte
    [633]   50  byte
    [634]   89  byte
    [635]   63  byte
    [636]   0   byte
    [637]   0   byte
    [638]   0   byte
    [639]   0   byte
    [640]   0   byte
    [641]   0   byte
    [642]   0   byte
    [643]   0   byte
    [644]   0   byte
    [645]   0   byte
    [646]   0   byte
    [647]   0   byte
    [648]   1

New Post: Right ear is still playing

$
0
0
maybe is the way I am playing it ?
 void Play(double Amp, double Left, double Right)
        {
            BBeats = new binaural_beats();
            BBeats.Amplitude = Amp;
            BBeats.Amplitude2 = Amp;
            BBeats.Frequency = Left;
            BBeats.Frequency2 = Right;

            BBeats.Bufferlength = 44100 * 2 * 3;// will play for 3 sec

            waveout = new WaveOut();
            WaveChannel32 temp = new WaveChannel32(BBeats);
            temp.PadWithZeroes = false;
            waveout.Init(temp);
            waveout.Play();

        }

New Post: Right ear is still playing


New Post: two problems with wave playack

$
0
0
I record speech synthesis to wave file.
                            reader = new SpeechSynthesizer();
                            reader.SelectVoice(AppSettings.VoiceName);
                            reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(OnReaderSpeakCompleted);
                            reader.SpeakStarted += new EventHandler<SpeakStartedEventArgs>(OnReaderSpeekStarted);
                            reader.StateChanged += new EventHandler<StateChangedEventArgs>(OnReaderStateChanged);

                            reader.SetOutputToWaveFile(m_waveFile,
                                new System.Speech.AudioFormat.SpeechAudioFormatInfo(44100, AudioBitsPerSample.Eight, AudioChannel.Mono)
                            );

                            reader.Speak(strMessage);

                            reader.SpeakAsync(WeatherMessage);

                            reader.Dispose();
                            reader = null;
How can I add fade before and after playback?

New Post: 'NAudio.MmException

$
0
0
what is a NAudio.MmException ? and how do I fix it ?

New Post: PianoRoll

$
0
0
There doesn't seem to be much activity on this discussion board, but I'll go ahead and post my code for the Piano Roll here.
I tried to remove elements specific to other parts of my project so that the Piano Roll can be used as is, so I hope I adjusted it correctly... (Also, the C# is translated from VB. One of these days I'll try to get used to curly brackets but that time as not yet arrived for me :)

First, the XAML for the GUI:
<UserControl x:Class="PianoRoll"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="133" d:DesignWidth="413" x:Name="PRoll" >
        
    <Grid>
            
                <Border BorderThickness="4" BorderBrush="#FF5885AA" CornerRadius="4" Background="#FFF7F0F0">
            <DockPanel>
                <DockPanel x:Name="TopDB" DockPanel.Dock="Top" Height="20">
                    <Line HorizontalAlignment="Stretch" DockPanel.Dock="Bottom" Stroke="Black" StrokeThickness="2" X2="{Binding ElementName=TopDB, Path=ActualWidth}"></Line>
                    <StackPanel Width ="70">
                        <ComboBox x:Name="SnapToCB" Margin="0, 0, 0, 2" SelectedValuePath="Content">
                            <ComboBoxItem Content="Snap" IsSelected="True" Tag="No snap"/>
                            <ComboBoxItem Content="1/2 note" Tag="Snap to 1/2"/>
                            <ComboBoxItem Content="1/4 note" Tag="Snap to 1/4"/>
                            <ComboBoxItem Content="1/8 note" Tag="Snap to 1/8"/>
                            <ComboBoxItem Content="1/16 note" Tag="Snap to 1/16"/>
                        </ComboBox>
                    </StackPanel>
                    <StackPanel DockPanel.Dock="Right" Background="{DynamicResource {x:Static SystemColors.ControlColor}}" Width="{x:Static SystemParameters.VerticalScrollBarWidth}"></StackPanel>
                    <ScrollViewer x:Name="ScaleScrollViewer" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Disabled">
                        <DockPanel>
                            <Canvas x:Name="ScaleCanvas" Width="{Binding ElementName=NoteCanvas, Path=ActualWidth}" Background="#FFFFE6C9"></Canvas>
                        <Canvas x:Name="RightFillScaleCanvas"></Canvas>
                        </DockPanel>
                    </ScrollViewer>
                  </DockPanel>
                
                <DockPanel x:Name="ScrollBarDB" DockPanel.Dock="Bottom" Height="15">
                    <Label x:Name="RepLabel" Margin="0, -3, 0, 0" FontSize="7" DockPanel.Dock="Left" Width="{Binding ElementName=PianoStackPanel, Path=ActualWidth}" Background="#FFC7D6CD"></Label>
                    <ScrollBar x:Name="RLScrollBar" Orientation="Horizontal" Background="#FFD4CCE6"/>
                </DockPanel>
                <ScrollViewer x:Name="UDScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                    <DockPanel x:Name="MainDockPanel" HorizontalAlignment="Stretch">
                        <DockPanel x:Name="PianoDockPanel"  DockPanel.Dock="Left" Width="70" LastChildFill="False">
                            <DockPanel.Background>
                                <ImageBrush ImageSource="pack://siteoforigin:,,,/CustomControls/Resources/PianoLong.bmp"/>
                            </DockPanel.Background>
                            <Line DockPanel.Dock="Right"  Y1="0" Y2="{Binding ElementName=PianoStackPanel, Path=ActualHeight}" X1="0" X2="0" Stroke="Black" StrokeThickness="1">
                                <Line.Fill>
                                    <ImageBrush/>
                                </Line.Fill>
                            </Line>
                            <Canvas x:Name="NoteNameCanvas"></Canvas>
                        </DockPanel>
                        <ScrollViewer x:Name="RLScrollViewer" HorizontalScrollBarVisibility="Hidden"  Height="{Binding ElementName=GridCanvas, Path=ActualHeight}" VerticalScrollBarVisibility="Auto" Background="Cornsilk">
                            
                                <Canvas x:Name="HolderCanvas" VerticalAlignment="Top" HorizontalAlignment="Left"  >

                                <AdornerDecorator Height="{Binding ElementName=NoteCanvas, Path=ActualHeight}" Width="{Binding ElementName=NoteCanvas, Path=ActualWidth}" HorizontalAlignment="Left" VerticalAlignment="Top"  x:Name="MeasBufAdornerLayer" Visibility="Visible" >
                                    <Grid>
                                        <Grid.Background>
                                            <LinearGradientBrush  EndPoint="1,0.5" StartPoint="0,0.5" >
                                                <GradientStop Color="#00000000" Offset="0"/>
                                                <GradientStop x:Name="MeasBuf1" Color="#00000000" Offset="1"/>
                                                <GradientStop x:Name="MeasBuf2" Color="#917CEE6A" Offset="1"/>
                                                <GradientStop Color="#917CEE6A" Offset="1"/>
                                            </LinearGradientBrush>
                                        </Grid.Background>
                                    </Grid>
                                </AdornerDecorator>

                                <Canvas x:Name="GridCanvas" Background="Transparent" Top="0" Left="0" Height="{Binding ActualHeight, ElementName=NoteCanvas}" >
                                    
                                </Canvas>
                                <Canvas x:Name="NoteCanvas" Left="0" Top="0" Focusable="True" Height="{Binding ElementName=GridCanvas, Path=ActualHeight}" >
                                    <Canvas.CacheMode>
                                        <BitmapCache EnableClearType="False"  RenderAtScale="1" SnapsToDevicePixels="True" />
                                    </Canvas.CacheMode>
                                </Canvas>
                                <Canvas x:Name="RightFillNoteCanvas"></Canvas>

                            </Canvas>
                        </ScrollViewer>
                          
                </DockPanel>
            </ScrollViewer>
            </DockPanel>
        </Border>
    </Grid>
</UserControl>
This Piano Roll is associated with one track (list of MidiEvents) from a MidiEventCollection. I did this because it seems most logical (having all note from all tracks on the same Piano Roll is huge visual mess). So, you would create a new instance of PianoRoll for each track when you open a MidiFile. (However, you have to set the DetaTicks per quarter note for each instance of PianoRoll.)
Also, some extra things I've implemented for this Piano Roll is the ability to move notes with the mouse,
a snap-to feature (1/2, 1/4, 1/8, 1/16 note or measure), from a selected from a ComboBox. Or no snap to), zooming and a numbered grid.
There are three layers of Canvases, the "Holder Canvas", the size of which is used to determine whether or not the scrollbars should be visible.
Also, a "GridCanvas" and "NoteCanvas" on top of this. This setup was necessary for zooming, which is easy to do in WPF but not without caveats.
The problem with just applying a ScaleTransform to the control is that things appear ugly as you increase the scale. I think WPF just takes a visual snapshot of the control and renders a larger image of it, which means lines get thicker, etc.. It just doesn't look nice. So you have to reset the StrokeThickness of lines within your layers to the inverse (1/ScaleTransform). This is all done in the code behind which is posted below.

New Post: PianoRoll

$
0
0
Below I'll also post the keyboard image I used as the background image for PianoDockPanel.
You could make your own image, or use the one I use. It's one long bitmap with all 128 keys shown.

Also in the above code, it might be wondered why I had to implement an extra horizontal scrollbar and disabled the one in the scrollviewer. It's because of the asymmetrical way the scrolling has to be done.
That is, horizontal scrolling of the note grid has to also scroll the top grid (which shows the beat numbers), but not the PianoDockPanel. Conversely, vertical scrolling of the note grid has to vertical scroll the PianoDockPanel, but not the top grid. So, I chose to put one of the scroll bars outside of the scrollviewer to make it simpler.

New Post: PianoRoll

$
0
0
Here is the code behind (Part I):
``` using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using NAudio.Midi;

public class PianoRoll : UserControl
{
private double xScale = 1.0 / 10;
private double yScale = 10;
private LinearGradientBrush NoteBrush = new LinearGradientBrush();

private LinearGradientBrush SelectedNoteBrush = new LinearGradientBrush();

public PianoRoll()
{
    // This call is required by the designer.
    InitializeComponent();

    if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)) {
        return;
    }

    DrawHorizontalLines();

    Canvas.SetZIndex(GridCanvas, 2);
    //Bring grid canvas above holder canvas
    Canvas.SetZIndex(NoteCanvas, 3);
    //Bring note canvas to top

    //Define brushes (could be moved to Xaml designer)
    NoteBrush.StartPoint = new Point(0, 0);
    NoteBrush.EndPoint = new Point(0, 1);
    NoteBrush.GradientStops.Add(new GradientStop(Colors.White, 0));
    NoteBrush.GradientStops.Add(new GradientStop(Colors.CadetBlue, 1));
    SelectedNoteBrush.StartPoint = new Point(0, 0);
    SelectedNoteBrush.EndPoint = new Point(0, 1);
    SelectedNoteBrush.GradientStops.Add(new GradientStop(Colors.White, 0));
    SelectedNoteBrush.GradientStops.Add(new GradientStop(Colors.Red, 1));


}

private void DrawHorizontalLines()
{
    int GridHeight = yScale * 128;

    for (int noteno = 0; noteno <= 127; noteno++) {
        switch (true) {

            case noteno % 12 == 1:
            case noteno % 12 == 3:
            case noteno % 12 == 6:
            case noteno % 12 == 8:
            case noteno % 12 == 10:
                //C#, Eb, F#, Ab, Bb
                //Black key lines
                Rectangle blackkeyrect = new Rectangle();
                blackkeyrect.Fill = new SolidColorBrush(Color.FromArgb(100, 180, 180, 180));
                blackkeyrect.SetValue(Canvas.TopProperty, (GridHeight - yScale) - (noteno * yScale));
                blackkeyrect.Stroke = Brushes.Black;
                blackkeyrect.Margin = new Thickness(-1, 0, 0, 0);
                BindingOperations.SetBinding(blackkeyrect, Rectangle.WidthProperty, new Binding {
                    Source = HolderCanvas,
                    Path = new PropertyPath("ActualWidth")
                });
                blackkeyrect.Height = yScale;
                HolderCanvas.Children.Add(blackkeyrect);

                break;
            default:
                //White key lines
                Line newline = new Line();
                newline.Stroke = Brushes.Black;
                newline.StrokeThickness = 1;
                newline.X1 = 0;
                BindingOperations.SetBinding(newline, Line.X2Property, new Binding {
                    Source = HolderCanvas,
                    Path = new PropertyPath("ActualWidth")
                });
                newline.Y1 = 0;
                newline.Y2 = 0;
                newline.SetValue(Canvas.TopProperty, (GridHeight - yScale) - (noteno * yScale));
                HolderCanvas.Children.Add(newline);

                break;
        }
        GridCanvas.Height = GridHeight;

    }


}
private SolidColorBrush measureSeparatorBrush = new SolidColorBrush(Colors.Black);
private SolidColorBrush beatSeparatorBrush = new SolidColorBrush(Colors.LightGray);
private int lastPosition;

private int GridDisplayWidth;

private void DrawVerticalGrid()
{
    GridCanvas.Children.Clear();
    int beat = 0;
    long n = 0;
    while (n <= GridDisplayWidth) {
        Line line = new Line();
        Line line2 = new Line();
        TextBlock nTB = new TextBlock();
        line.X1 = n * xScale;
        line.X2 = line.X1;
        line.Y1 = 0;
        line.Y2 = 128 * yScale;
        line2.X1 = n * xScale;
        line2.X2 = line2.X1;
        line2.Y1 = 0;
        line2.Y2 = ScaleCanvas.ActualHeight;
        if (beat % 4 == 0) {
            line.Stroke = measureSeparatorBrush;
            line2.Stroke = measureSeparatorBrush;
            nTB.Margin = new Thickness(line2.X1, 0, 0, 0);
            nTB.Text = (Conversion.Int(Conversion.Int(n / DeltaTicks) / 4) + 1).ToString;
        } else {
            line.Stroke = beatSeparatorBrush;
            line2.Stroke = beatSeparatorBrush;
        }
        GridCanvas.Children.Add(line);
        ScaleCanvas.Children.Add(line2);
        ScaleCanvas.Children.Add(nTB);
        beat += 1;
        n += DeltaTicks;
    }


}


private void DrawNotes()
{
    NoteCanvas.Children.Clear();

    foreach (MidiEvent midiEvent in MidiEvents) {
        if (midiEvent.CommandCode == MidiCommandCode.NoteOn) {
            NoteOnEvent NoteOn = (NoteOnEvent)midiEvent;
            if (NoteOn.OffEvent != null) {
                Path rectpath = GetNoteRectPath(NoteOn.NoteNumber, NoteOn.AbsoluteTime, NoteOn.NoteLength, ref NoteOn);
                NoteHighPt = Math.Max(NoteHighPt, NoteOn.NoteNumber);
                NoteLowPt = Math.Min(NoteLowPt, NoteOn.NoteNumber);
                rectpath.MouseDown += MouseDownInNote;
                rectpath.MouseUp += MouseUpInNote;
                rectpath.MouseEnter += MouseEnterNote;
                rectpath.MouseLeave += MouseLeaveNote;
                rectpath.MouseMove += MouseMoveNote;
                NoteCanvas.Children.Add(rectpath);
            }
        }
    }


}

private Path GetNoteRectPath(int noteNumber, long startTime, int duration, ref NoteOnEvent CustNoteOnEv)
{

    Path rpath = new Path();
    RectangleGeometry rectgeom = new RectangleGeometry();

    rectgeom.Rect = new Rect(Convert.ToDouble(CustNoteOnEv.AbsoluteTime) * xScale, Convert.ToDouble(127 - CustNoteOnEv.NoteNumber) * yScale + 1, Convert.ToDouble(CustNoteOnEv.NoteLength) * xScale, (yScale - 1));

    rectgeom.RadiusX = 1;
    rectgeom.RadiusY = 1;
    rpath.Stroke = RectBorderBrush;
    rpath.StrokeThickness = 1;
    rpath.Margin = new Thickness(0, 0, 0, 0);
    rpath.Fill = NoteBrush;
    rpath.Data = rectgeom;

    return rpath;

}

SolidColorBrush RectBorderBrush = new SolidColorBrush(Colors.DarkBlue);
public int NoteHighPt = 0;
public int NoteLowPt = 127;
int InitialWidth;
public DependencyProperty MidiEventsDP { get; set; }
public DependencyProperty DeltaTicksDP { get; set; }

public void UpdatePianoRoll()
{
    //Get Last position (for grid width)
    lastPosition = 0;
    foreach (MidiEvent midiEvent in MidiEvents) {
        if (midiEvent.CommandCode == MidiCommandCode.NoteOn) {
            NoteOnEvent noteOn = (NoteOnEvent)midiEvent;
            if (noteOn.OffEvent != null) {
                lastPosition = Math.Max(lastPosition, noteOn.AbsoluteTime + noteOn.NoteLength);
            }
        }
    }

    // a quarter note is 20 units wide
    xScale = (20.0 / DeltaTicks);

    //Add an extra measure at right as visual buffer
    GridDisplayWidth = (lastPosition + (DeltaTicks * 4) - (lastPosition % (DeltaTicks * 4))) + (4 * DeltaTicks);
    InitialWidth = GridDisplayWidth * xScale;

    NoteCanvas.Width = InitialWidth;

    DrawVerticalGrid();

    DrawNotes();

    HolderCanvas.Width = Math.Max(InitialWidth, RLScrollViewer.ActualWidth);
    GridCanvas.Width = HolderCanvas.ActualWidth;
    ScaleCanvas.Width = HolderCanvas.ActualWidth;
    MeasBufAdornerLayer.Width = HolderCanvas.ActualWidth;

    //Linear gradient brush settings for non-editable area
    MeasBuf1.Offset = ((NoteCanvas.ActualWidth - (4 * 20)) / HolderCanvas.ActualWidth);
    MeasBuf2.Offset = MeasBuf1.Offset;

    ScaleFactorX = RLScrollViewer.ActualWidth / InitialWidth;
    ScalePianoRoll();

    UDScrollViewer.ScrollToVerticalOffset(((NoteLowPt + NoteHighPt) / 2) * yScale - UDScrollViewer.ActualHeight / 2);
    //Scroll to midrange area
    RLScrollViewer.ScrollToHorizontalOffset(0);

}
public int DeltaTicks {
    get { return GetValue(DeltaTicksDP); }
    set { SetValue(DeltaTicksDP, value); }
}
public List<MidiEvent> MidiEvents {
    get { return GetValue(MidiEventsDP); }
    set {
        SetValue(MidiEventsDP, value);
        UpdatePianoRoll();
    }
}


private void RLScrollBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    int scrollval = e.NewValue * ((InitialWidth * ScaleFactorX) - RLScrollViewer.ActualWidth);
    RLScrollViewer.ScrollToHorizontalOffset(scrollval);
    ScaleScrollViewer.ScrollToHorizontalOffset(scrollval);

}
private void NoteCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
{
    UpdateHorizontalScrollBar();
}
private void UpdateHorizontalScrollBar()
{
    RLScrollViewer.UpdateLayout();
    if (HolderCanvas.ActualWidth == 0)
        return;

    decimal p = (RLScrollViewer.ActualWidth) / (InitialWidth * ScaleFactorX);
    if (p >= 1) {
        ScrollBarDB.Visibility = Windows.Visibility.Collapsed;
    } else {
        ScrollBarDB.Visibility = Windows.Visibility.Visible;
        RLScrollBar.ViewportSize = (RLScrollBar.Maximum - RLScrollBar.Minimum) * p / (1 - p);
    }
    RLScrollBar.Value += 0.01;
    RLScrollBar.Value -= 0.01;
    //trigger

}
private double ScaleFactorX = 1;

private void ScalePianoRoll()
{
    HolderCanvas.LayoutTransform = new ScaleTransform(ScaleFactorX, 1);
    //includes GridCanvas & NoteCanvas
    ScaleCanvas.LayoutTransform = new ScaleTransform(ScaleFactorX, 1);
    NoteCanvas.LayoutTransform = new ScaleTransform(1 / ScaleFactorX, 1);
    //Don't scale up the note canvas - only geometries in it

    //Adjust grid line thicknesses
    foreach (UIElement uielm in GridCanvas.Children) {
        if (uielm.GetType() == typeof(Line)) {
            ((Line)uielm).StrokeThickness = 1 / ScaleFactorX;
        } else {
            uielm.RenderTransform = new ScaleTransform(1 / ScaleFactorX, 1);
        }
    }
    //Adjust note sizes
    foreach (UIElement uielm in NoteCanvas.Children) {
        if (uielm.GetType() == typeof(Path)) {
            Path rpath = (Path)uielm;
            rpath.Data.Transform = new ScaleTransform(ScaleFactorX, 1);
        }
    }
    //Adjust scale line thicknesses
    foreach (UIElement uielm in ScaleCanvas.Children) {
        if (uielm.GetType() == typeof(Line)) {
            ((Line)uielm).StrokeThickness = 1 / ScaleFactorX;
        } else {
            uielm.RenderTransform = new ScaleTransform(1 / ScaleFactorX, 1);
        }
    }

    MeasBuf1.Offset = ((NoteCanvas.ActualWidth - (4 * 20)) / HolderCanvas.ActualWidth);
    MeasBuf2.Offset = MeasBuf1.Offset;

    //Add extra measures to fill space at right if smaller than the control
    if (GridDisplayWidth * xScale < RLScrollViewer.ActualWidth / ScaleFactorX) {
        GridCanvas.Width = (RLScrollViewer.ActualWidth / ScaleFactorX);
        GridDisplayWidth = (RLScrollViewer.ActualWidth / ScaleFactorX) / xScale;
        DrawVerticalGrid();
    }

    UpdateHorizontalScrollBar();

}

```
Viewing all 5831 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>