Csound Csound-dev Csound-tekno Search About

[Cs-dev] tuning Jack server and external Midi callback

Date2011-06-09 13:04
FromDurga
Subject[Cs-dev] tuning Jack server and external Midi callback
Dear Csound community

I tinkered a dirty implementation of the csoundSetExternalMidiRead Callback:
	int midi_in_read(CSOUND * csound, void *userData, unsigned char *buf, int
nBytes) {
		int i;
		int bytes = 0;
		ProjData * pFP = (ProjData *) userData;	
		if(userData)
		{
			if(pFP->buf_fill > 0)
			{
				for(i=0; i < pFP->buf_fill; i++)
				{
					buf[i] = pFP->cs_midi_buf[i];
					bytes++;
				}
			}
		}
		return bytes;
	}

pFP->cs_midi_buf is filled by the Jack process callback which is called each
Jack cycle.
pFP->buf_fill is the amount of bytes written by the Jack process callback. 

I use a Jack buffer size of 1024 frames / period
ksmps = 256
csound software buffer size (-b) = -4
So the Csound software buffer is also 1024 and synced with kr.

I get a satisfying real time response to my keyboard strokes but
unfortunately Csound outputs around 30:
new alloc for instr 1:
WARNING: MIDI note overlaps with key 71 on same channel
messages.

If I reduce the Jack buffer size to 64 (of course I also have to adjust
ksmps to =< 64) I can reduce these messegas to around 3 each time I press a
key on my keyboard. 

I dont understand this behavior. In the Csound Book CD chapter 2 “Real-time
Synthesis in Csound with MIDI Control” I've learned “Csound looks to see if
there are any new MIDI messages once per k-period.”. 

So for a -b -4 value I expect 4  “WARNING: MIDI note overlaps with key 71 on
same channel” messages.

Since I am pretty new to C programming I might have forgotten something
fundamental. Do I have to lock the pFP->cs_midi_buf array while Jack is
writing  / Csound is reading?

Any hints are appreciated, please tell me if you need more information about
my code / setup.
Thank you!
Durga

--
View this message in context: http://csound.1045644.n5.nabble.com/tuning-Jack-server-and-external-Midi-callback-tp4472479p4472479.html
Sent from the Csound - Dev mailing list archive at Nabble.com.

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net
https://lists.sourceforge.net/li

Date2011-06-09 14:09
FromVictor Lazzarini
SubjectRe: [Cs-dev] tuning Jack server and external Midi callback
Well , if you are using a single buffer, I guess you should lock it,  
since the two threads might try to change it at the same time.
Another solution is to use a double buffer, so each thread is reading/ 
writing to a different half of the buffer at a time. I suggest you
have a look at the code in rtwinmm.c, which uses a callback for MIDI  
input (Windows MME lib). You could adapt it to your needs,
it uses locks and a circular buffer.

Note that 256 is far too high ksmps for any RT use. I should try to  
keep below 64. The default is 10, so you could try
16, if you want to use power of two sizes.

Victor

On 9 Jun 2011, at 13:04, Durga wrote:

> Dear Csound community
>
> I tinkered a dirty implementation of the csoundSetExternalMidiRead  
> Callback:
> 	int midi_in_read(CSOUND * csound, void *userData, unsigned char  
> *buf, int
> nBytes) {
> 		int i;
> 		int bytes = 0;
> 		ProjData * pFP = (ProjData *) userData;	
> 		if(userData)
> 		{
> 			if(pFP->buf_fill > 0)
> 			{
> 				for(i=0; i < pFP->buf_fill; i++)
> 				{
> 					buf[i] = pFP->cs_midi_buf[i];
> 					bytes++;
> 				}
> 			}
> 		}
> 		return bytes;
> 	}
>
> pFP->cs_midi_buf is filled by the Jack process callback which is  
> called each
> Jack cycle.
> pFP->buf_fill is the amount of bytes written by the Jack process  
> callback.
>
> I use a Jack buffer size of 1024 frames / period
> ksmps = 256
> csound software buffer size (-b) = -4
> So the Csound software buffer is also 1024 and synced with kr.
>
> I get a satisfying real time response to my keyboard strokes but
> unfortunately Csound outputs around 30:
> new alloc for instr 1:
> WARNING: MIDI note overlaps with key 71 on same channel
> messages.
>
> If I reduce the Jack buffer size to 64 (of course I also have to  
> adjust
> ksmps to =< 64) I can reduce these messegas to around 3 each time I  
> press a
> key on my keyboard.
>
> I dont understand this behavior. In the Csound Book CD chapter 2  
> “Real-time
> Synthesis in Csound with MIDI Control” I've learned “Csound looks to  
> see if
> there are any new MIDI messages once per k-period.”.
>
> So for a -b -4 value I expect 4  “WARNING: MIDI note overlaps with  
> key 71 on
> same channel” messages.
>
> Since I am pretty new to C programming I might have forgotten  
> something
> fundamental. Do I have to lock the pFP->cs_midi_buf array while Jack  
> is
> writing  / Csound is reading?
>
> Any hints are appreciated, please tell me if you need more  
> information about
> my code / setup.
> Thank you!
> Durga
>
> --
> View this message in context: http://csound.1045644.n5.nabble.com/tuning-Jack-server-and-external-Midi-callback-tp4472479p4472479.html
> Sent from the Csound - Dev mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Csound-devel mailing list
> Csound-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/csound-devel

