Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 36

Thread: Byte-by-byte allocated data manipulation

  1. #21

    Byte-by-byte allocated data manipulation

    Quote Originally Posted by WILL
    What I mean is; so far I understand that I can read the value of a sample in an allocation of memory using the byte and word pointers, but would I be able to then assign values back to the allocated memory using these same pointers?
    Yes.
    [pascal]Chuck := @MemoryAddress^; // Get Address I need
    [/pascal]
    If MemoryAddress is a pointer, [pascal]Chuck := MemoryAddress;[/pascal] is ok too.
    Quote Originally Posted by WILL
    Also can I easily extract the data and the inser it in as a diferent format? ie. read a signed byte/word value and then feed back an unsigned byte/word value?
    The format for signed and unsigned values depends on how you treat the array:
    [pascal]var
    Bob: shortint;
    Chuck: pshortint;

    Chuck := @MemoryAddress^; // access same memory
    Bob := Chuck^; // Read signed data I need
    inc(Bob); // Mess with it...
    Chuck^ := Bob; // Stick it back in there how I need it![/pascal]
    The values that are over 127 will be negative now.
    As for the OpenAL issue... try to feed it with signed data and see what happens, if it treats the data as signed values internally, it should work.

  2. #22

    Byte-by-byte allocated data manipulation

    This is off of the top of my head and without an IDE to test in, but I think I got it close. This is based off of the original source WILL posted.[pascal]// This routine doesn't allocate or check memory boundries, thats left up to you
    procedure DecodeBlock(SrcPtr, DstPtr : Pointer; Length : SizeInt; InitialVal : ShortInt = 0);
    var
    Start : Pointer;
    old,
    worker : ShortInt;
    begin
    old := InitialVal;
    Start := SrcPtr;
    while ((SrcPtr-Start)<Length) do
    begin
    // First get a value from the source pointer
    worker := PShortInt(SrcPtr)^;
    // Now perform your math on the worker
    worker := worker + old;
    // Reset the old value for the next itteration
    old := worker;
    // Write out the new value
    DstPtr^ := worker;
    // Move forward
    inc(SrcPtr, SizeOf(worker));
    inc(DstPtr, SizeOf(Worker));
    end;
    end;

    procedure LoadSamples(Instrument : TInstrumentType; NumberOfSamples : SizeInt; FileStream : TFileStream);
    var
    i : Integer;
    begin
    for i := NumberOfSamples - Instrument.NumOfSamples to NumberOfSamples - 1 do
    begin
    // Get a worker block the size of the sample data
    // NOTE : DON'T FORGET TO FREE IT LATER WHEN YOU FREE INSTRUMENT
    GetMem(Samples[i].SampleData, Samples[i].SampleLength);
    // Load the file data from the file
    FileStream.ReadBuffer(Samples[i].SampleData^, Samples[i].SampleLength);
    // Decode the memory
    DecodeBlock(Samples[i].SampleData^, Samples[i].SampleData^, Samples[i].SampleLength);
    end;
    end;[/pascal]

  3. #23
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Byte-by-byte allocated data manipulation

    Excellent! Yes, thats essentially what I need to do.

    [size=9px](Jer: You know of delta encoding I see. )[/size]

    The issue with OpenAL is that it doesn't accept signed sample data. So I have to convert it all from the allocation that I have that will either be 16-bit or 8-bit. After I have a converted/decoded allocation of memory THAT is my sample so I won't need to manipulate it further. Just feed it, or part of it, into an AL sound buffer for mixing/playback.

    The problem is 2-fold. [size=9px](or 3 if you consider the delta encoding)[/size] So I don't want to get into the second part in this thread much as I just want to figure out the manipulation of memory for now.

    In THIS THREAD I have posted the question of how I take audio sample data that is signed and properly convert it to unsigned. The data will obviously remain the same byte-size, but will merely go from signed to unsigned.

    So I'd be taking a ShortInt(8-bit signed) and turning it into a Byte(8-bit unsigned).

    ...and then...

    taking a SmallInt(16-bit signed) and turning it into a Word(16-bit unsigned).
    Jason McMillen
    Pascal Game Development
    Co-Founder





  4. #24
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Byte-by-byte allocated data manipulation

    Quote Originally Posted by User137
    Try keep variable types the same on source and destination. Extra type conversions takes time and makes errors possible. Especially if you have
    [pascal]var
    source: smallint;
    dest: word;
    begin
    source:=-1;
    dest:=source; // Now dest would be something over 30000 i guess
    // or vice versa, unsigned to signed can result in number go negative
    // or larger type to small type can make the value looping like with mod
    end;[/pascal]
    You know what i mean...

    And your code seems valid.
    Well looking at the problem that way, I can see how I might go wrong.

    I however would instead take the signed value into a local variable move that to another variable of the required sample data type and then copy that into the allocated data pointer.

    So from pointer to different typecast pointer OR from variable to different type cast pointer I can see how it might mess up my values, BUT from variable to different typecast variable it shouldn't give me issues, right?
    Jason McMillen
    Pascal Game Development
    Co-Founder





  5. #25

    Byte-by-byte allocated data manipulation

    Looks to me it doesn't matter if you typecast or use pointer, both do the same
    [pascal]procedure TForm1.FormCreate(Sender: TObject);
    var b: byte;
    s: shortint; ps: PShortint;
    begin
    b:=230;
    s:=b;
    memo1.Lines.Add(inttostr(s)); // typecasted
    ps:=@b;
    memo1.Lines.Add(inttostr(ps^)); // pointer
    end;[/pascal]
    This results the memo1 to have 2 numbers which are in this case -26 and still same if i change b value to any other.

  6. #26
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Byte-by-byte allocated data manipulation

    I understand what you are saying, BUT...

    I still have to convert data that is in 8 or 16 bit samples that have signed typecasting and convert it to unsigned samples.

    This is now getting into the issue of how I calculate/convert the new sample data to fit into the new format than how I would actually write it into the allocated memory. Though... the it is where the two are somewhat tied.

    Anyhow I think I know how to read/write to the memory, I just have to tackle the conversion issue now.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  7. #27
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Byte-by-byte allocated data manipulation

    Ran into a problem it seems...

    I for some strange and unknown reason keep getting SEGSEGV error on the following indicated line below. Now my first assumption is that I'm trying to access a piece of memory that is not allocated. But my code should restrict this. I know it's this block of code because when I remove it, not a complaint whatsoever.

    So... where am I making my mistake?

    [pascal] {Decode Sample Data}
    // Decode Audio data!
    for i := 0 to NumberOfSamples - 1 do
    begin
    if (Samples[i].is16Bit) then
    begin
    AudioBuffer_16bit_Signed := @Samples[i].SampleData;
    AudioBuffer_16bit_Unsigned := @Samples[i].SampleData;

    for j := 0 to Samples[i].SampleLength div 2 - 1 do
    begin
    SignedWordBuffer := AudioBuffer_16bit_Signed^; // << This is where I get my error!
    WordBuffer := SignedWordBuffer + 32768;
    AudioBuffer_16bit_Unsigned^ := WordBuffer;

    if (j < Samples[i].SampleLength div 2 - 1) then
    begin
    inc(AudioBuffer_16bit_Signed, 2);
    inc(AudioBuffer_16bit_Unsigned, 2);
    end;
    end;
    end
    else // not Samples[j].is16Bit // is 8-bit!
    begin
    AudioBuffer_8bit_Signed := @Samples[i].SampleData;
    AudioBuffer_8bit_Unsigned := @Samples[i].SampleData;

    for j := 0 to Samples[i].SampleLength - 1 do
    begin
    SignedByteBuffer := AudioBuffer_8bit_Signed^;
    ByteBuffer := SignedByteBuffer + 128;
    AudioBuffer_8bit_Unsigned^ := ByteBuffer;

    if (j = 113570) then
    WordBuffer := ByteBuffer;
    if (j < Samples[i].SampleLength - 1) then
    begin
    inc(AudioBuffer_8bit_Signed, 1);
    inc(AudioBuffer_8bit_Unsigned, 1);
    end;
    end;
    end;
    end;[/pascal]
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #28
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Byte-by-byte allocated data manipulation

    Oh and to be clear 'Samples[i].SampleLength' is the length of data in bytes.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  9. #29

    Byte-by-byte allocated data manipulation

    inc(AudioBuffer_16bit_Signed, 2);
    if the type of AudioBuffer_16bit_Signed is something like ^Shortint the line above is incorrect. Should be just
    inc(AudioBuffer_16bit_Signed);
    Because when you increment a pointer its type's size is automatically taken in accunt by the compiler.

    Therefore I prefer dynamic arrays.

  10. #30
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Byte-by-byte allocated data manipulation

    Tried with your inc(..blah...); correction. Same issue. Funny thing is that I get the error well before the end of what the data should be.

    Also the funniest thing... when I try to comment out all the code inside the block meant for 16-bit data I get the same SIVSEGV error and it also asks to locate 'astrings.inc'. Which just blows my mind. :? Why do I need to locate such a file and if I need it why on earth is it not there in the first place

    I'm getting the feeling that FPC is just tripping on me here... can anyone else see an error on my part? I can upload/post the whole code if you need...?
    Jason McMillen
    Pascal Game Development
    Co-Founder





Page 3 of 4 FirstFirst 1234 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •