in reply to Converting 7 & 8-bit values

The following pure Perl code runs at least 2000 times faster than required to just keep up with one 1200 baud midi channel in one direction. Possibly fast enough?

use strict; use warnings; use Benchmark qw(cmpthese); my @data28 = map {int (rand () * 256)} 1..28; printf "%02x ", $_ for @data28; print "\n"; my @xlate = thereAndBack (@data28); printf "%02x ", $_ for @xlate; print "\n\n"; my @data280 = map {int (rand () * 256)} 1..280; my @data2800 = map {int (rand () * 256)} 1..2800; cmpthese (-1, { data28 => sub {thereAndBack (@data28)}, data280 => sub {thereAndBack (@data280)}, data2800 => sub {thereAndBack (@data2800)}, } ); sub thereAndBack { return fromKarma (toKarma (@_)); } sub toKarma { die "Blocks must be multiples of 8 bytes" if @_ % 7; my @raw = @_; my @karma; while (@raw) { my @block = splice @raw, 0, 7; my $extra = 0; for my $byte (@block) { $extra |= $byte & 0x80; $byte &= 0x7F; $extra >>= 1; } push @karma, ($extra, @block); } return @karma; } sub fromKarma { die "Blocks must be multiples of 8 bytes" if @_ % 8; my @karma = @_; my @raw; while (@karma) { my @block = splice @karma, 0, 8; my $extra = shift @block; for my $byte (reverse @block) { $extra <<= 1; $byte |= $extra & 0x80; } push @raw, @block; } return @raw; }

Prints:

37 8b a0 23 ef 46 68 f8 a1 75 71 de aa eb 0e ee 08 54 c7 77 9f 0b ee d +7 d3 f4 34 69 37 8b a0 23 ef 46 68 f8 a1 75 71 de aa eb 0e ee 08 54 c7 77 9f 0b ee d +7 d3 f4 34 69 Rate data2800 data280 data28 data2800 124/s -- -90% -98% data280 1228/s 888% -- -85% data28 8200/s 6496% 568% --

Note that there is some per packet overhead so there is some advantage in translating larger blocks.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Converting 7 & 8-bit values
by BrowserUk (Patriarch) on Dec 04, 2006 at 04:13 UTC
    ... to just keep up with one 1200 baud midi channel ...

    Um. It's been several years since I played with a midi device, but back then the baud rate was 31250 bps. The number sticks in my head because a 500kHz clock with a divide-by-16 circuit produced exactly the required clock rate.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      You may well be right. I was just going on Corion's post Re: Converting 7 & 8-bit values. That makes the code only about 100 times faster than it needs to be just to keep up and if nothing else were happening. Probably ok, but depends what else is happening and what sort of response time is required.


      DWIM is Perl's answer to Gödel
        GrandFather, That's *exactly* what I was after, and no need for Inline::C - one less dependency. And it's pretty elegant, too. For the record, the speed is well within the bounds of what's necessary: it needs to reply & respond well within a workable time, but not strictly real-time in the MIDI sense, so all is good.

        Your help is really appreciated.