IPB

Welcome Guest ( Log In | Register )

> Upload forum rules

- No over 30 sec clips of copyrighted music. Cite properly and never more than necessary for the discussion.


- No copyrighted software without permission.


- Click here for complete Hydrogenaudio Terms of Service

lossyWAV 1.3.0 source translated to C++ from Delphi
tycho
post Aug 26 2012, 22:02
Post #1





Group: Members
Posts: 345
Joined: 5-August 03
Member No.: 8183



Here is the first public preview of the source translation of lossyWAV 1.3.0 to C++. It is not finished, but the main work is done.

- vc10_err.txt list the current compiler output in Visual Studio C++ 2010.
- A number of built-in Delphi functions are not yet converted. Most are trivial, but Write() involves some work.

Please note, I know very little of how the code actually works, I basically translated it line by line, and did minor optimizations.
Developers can look over the code and compare with the Delphi source. I have tried to take into account a number of C++'s quirks, e.g.:
- integer / integer -> integer (not floating point, but if one operand is float, it returns float)
- unsigned int (opr) signed int -> unsigned int
- & is bitwise-and, && is boolean-and, likewise with or: |, ||
- c++ base 0 arrays, versus base X in delphi. (range_array class in nSupport.h emulates that)

Optimizations:
* var = var opr value --> var opr= value;
* var = var + 1 --> ++ var;
* and a few more

I added file nSupport.h that has a few Delphi emulation functions and classes. The shared global variables are only declared in the header file. I've not defined them in the cpp files yet.
Attached File(s)
Attached File  lossyWAV_cpp.7z ( 90.42K ) Number of downloads: 89
 
Go to the top of the page
+Quote Post
 
Start new topic
Replies (1 - 12)
Nick.C
post Aug 26 2012, 22:09
Post #2


lossyWAV Developer


Group: Developer
Posts: 1789
Joined: 11-April 07
From: Wherever here is
Member No.: 42400



Thanks tycho - I'll have a look tomorrow....

Really must dust off my Turbo CPP Explorer install!


--------------------
lossyWAV -q X -a 4 --feedback 4| FLAC -8 ~= 320kbps
Go to the top of the page
+Quote Post
tycho
post Aug 26 2012, 22:59
Post #3





Group: Members
Posts: 345
Joined: 5-August 03
Member No.: 8183



You're welcome, Nick.

Not sure, but your compiler may have gotten too old. You can download Visual C++ 2010 Express - it's free, but must be registered via a microsoft account within 30 days or so.

Alternatively (or in addition) you may use the gnu g++ compiler (Mingw). Code::Blocks or Qt Creator are great IDE's for that. ( http://shinnok.com/rants/2011/07/18/using-...on-qt-projects/ ). Using g++ will make it easier to port to Linux as well.

Go to the top of the page
+Quote Post
[JAZ]
post Aug 27 2012, 20:22
Post #4





Group: Members
Posts: 1772
Joined: 24-June 02
From: Catalunya(Spain)
Member No.: 2383



A couple of code snippets:

in nSupport.h:

CODE

inline std::string IntToStr(int intval) {
std::stringstream st;
st << intval;
return st.str();
}
/*Accounts for Delphi's 1-based index*/
inline std::string Copy(const std::string& in, int begin1b, int amount) {
in.substr(begin1b-1, amount);
}

inline bool FileExists(std::string file_name) {
std::fstream filetest(file_name.c_str(), std::ios_base::in | std::ios_base::binary);
if (filetest.is_open())
{
filetest.close();
return true;
}
return false;
}

Also, note that where you have the Copy calls, you're replacing the Length(string) for sizeof(string). That's wrong. use string.length().

in nCore.h, stdint is not present in VS 2008 and below
CODE

#if defined _MSC_VER
#if _MSC_VER > 1500
#include <stdint>
#else
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef signed long long int int64_t;
typedef unsigned long long int uint64_t;
#endif
#endif

I also needed to redefine the way you declared the FFTW interface, like this:
fftw_interface.h
CODE

typedef void * (* FUN_Plan_DFT_r2c_1d) (int, double* , double* , unsigned int );
typedef void* (* FUN_Plan_DFT_1d)(int, double* , double* , int , unsigned int );
typedef void (* FUN_Execute)(void* );
typedef void (* FUN_Destroy_Plan)(void* );

struct FFTW_Rec
{
FUN_Plan_DFT_r2c_1d Plan_DFT_r2c_1d;
FUN_Plan_DFT_1d Plan_DFT_1d;
FUN_Execute Execute;
FUN_Destroy_Plan Destroy_Plan;
void* Plans [ MAX_FFT_BIT_LENGTH + 1 ];
};


fftw_interface.cpp
CODE

