jmlynesjr has asked for the wisdom of the Perl Monks concerning the following question:

Hello All:

(Cross posted to the wxperl-users mailing list)

Awhile back I was working on some wxPerl code to simulate an o-scope display screen. While the original requirement that started me down that path went away, I did get a proof of concept working that displayed several waveforms and a four channel logic analyser(animated). I have also been playing with the Gqrx software defined radio package and developed a wxPerl based Gqrx scanner application plug-in. A request was made on the Gqrx google group for a method to display the raw audio waveform. It sounded like a good challenge. Gqrx outputs one channel of 16-bit, 48KHz audio to a UDP port. I have this working with a scrolling audio waveform on the o-scope screen. I wonder, and it's hard to tell, if the display is keeping up with the 48KHz data stream. Am I dropping packets? Can I speed up the display process using a memory file rather than writing to a disk file? Will Wx support this? This is the current display and refresh code.

# Create the Display(bmp & sbm) and Drawing(image) Objects # Uses GD calls to draw the screen background and waveform on t +he image object $self->{bmp} = Wx::Bitmap->new("rawaudio.png", wxBITMAP_TYPE_PNG); $self->{sbm} = Wx::StaticBitmap->new($self, wxID_ANY, $self->{bmp} +, wxDefaultPosition, [600,600]); $oscope->{image} = GD::Image->new($oscope->{maxw}, $oscope->{maxh} +) || die; # Read the updated image and refresh the display $self->{bmp} = Wx::Bitmap->new("rawaudio.png", wxBITMAP_TYPE_PNG); + # Reload the screen image $self->{sbm}->SetBitmap($self->{bmp}); # Refresh sc +reen # Save the Drawing Object to a disk file my $png_data = $oscope->{image}->png; # Write image + to a file open OUTFILE, ">", "rawaudio.png" || die; binmode OUTFILE; print OUTFILE $png_data; close OUTFILE;
Can this code be modified to use a memory file? Syntax? I am also considering moving the socket code into it's own thread to isolate the reading and decoding of the UDP packets from the updating of the display. Thanks in advance,

Update:

I now have a fairly functional raw audio display script. I still don't know if I am dropping any packets. I am considering developing a UDP server script to feed controlled waveform packets to my script to help to resolve this question. See jmlynesjr for a snapshot of the screen. I will post the code to CUFP later as another wxPerl example. It was a good learning experience for using and decoding/unpacking UDP packets, writing to memory files, and for reusing my simulated o-scope code. Dereferencing lists and arrays inside hashes gave me trouble and I may post a follow-up question after a little more reading on the topic.

James

There's never enough time to do it right, but always enough time to do it over...

Replies are listed 'Best First'.
Re: Memory File and wxPerl
by BrowserUk (Patriarch) on Mar 16, 2016 at 20:09 UTC
    Can this code be modified to use a memory file?

    To paraphrase your question: Can Wx::Bitmap->new("rawaudio.png", wxBITMAP_TYPE_PNG) take a filehandle or scalar variable instead of a filename from which to create the bitmap?

    Maybe this helps?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      BrowserUk, nice, concise restatement of my question. I tend to ramble on with background info. :)

      The XPM file approach looks like a very good next thing to try. The wxBook states it's primary use as being for small bitmaps, but 600x600 is still under 400K, so it shouldn't be a memory problem. Thanks for your suggestion. I will post an update when I try it.

      James

      There's never enough time to do it right, but always enough time to do it over...

        The XPM file approach looks like a very good next thing to try.

        Actually, I think anonymonks pointer to newFromBits() is probably a better lead.

        Whilst trying to look at that, I came across newFromPngData() which is described as: "Loads a bitmap from the memory containing image data in PNG format."; and sounds perfect for your purpose.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Memory File and wxPerl
by stevieb (Canon) on Mar 16, 2016 at 19:59 UTC

    Here's an example of read and write using a memory 'file' utilizing a scalar. Give it a try and see if Wx will be ok with it:

    use warnings; use strict; my $read_memfile = <<'EOF'; one two three EOF open my $mem_fh, '<', \$read_memfile; print $_ while(<$mem_fh>); close $mem_fh; my $write_memfile; open my $mem_wfh, '>', \$write_memfile; print $mem_wfh "hello, world!\n"; close $mem_wfh; open my $str_fh, '<', \$write_memfile; print $_ while (<$str_fh>); close $str_fh; __END__ one two three hello, world!

      Thanks stevieb for the clear example. I had played around with a similar structure, but haven't yet worked through all of the errors that popped up.

      James

      There's never enough time to do it right, but always enough time to do it over...

Re: Memory File and wxPerl
by Anonymous Monk on Mar 16, 2016 at 21:14 UTC
    Wx::Bitmap->newFromBits