|
Isn't this effectively a merge sort from N sources.
Each source is already sorted. I admit my ignorance in how you calculate your context.
But I assume that EITHER you have an incoming stream of N contexts pre-sorted (which would make outputting it more trivial as you output it in the order it arrives).
Or you have an incoming stream with N tracks, where each track is at a specific offset, but the magic is that while track 1 has a small delta, track 3 could have an excessively large delta, so it is played at the right time. And you might not hear from track 3 for some time.
I often find it helpful to imagine how they implemented the player at the hardware level.
For my take, I would write something that played only 1 of N contexts correctly...
Then I would look hard at how to implement simply adding a second context to that. Based on how the data shows up.
Because by the time you get to the third or fourth, I think you usually have a decent approach.
The other comparison I would make is a Multiplexer. is this similar to CDM or TDM (Code or Time division Multiplexing).
Another comparison is Stereo, where you get L+R and a 2L (I forget the actual), but taking -2L + L+R => -L+R + L+R = 2R
But they did it that way to take advantage of simpler hardware.
I remember learning from that example that coding stuff versus building components you have to think differently about what is easy/hard.
==
Finally, your problem reminds me of a Computer Engineering Class, where we built circuits that were run through a simulator. The simulator used a queue design, where "events" would trigger through the queue, and the simulator was able to be fast, because it ignored the timing signals, allowing it to "not wait" any time before processing an item. (I got in trouble in the class, because I wrote obscenely inefficient but SIMPLE code, reducing the homework to a TRIVIAL problem, avoiding the timing issues others were busy coding around).
anyways, I could envision a queue that is managing N queues of inputs, and only when you take off an item, do you go to the stream to pull in another item.
|
|
|
|
|
Kirk 10389821 wrote: Finally, your problem reminds me of a Computer Engineering Class, where we built circuits that were run through a simulator. The simulator used a queue design, where "events" would trigger through the queue, and the simulator was able to be fast, because it ignored the timing signals, allowing it to "not wait" any time before processing an item. (I got in trouble in the class, because I wrote obscenely inefficient but SIMPLE code, reducing the homework to a TRIVIAL problem, avoiding the timing issues others were busy coding around).
anyways, I could envision a queue that is managing N queues of inputs, and only when you take off an item, do you go to the stream to pull in another item.
You're describing the problem pretty well, which I actually solved last night. I won't paste the implementation here, but here is using it with a queue q
Forgive the grotty code. It's just test stuff I've been banging on
#ifndef ARDUINO
#include <sfx_midi_file.hpp>
#include <sfx_midi_clock.hpp>
#include <sfx_midi_utility.hpp>
#include <queue>
#include <stdio.h>
#include <math.h>
using namespace sfx;
void dump_midi(stream* stm, const midi_file& file) {
printf("Type: %d\nTimebase: %d\n",(int)file.type,(int)file.timebase);
printf("Tracks: %d\n",(int)file.tracks_size);
for(int i = 0;i<(int)file.tracks_size;++i) {
printf("\tOffset: %d, Size: %d, Preview: ",(int)file.tracks[i].offset,(int)file.tracks[i].size);
stm->seek(file.tracks[i].offset);
uint8_t buf[16];
size_t tsz = file.tracks[i].size;
size_t sz=stm->read(buf,tsz<16?tsz:16);
for(int j = 0;j<sz;++j) {
printf("%02x",(int)buf[j]);
}
printf("\n");
}
}
void dump_midi(const midi_message& msg) {
switch(msg.type()) {
case midi_message_type::note_off:
printf("Note Off: %d, %d\n",(int)msg.lsb(),(int)msg.msb());
break;
case midi_message_type::note_on:
printf("Note On: %d, %d\n",(int)msg.lsb(),(int)msg.msb());
break;
case midi_message_type::polyphonic_pressure:
printf("Poly pressure: %d, %d\n",(int)msg.lsb(),(int)msg.msb());
break;
case midi_message_type::control_change:
printf("Control change: %d, %d\n",(int)msg.lsb(),(int)msg.msb());
break;
case midi_message_type::pitch_wheel_change:
printf("Pitch wheel change: %d, %d\n",(int)msg.lsb(),(int)msg.msb());
break;
case midi_message_type::song_position:
printf("Song position: %d, %d\n",(int)msg.lsb(),(int)msg.msb());
break;
case midi_message_type::program_change:
printf("Program change: %d\n",(int)msg.value8);
break;
case midi_message_type::channel_pressure:
printf("Channel pressure: %d\n",(int)msg.value8);
break;
case midi_message_type::song_select:
printf("Song select: %d\n",(int)msg.value8);
break;
case midi_message_type::system_exclusive:
printf("Systex data: Size of %d\n",(int)msg.sysex.size);
break;
case midi_message_type::reset:
if(msg.meta.data==nullptr) {
printf("Reset\n");
} else {
int32_t result;
const uint8_t* p=midi_utility::decode_varlen(msg.meta.encoded_length,&result);
if(p!=nullptr) {
printf("Meta message: Type of %02x, Size of %d\n",(int)msg.meta.type, (int)result);
} else {
printf("Error reading message\n");
}
}
break;
case midi_message_type::end_system_exclusive:
printf("End sysex\n");
break;
case midi_message_type::active_sensing:
printf("Active sensing\n");
break;
case midi_message_type::start_playback:
printf("Start playback\n");
break;
case midi_message_type::stop_playback:
printf("Stop playback\n");
break;
case midi_message_type::tune_request:
printf("Tune request\n");
break;
case midi_message_type::timing_clock:
printf("Timing clock\n");
break;
default:
printf("Illegal message: %02x\n",(int)msg.status);
while(true);
}
}
using midi_queue = std::queue<midi_stream_event>;
int main(int argc, char** argv) {
midi_clock mclock;
static const char* def = "data\\sonata.mid";
const char* sz;
if(argc<2) {
sz = def;
} else {
sz=argv[1];
}
file_stream fstm(sz);
midi_file f;
midi_queue q;
sfx_result r=midi_file::read(&fstm,&f);
if(sfx_result::success!=r) {
printf("Error opening file: %d\n",(int)r);
return (int)r;
}
dump_midi(&fstm,f);
fstm.seek(0);
midi_file_source msrc;
struct mstate {
midi_clock* clock;
midi_queue* queue;
midi_file_source* source;
};
midi_stream_event* queue = (midi_stream_event*)calloc(4,sizeof(midi_stream_event));
if(queue==nullptr) {
printf("out of memory");
return (int)sfx_result::out_of_memory;
}
mstate st;
st.clock = &mclock;
st.queue = &q;
mclock.tick_callback([](uint32_t pending,unsigned long long elapsed,void* state){
mstate st=*(mstate*)state;
while(true) {
if(st.queue->size()) {
const midi_stream_event& event=st.queue->front();
bool first = true;
if(event.absolute<=elapsed) {
if(event.message.type()==midi_message_type::meta_event && event.message.meta.type==0x51) {
int32_t mt = (event.message.meta.data[0] << 16) | (event.message.meta.data[1] << 8) | event.message.meta.data[2];
printf("Set tempo to %f\n",midi_utility::microtempo_to_tempo(mt));
st.clock->microtempo(mt);
}
printf("delta: %lli - ",(long long)event.delta);
dump_midi(event.message);
event.message.~midi_message();
st.queue->pop();
} else {
break;
}
} else {
break;
}
}
},&st);
r = midi_file_source::open(&fstm,&msrc);
if(sfx_result::success!=r) {
printf("Error opening file: %d\n",(int)r);
return (int)r;
}
mclock.timebase(msrc.file().timebase);
mclock.start();
while(true) {
midi_event e;
if(q.size()>=16) {
mclock.update();
continue;
}
sfx_result r=msrc.receive(&e);
if(r!=sfx_result::success) {
if(r==sfx_result::end_of_stream) {
mclock.update();
if(!q.size()) {
printf("Exiting\n");
break;
}
continue;
} else {
printf("Error receiving message: %d\n",(int)r);
}
printf("Exiting\n");
break;
} else {
q.push({(unsigned long long)msrc.elapsed(),e.delta,e.message});
printf("queue size: %d\n",(int)q.size());
mclock.update();
}
}
}
#endif
To err is human. Fortune favors the monsters.
|
|
|
|
|
Even older than MIDI, mergesort on multi mag tapes. The issue sounds awful similar.
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
It does. It really does, assuming those mag tapes are interleaved like "raided" kind.
To err is human. Fortune favors the monsters.
|
|
|
|
|
|
They use butterflies.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
From what I recall, having done some MIDI stuff in the days before time, on a 1 MHz 8 bit CPU with 4k RAM, is that the data stream was at 31.25 kbaud.
[insert match equation here]
Which was one tick every 10 milliseconds; all 16 channels combined. So all I had to do was was to preprocess everything in less than 10 ms.
Nothing succeeds like a budgie without teeth.
To err is human, to arr is pirate.
|
|
|
|
|
Yeah, it wasn't really the speed that was my problem. It was the difficulty of streaming midi file tracks while merging them without loading more than I absolutely had to in RAM at once.
I got it working. It only keeps N messages in memory at a time, where N is the number of tracks. That's about as good as it gets I think.
To err is human. Fortune favors the monsters.
|
|
|
|
|
I think you're overcomplicating things. You could use a simple class for each track. Initialise it with the track length, byte offset/state, and a getNextByte(offset/state) callback function. Implement getNextEventTime(), and getNextEvent(). Now you can get the minimum getNextEventTime() of all tracks, and then getNextEvent() for any track that matched that minimum time. Calling getNextEvent() will update that track's next event time. Rinse & repeat. Rinsing is optional.
|
|
|
|
|
How do I know which track to pull an event from next? That's where it gets weird.
To err is human. Fortune favors the monsters.
|
|
|
|
|
From whichever tracks have the next time equal to the minimum-next-time of all tracks:
void playNextNotes()
{
int nextTime = MAX_INTVAL;
for (auto &track : tracks)
{
if (track.getNextTimestamp() < nextTime)
nextTime = track.getNextTimestamp();
}
waitUntil(nextTime);
for (auto &track : tracks)
{
if (track.getNextTimestamp() == nextTime)
{
auto event = track.getNextEvent();
midi.playEvent(event);
}
}
}
|
|
|
|
|
I got it all working last night.
The trick was in implementing your "getNextEvent()" method correctly (I don't call mine that, but same-o same-o)
To err is human. Fortune favors the monsters.
|
|
|
|
|
All good. Every problem is difficult, until it's not .
|
|
|
|
|
|
I know this feel. The problem is always present in your mind like an earworm. It invades your dreams and you can't escape it even though that is what it will take to gain a fresh perspective on it.
Have you tried any of the meditation apps? Maybe you could attend a yoga class? Sometimes writing down or talking out everything you know about the issue can help offload some of the brain activity.
There is a solution. Try to be settled by that.
|
|
|
|
|
I found the solution. It couldn't hide from me forever.
To err is human. Fortune favors the monsters.
|
|
|
|
|
For a moment forget about your environment and try to think like the original developers. You've got very limited RAM, and slightly-less limited code space. This means your code has to be clever. It also implies you don't necessarily have to handle the arbitrary, general case where all possibilities are handled regardless of what seems to be allowed by the parameters.
As an example, suppose you have a signed 16-bit value to handle. Does the usage really need to allow for negative values? What about zero (0)? What's the actual, practical range for the value?
Figuring out the actual, implicit (and undocumented) constraints can help figure out a practical algorithm.
Software Zen: delete this;
|
|
|
|
|
I totally agree with this, and when I originally planned this I was writing clever code.
I use signed values for most things because midi is largely a 7-bit protocol. that way if I accidentally set the sign bit the number jumps out at me as negative.
In the end I solved it. It took me moving my code to a real PC so I could fire up a debugger. It was just too complicated to work through it without one.
To err is human. Fortune favors the monsters.
|
|
|
|
|
honey the codewitch wrote: It took me moving my code to a real PC so I could fire up a debugger. It was just too complicated to work through it without one. I like it! I've used this approach on occasion when I "couldn't see the forest for the trees".
Software Zen: delete this;
|
|
|
|
|
It also helps keep it cross platform, so it's win win. I developed most of my GFX library for IoT devices on a PC. I would just draw to in memory bitmaps and then write those to the console as ascii. =)
To err is human. Fortune favors the monsters.
|
|
|
|
|
honey the codewitch wrote: memory bitmaps and then write those to the console as ascii That triggers a flashback to the early 90's. At the time all of the fonts in our printers were hand-drawn bitmaps. We had a text format where each character's bitmap was drawn using "." for a white pixel, and "@" for a black one. We then had a utility that would convert the text format to the binary form of the font file.
Software Zen: delete this;
|
|
|
|
|
haha I had a python script that created or extracted win 3.1 .FON files in that format for editing or creating.
I used it as a reference to create my raster font loading routines.
To err is human. Fortune favors the monsters.
|
|
|
|
|
|
I got my book written in French last week from a German seller and browsed some pages.
I felt 50% French words are English and guessed their meanings.
figured out some formulas and graphs and got some sense of this book already...
diligent hands rule....
|
|
|
|
|
formulase
The other half is Latin.
|
|
|
|
|