FFTW.Plan_DFT_r2c_1d = (FUN_Plan_DFT_r2c_1d) GetProcAddress(FFTW_DLL_Handle, "fftw_plan_dft_r2c_1d");
FFTW.Plan_DFT_1d = (FUN_Plan_DFT_1d) GetProcAddress(FFTW_DLL_Handle, "fftw_plan_dft_1d");
FFTW.Execute = (FUN_Execute) GetProcAddress(FFTW_DLL_Handle, "fftw_execute");
FFTW.Destroy_Plan = (FUN_Destroy_Plan) GetProcAddress(FFTW_DLL_Handle, "fftw_destroy_plan");


Also, i had to add some includes here and there ( <cmath> mostly )

At last, i'm not sure if it will be of help, or it will add to confusion, but these two source files might help on the file loading
riff.cpp
riff.hpp
That's a .wav file load/save in a mixture of C and C++. (Not the best code anyway, it was scheduled for improvement, but since it works, it has been kept)


This post has been edited by [JAZ]: Aug 27 2012, 20:26
Go to the top of the page
+Quote Post
db1989
post Aug 27 2012, 21:40
Post #5





Group: Super Moderator
Posts: 5275
Joined: 23-June 06
Member No.: 32180



QUOTE ([JAZ] @ Aug 27 2012, 20:22) *
in nCore.h, stdint is not present in VS 2008 and below
CODE

#if defined _MSC_VER
#if _MSC_VER > 1500
#include <stdint>
#else
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed short int16_t;
typedef unsigned short uint16_t;
typedef signed int int32_t;
typedef unsigned int uint32_t;
typedef signed long long int int64_t;
typedef unsigned long long int uint64_t;
#endif
#endif
I canít claim to be an expert, but I thought that the reason C99 introduced these numbered types in its stdint.h was due to a demand for integers of known and fixed widths, with the result that these types are guaranteed to have the corresponding widths in bits. Although it may not matter for lossyWAV, I would say in a general sense that typedefing them to be equivalent to standard types, which come with no guarantee of a given fixed width (you get a minimum width at best), doesnít seem an adequate compensation for a compiler that lacks stdint.h.
Go to the top of the page
+Quote Post
[JAZ]
post Aug 27 2012, 22:01
Post #6





Group: Members
Posts: 1772
Joined: 24-June 02
From: Catalunya(Spain)
Member No.: 2383



Oops..
Yes, you're right. I should have looked better for the correct way of doing so:

CODE

#if defined _MSC_VER
#if _MSC_VER > 1500
#include <stdint>
#else
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
#endif


Seems this existed since VC6 (so that makes it compatible with enough older versions)
Go to the top of the page
+Quote Post
db1989
post Aug 27 2012, 22:30
Post #7





Group: Super Moderator
Posts: 5275
Joined: 23-June 06
Member No.: 32180



I was going to say it was good (and my something-new-learned-today) that Microsoft provide a way (albeit proprietary, but who would’ve expected anything else?) of defining fixed widths, but then I read about their types, and they seem to be merely a different flavour of syntactic sugar for the same non–fixed-width standard types:
The types __int8, __int16, and __int32 are synonyms for the ANSI types that have the same size, and are useful for writing portable code that behaves identically across multiple platforms. The __int8 data type is synonymous with type char, __int16 is synonymous with type short, and __int32 is synonymous with type int.
unsure.gif

FWIW, there are implementations of stdint.h that can be used with VC(++), or there’s Boost.

It’s not my intention to assert (geddit?) a problem where they might not be one, and I’m not meaning to blame anyone for C/C++’s lack of well-defined boundaries in various contexts, but I’m just mentioning all this in case it might matter to the project at hand.

This post has been edited by db1989: Aug 27 2012, 22:46
Go to the top of the page
+Quote Post
tycho
post Aug 27 2012, 23:19
Post #8





Group: Members
Posts: 345
Joined: 5-August 03
Member No.: 8183



Thank you guys. I already did IntToStr() and Copy(), but the rest goes straight in. Actually, I simply replaced directly Copy() with substr() and did the -1 adjustment. I've defined variables in cpp files, and implemented Bit_Scan_Left() and ..Right(). Both GCC and Visual Studio have intrinsic functions for these, and they are now used. There is a C-implementation for other compilers/HW as well. /add: I've also done Val()

I think the stdint.h thing is a minor problem, only the int32_t and int64_t are used. Most important now is to get it working on ONE compiler, then it's a simple matter to port it to others (older etc).

The WAV-reading files could possibly be used, although I hope it's is easier to implement the rest of the Delphi IO functions that are missing. These are roughly:

AssignFile
BlockRead
BlockWrite
Close
CloseFile
DirectoryExists
Eof
FileGetAttr
FileMode
FilePos
FileSetAttr
Reset
Rewrite
Seek

Others:
Build
DateTimeToString
Format
Now

Like I said before, many of these can be replaced with a single correponding c++ stdlib function, others need small wrappers.

This post has been edited by tycho: Aug 27 2012, 23:50
Go to the top of the page
+Quote Post
tycho
post Aug 28 2012, 21:26
Post #9





Group: Members
Posts: 345
Joined: 5-August 03
Member No.: 8183