Dr Victor Lazzarini
Senior Lecturer
Dept. of Music
NUI Maynooth Ireland
tel.: +353 1 708 3545
Victor dot Lazzarini AT nuim dot ie




------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net

Date2011-06-09 15:49
FromDurga
SubjectRe: [Cs-dev] tuning Jack server and external Midi callback
AttachmentsNone  None  
Victor, thanks a lot for your quick answer! Actually the Jack thread is only writing while the Csound thread is only reading. Thats why I didnt see the need of locking the thread. I am just looking for an explanation for the "WARNING: MIDI note overlaps with key 71 on same channel" messages.

For sure I will check rtwinmm.c and reduce ksmps as soon as I am back home, thank you for the hints!

Durga


2011/6/9 Victor Lazzarini [via Csound] <[hidden email]>
Well , if you are using a single buffer, I guess you should lock it,  
since the two threads might try to change it at the same time.
Another solution is to use a double buffer, so each thread is reading/
writing to a different half of the buffer at a time. I suggest you
have a look at the code in rtwinmm.c, which uses a callback for MIDI  
input (Windows MME lib). You could adapt it to your needs,
it uses locks and a circular buffer.

Note that 256 is far too high ksmps for any RT use. I should try to  
keep below 64. The default is 10, so you could try
16, if you want to use power of two sizes.

Victor

On 9 Jun 2011, at 13:04, Durga wrote:

> Dear Csound community
>
> I tinkered a dirty implementation of the csoundSetExternalMidiRead  
> Callback:
> int midi_in_read(CSOUND * csound, void *userData, unsigned char  
> *buf, int
> nBytes) {
> int i;
> int bytes = 0;
> ProjData * pFP = (ProjData *) userData;
> if(userData)
> {
> if(pFP->buf_fill > 0)
> {
> for(i=0; i < pFP->buf_fill; i++)
> {
> buf[i] = pFP->cs_midi_buf[i];
> bytes++;
> }
> }
> }
> return bytes;
> }
>
> pFP->cs_midi_buf is filled by the Jack process callback which is  
> called each
> Jack cycle.
> pFP->buf_fill is the amount of bytes written by the Jack process  
> callback.
>
> I use a Jack buffer size of 1024 frames / period
> ksmps = 256
> csound software buffer size (-b) = -4
> So the Csound software buffer is also 1024 and synced with kr.
>
> I get a satisfying real time response to my keyboard strokes but
> unfortunately Csound outputs around 30:
> new alloc for instr 1:
> WARNING: MIDI note overlaps with key 71 on same channel
> messages.
>
> If I reduce the Jack buffer size to 64 (of course I also have to  
> adjust
> ksmps to =< 64) I can reduce these messegas to around 3 each time I  
> press a
> key on my keyboard.
>
> I dont understand this behavior. In the Csound Book CD chapter 2  
> “Real-time
> Synthesis in Csound with MIDI Control” I've learned “Csound looks to  
> see if
> there are any new MIDI messages once per k-period.”.
>
> So for a -b -4 value I expect 4  “WARNING: MIDI note overlaps with  
> key 71 on
> same channel” messages.
>
> Since I am pretty new to C programming I might have forgotten  
> something
> fundamental. Do I have to lock the pFP->cs_midi_buf array while Jack  
> is
> writing  / Csound is reading?
>
> Any hints are appreciated, please tell me if you need more  
> information about
> my code / setup.
> Thank you!
> Durga
>
> --
> View this message in context: http://csound.1045644.n5.nabble.com/tuning-Jack-server-and-external-Midi-callback-tp4472479p4472479.html
> Sent from the Csound - Dev mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Csound-devel mailing list
> [hidden email]

Dr Victor Lazzarini
Senior Lecturer
Dept. of Music
NUI Maynooth Ireland
tel.: +353 1 708 3545
Victor dot Lazzarini AT nuim dot ie




------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Csound-devel mailing list
[hidden email]

If you reply to this email, your message will be added to the discussion below:
http://csound.1045644.n5.nabble.com/tuning-Jack-server-and-external-Midi-callback-tp4472479p4472637.html
To unsubscribe from tuning Jack server and external Midi callback, click here.



View this message in context: Re: tuning Jack server and external Midi callback
Sent from the Csound - Dev mailing list archive at Nabble.com.

Date2011-06-10 21:05
FromDurga
SubjectRe: [Cs-dev] tuning Jack server and external Midi callback
ksmps is 16 now. I still get the same amount of “WARNING: MIDI note overlaps
with key XX on same channel” messages (around 30).

The only way I can reduce these messages is to size down the Frames/Period
of Jack. Jack runs now with 256 Frames/Period (before 1024) and I get around
6 “WARNING: MIDI note overlaps with key XX on same channel” messages for
each key stroke.

Obviously Csound’s Midi callback gets called more frequently than Jack’s
process callback.
The question is now: How do I control the Csound Midi callback rate?

For the moment I added this line after the for loop to the code above to
filter the unwanted messages:
pFP->buf_fill = 0;
So Csound won’t read the buffer until it is filled again by Jack. 

Of course, this solution is not satisfying. I really would like to
understand the Csound call and run it at the same speed as the Jack
callback…..thanks for your help!
Durga


--
View this message in context: http://csound.1045644.n5.nabble.com/tuning-Jack-server-and-external-Midi-callback-tp4472479p4477154.html
Sent from the Csound - Dev mailing list archive at Nabble.com.

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Csound-devel mailing list
Csound-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/csound