HydrogenAudio

Hydrogenaudio Forum => Uploads => Topic started by: tycho on 2012-08-26 22:02:53

Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: tycho on 2012-08-26 22:02:53
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.
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: Nick.C on 2012-08-26 22:09:11
Thanks tycho - I'll have a look tomorrow....

Really must dust off my Turbo CPP Explorer install!
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: tycho on 2012-08-26 22:59:21
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/ (http://shinnok.com/rants/2011/07/18/using-the-qt-creator-ide-with-non-qt-projects/) ). Using g++ will make it easier to port to Linux as well.

Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: [JAZ] on 2012-08-27 20:22:17
A couple of code snippets:

in nSupport.h:

Code: [Select]
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: [Select]
#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: [Select]
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: [Select]
		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 (http://psycle.svn.sourceforge.net/viewvc/psycle/trunk/psycle-helpers/src/psycle/helpers/riff.cpp?revision=10069&view=markup)
riff.hpp (http://psycle.svn.sourceforge.net/viewvc/psycle/trunk/psycle-helpers/src/psycle/helpers/riff.hpp?revision=10069&view=markup)
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)
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: db1989 on 2012-08-27 21:40:48
in nCore.h, stdint is not present in VS 2008 and below
Code: [Select]
#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.
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: [JAZ] on 2012-08-27 22:01:50
Oops..
Yes, you're right. I should have looked better for the correct way of doing so:

Code: [Select]
#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)
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: db1989 on 2012-08-27 22:30:39
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:
Quote
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.


FWIW, there are implementations of stdint.h that can be used with VC(++), or there’s Boost (http://www.boost.org/).

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.
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: tycho on 2012-08-27 23:19:31
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.
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: tycho on 2012-08-28 21:26:28
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.
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: tycho on 2012-09-04 06:34:22
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 limited time to follow up on this before Christmas, so any help to get this working is welcome..

Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: Nick.C on 2012-09-04 18:40:47
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.
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: tycho on 2012-09-05 00:38:13
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.
Title: lossyWAV 1.3.0 source translated to C++ from Delphi
Post by: Nick.C on 2012-09-05 13:03:27
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]