Here is v03 of the translation. (I didn't publish v02). I know its silly not to use github, but I haven't gotten around to it. To compare the two directory trees, use WinMerge. Now, it almost compiles, only complaining about some missing functions, and two more issues:

Append
AssignFile
BlockWrite
Close
DirectoryExists
Pos
Reset
Rewrite
blockread
closeFile
eof
filegetattr
filemode
filepos
filesetattr
seek

These calls should replaced by the native c++ <iostream> lib calls in nWav.cpp and nParameter.cpp without making wrapper function. I would be happy if someone (JAZ?) would take the job - it's not hard, but I need a break. Google "Append delphi", etc. will give the required documentation at delphibasics.co.uk.

Write <-- this is harder, may wait (empty function defined in nSupport.h)
datetimetostring

Nick, also the argument to datetimetostring() call in nCore is a little mysterious to me - not sure handle that one.
Attached File(s)
Attached File  lossyWAV_cpp_v03.7z ( 90.1K ) Number of downloads: 66
 
Go to the top of the page
+Quote Post
tycho
post Sep 4 2012, 06:34
Post #10





Group: Members
Posts: 345
Joined: 5-August 03
Member No.: 8183



Attached, lossyWAV-cpp_v04.

I finally made it to link successfully, however it does not work yet. I have only tried it once, and it fails on trying to read a wav file although no arguments are given. Something with the parameter checking must be wrong. The program must be compiled and run with a debugger, to ensure that it does what it is supposed to.
Notes:
  • I have used gnu g++ 4.7.0 via Qt Creator (CMake) for the last part of the translation, not sure if it will compile cleanly in Visual Studio anylonger.
  • Many of the Delphi structs were "Packed Record" types, meaning they have non-aligned members. There is a #pragma pack() feature in C as well, but it's not portable, and slows down processing. Please identify which, if any structs needs to be "packed". Typically if the WAV format has "unaligned fields" that are directly read from/written to data structures. Check sizeof(struct) if they are of expected size.
  • The Delphi functions filegetattr(), filesetattr() are not translated (yet), only used for checking and forcing to write on read-only output files.
  • Version checking stuff is translated, but commented out. The version is simply set (some constants should be made or a file with version could be #included). Also, this code wouldn't be possible to translate to Linux anyhow.
  • Global initialization objects in each translation unit (each .cpp file) may have some issues because the sequence of them are random. Initialization could better be done explicitly from the main() routine, in a controlled sequence.
  • The Delphi text Write() function calls has optional formatting in arguments (like :6:4). These are commented out, but can be emulated closely by by inserting << setw(n) and << setprecision(m), etc.
  • I may have done all sorts of other errors during translation, please verify with the original source. The translation still matches the original closely, and it should stay so until it works.
  • DirectoryExist() function is not translated yet. Only an empty function returning true in nSupport.h for now. Must be done.

I have limited time to follow up on this before Christmas, so any help to get this working is welcome..


Attached File(s)
Attached File  lossyWAV_cpp_v04.7z ( 341.91K ) Number of downloads: 122
 
Go to the top of the page
+Quote Post
Nick.C
post Sep 4 2012, 18:40
Post #11


lossyWAV Developer


Group: Developer
Posts: 1789
Joined: 11-April 07
From: Wherever here is
Member No.: 42400



Thanks Tycho.

I''ve got it to compile with Dev-CPP. It doesn't't like strings inside structures and parameter passing was a bit tricky. I''ll post a revision in the development thread when I have something worthwhile to contribute.


--------------------
lossyWAV -q X -a 4 --feedback 4| FLAC -8 ~= 320kbps
Go to the top of the page
+Quote Post
tycho
post Sep 5 2012, 00:38
Post #12





Group: Members
Posts: 345
Joined: 5-August 03
Member No.: 8183



QUOTE (Nick.C @ Sep 4 2012, 09:40) *
Thanks Tycho.

I''ve got it to compile with Dev-CPP. It doesn't't like strings inside structures and parameter passing was a bit tricky. I''ll post a revision in the development thread when I have something worthwhile to contribute.

Ok. I have fixed the initialization problems, and more, so now it actually goes all the way to the main processing loop, and things look good up until there in the debugger. The main loop is slow as heck, and puts out garbage, so something is very wrong - haven't looked at it. I'm now back to using Visual Studio 2010, using the debugger there.
Attached File(s)
Attached File  lossyWAV_cpp_v05.7z ( 91.09K ) Number of downloads: 70
 
Go to the top of the page
+Quote Post
Nick.C
post Sep 5 2012, 13:03
Post #13


lossyWAV Developer


Group: Developer
Posts: 1789
Joined: 11-April 07
From: Wherever here is
Member No.: 42400



After a bit of debugging, I've got this version to write bit-reduced output - not dealing with the residue in the write buffers yet, but it's working!

[edit] Superseded. [/edIt]

This post has been edited by Nick.C: Sep 8 2012, 09:47


--------------------
lossyWAV -q X -a 4 --feedback 4| FLAC -8 ~= 320kbps
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 20th August 2014 - 20:03