Author Topic: Direct Stream Digital (DSD) add-on  (Read 27872 times)

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #25 on: 20 Nov '14 - 01:44 »
Have one question related to DSD IFF files Ian.

In DSD DSF file there's a 'Sample count' field in the header, but looking at the DSF IFF specs., couldn't find this field.
Do I need to calculate this from the actual audio data chunk size, or how to get this value?

I am working on decodeless DSD editing for MP3 Stream Editor. I basically finished DSF editing, and now looking into making this work for DFF files too.
If you could answer one more question... Does the DSF and DFF files have the same audio data format? I'll try to test this, but would be useful if someone knows that if it's possible to copy audio data between DSF and DFF files, would be helpful.

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #26 on: 20 Nov '14 - 12:21 »
In DSD DSF file there's a 'Sample count' field in the header, but looking at the DSF IFF specs., couldn't find this field.
Do I need to calculate this from the actual audio data chunk size, or how to get this value?

Yes, the DSD chunk size determines the number of samples.

I am working on decodeless DSD editing for MP3 Stream Editor. I basically finished DSF editing, and now looking into making this work for DFF files too.
If you could answer one more question... Does the DSF and DFF files have the same audio data format? I'll try to test this, but would be useful if someone knows that if it's possible to copy audio data between DSF and DFF files, would be helpful.

DFF is MSB 1st, while DSF can be either MSB or LSB 1st (depending on the "Bits per sample" field).

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #27 on: 20 Nov '14 - 19:19 »
Ok. Thank you for the information.

This cross-copy thing did not work. Creating a DSF header and copying the audio data from a DFF results a quiet hissing sound and the audio plays with a lower tempo above it regardless whether I set 1 or 8 bit for 'Bits per sample'. :-\

EDIT: Tried reversing the bits for all the bytes before writing them but results the same sound.
« Last Edit: 20 Nov '14 - 21:05 by 3delite »

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #28 on: 21 Nov '14 - 21:30 »
Tried going another route, created a DSF header and wrote all the data BASS_ChannelGetData() returned, for channel created with there RAW flag. This resulted exactly the same file (bit identical) when I copied all the data from a DFF file. Again reversing the bits didn't help.

It seems, of course I am not sure, that DSF data is not raw DSD (DFF) data.

If anybody has some info on this, please post it! :-\

Thank you!

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #29 on: 24 Nov '14 - 13:13 »
Did you split the DSD data into blocks when writing the DSF file? Remember that each channel's data is in separate blocks (usually 4096 bytes) in the DSF format, rather than being interleaved bytes as in the DFF format.

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #30 on: 24 Nov '14 - 23:25 »
Thank you very much Ian, really appreciate it, it works now! This solved the issue. I now reverse the bits, collect the bytes into 4096 byte chunks per channel and now the song is playing as it should.

EDIT: There seems to be 32 bytes / channel missing when I convert the file with my code, compared to what 'DFF to DSF Converter - Light' produces.

Maybe the problem is with this tool, it also seems like it doesn't set properly the DSF 'file size' header field either.

EDIT 2: Those extra bytes are probably junk. Looking at them with a hex editor doesn't seem like DSD data, and the 'sample count' field in the header is the same for both files, so it seems that it's ok.
« Last Edit: 25 Nov '14 - 07:07 by 3delite »

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #31 on: 29 Nov '14 - 00:37 »
Encountered a strange issue happening with DSD files - it seems both DFF and DSF are affected.

When I play the file for the first time the song is playing at like half the speed. If I release the BASS channel handle, end stream create, and play again, it's ok. If I exit the app. and start again, play the file again, it's slow again.

The playback position is also counting slower.

Additional info: BASS_SetConfig(BASS_CONFIG_DSD_FREQ, 352800) is used, the channel is plugged into a 4 channel 192000 Hz mixer which feeds a WASAPI output.

« Last Edit: 30 Nov '14 - 17:47 by 3delite »

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #32 on: 1 Dec '14 - 14:43 »
Are you sure the mixer's sample format matches the WASAPI output format as reported by BASS_WASAPI_GetInfo? For example, if the mixer has 4 channels but the WASAPI output is only stereo, then it will play at half speed. Does the problem still happen if you use BASS/DirectSound output instead of WASAPI?

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #33 on: 1 Dec '14 - 19:08 »
Yes the mixer format is ok for sure, it's a complete app. just now not mp3 but dsd is plugged in (other file formats play ok, and stream creating for a second time, it works for DSD too).
And I am afraid yes, it happens with Direct Sound too, tried just now.

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #34 on: 2 Dec '14 - 15:12 »
That's strange. Does the problem only happen when BASS_CONFIG_DSD_FREQ is set to 352800, not 176400 or lower? What does BASS_ChannelGetInfo say about the stream's sample format when the problem happens?

alerma

  • Posts: 16
Re: Direct Stream Digital (DSD) add-on
« Reply #35 on: 2 Dec '14 - 16:26 »
Hey Ian,

keep up the good work! Any plans to port the DSD add-on to Android?

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #36 on: 2 Dec '14 - 17:37 »
An Android port is possible, but aren't DSD files a bit too large for mobile platforms? One issue is that the Android BASS version doesn't currently support floating-point output (ie. no BASS_SAMPLE_FLOAT), but that may change.

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #37 on: 2 Dec '14 - 19:19 »
Ok, found the bug... ...there is a track bar to adjust playback speed, and it's max value was at 191000, and after clicking play it's OnChange() was called which set the playback speed to 191000...

It works fine now. Thank you!

alerma

  • Posts: 16
Re: Direct Stream Digital (DSD) add-on
« Reply #38 on: 3 Dec '14 - 18:50 »
An Android port is possible, but aren't DSD files a bit too large for mobile platforms? One issue is that the Android BASS version doesn't currently support floating-point output (ie. no BASS_SAMPLE_FLOAT), but that may change.

Nowadays mobiles easily support up to 128GB of external SD cards with theoretical limit up to 2TB, so space shouldn't be a problem. Also people are free to use Network playback for Hi-Res files (including DSD) using Samba shares (should work with Windows and NAS) or DLNA servers like JRiver.
I am not sure why lack of floating-point output can affect DSD decoding, but hopefully you can add the floating-point support as an option for those developers who want to use it on powerful enough mobiles (nowadays android flagman mobiles are beasts)

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #39 on: 8 Dec '14 - 16:48 »
DSD can be decoded to 16-bit PCM. It's just that it seems a bit wasteful to play DSD files in 44100Hz 16-bit PCM, which is what most Android devices would be using. May as well convert them to FLAC/WavPack and save a load of space :)

alerma

  • Posts: 16
Re: Direct Stream Digital (DSD) add-on
« Reply #40 on: 8 Dec '14 - 19:13 »
DSD can be decoded to 16-bit PCM. It's just that it seems a bit wasteful to play DSD files in 44100Hz 16-bit PCM, which is what most Android devices would be using. May as well convert them to FLAC/WavPack and save a load of space :)

Your point is good for most cases, but... I am working on Hi-Res music player for Android with a custom (user mode) USB audio driver which should allow to play natively up to 32-bit/384kHz to an external USB DAC, thus completely bypassing the Android audio system limitation of 16/48.

Probably my request is rare, but maybe other developers will find this add-on for Android useful for their own needs...

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #41 on: 11 Dec '14 - 18:02 »
An update is up in the 1st post, adding support for internet and buffered user file streaming, and DSDIFF comments.

If there are no problems, I think BASSDSD is pretty much ready for release now. So please report any problems encountered.

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #42 on: 13 Dec '14 - 06:19 »
Some notes for the Delphi unit:

BASS_ChannelGetTags() returns a pointer to the TAG_DSD_COMMENT structure if I am correct, so define a type for that:

Code: [Select]
type
    PTAG_DSD_COMMENT = ^TAG_DSD_COMMENT;

The comment chars are needed to be specified as a fixed char array,

Code: [Select]
    commentText: Array of AnsiChar; // text

this defines a Delphi specific array which will be technically a pointer, it should be something like:

Code: [Select]
    commentText: Array[0..256] of AnsiChar; // text

or maybe better 0..4096, or some reasonable value.
But there is no AnsiChar in Delphi NextGen compiler so it should be better a 'byte' array, so all the above:

Code: [Select]
type
  PTAG_DSD_COMMENT = ^TAG_DSD_COMMENT;
  TAG_DSD_COMMENT = packed record
timeStampYear: Word; // creation year
TimeStampMonth: Byte; // creation month
timeStampDay: Byte; // creation day
timeStampHour: Byte; // creation hour
timeStampMinutes: Byte; // creation minutes
cmtType: Word; // comment type
cmtRef: Word; // comment reference
count: DWORD; // string length
commentText: Array[0..4096] of Byte; // text
  end;

That's all.

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #43 on: 15 Dec '14 - 13:19 »
Thanks. I guess 4096 characters should be enough, but in theory the "commentText" member could be anything up to 4 billion characters long, as "count" is 32-bit. In the C/C++ header, it is defined as a flexible array ("char commentText[]") which allows the array to be any size (whatever is allocated). Does Delphi have something like that?

Presumably that "AnsiChar" thing will also affect BASS and the other add-ons, and a lot of non-BASS code too I guess. Rather than modifying a bunch of existing units, perhaps it is possible for the user to define "AnsiChar" as "Byte" themselves? Something like this...

Code: [Select]
type
  AnsiChar = Byte;
  PAnsiChar = PByte;

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #44 on: 17 Dec '14 - 14:22 »
BASSDSD and the BASSASIO update are now up on the BASS and BASSASIO pages, respectively. Note the BASS_TAG_DSD_COMMENT value was changed in the BASSDSD release (to make space for new BASS_TAG_DSD_ARTIST/TITLE tags).

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #45 on: 19 Dec '14 - 03:50 »
Maybe the best method is like in BASS.pas:

Code: [Select]
   commentText: Array[0..maxInt div 2 - 1] of Byte;

This compiles.

About AnsiChar: quick test shows here that the following works:

Code: [Select]
{$IFDEF NEXTGEN}
type
    AnsiChar = Byte;
    PAnsiChar = PByte;
{$ENDIF}

It seems like enough just to add at the beginning of the unit and the existing declarations will work with NextGen (tried with BASS.pas).

But there is a problem, when trying to compile for Android:

Code: [Select]
const
bassdll = 'libbass.dylib';
{$IFDEF MSWINDOWS}
  bassdll = 'bass.dll';
{$ENDIF}
{$IFDEF LINUX}
  bassdll = 'libbass.so';
{$ENDIF}
{$IFDEF MACOS}
  bassdll = 'libbass.dylib';
{$ENDIF}

...missing the Android part:

Code: [Select]
{$IFDEF ANDROID}
  bassdll = 'libbass.so';
{$ENDIF}

Although not tested if changing these things will work with Delphi for Android, there is a linker error, when trying to build an empty app. with added this BASS.pas:

Quote
[DCC Error] E2597 D:\Users\Public\Documents\Embarcadero\Studio\15.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -lbass

Don't know what this means. I mean dynamically loading the BASS .so files (and plugins) work fine.
« Last Edit: 20 Dec '14 - 00:21 by 3delite »

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #46 on: 23 Dec '14 - 14:25 »
Maybe the best method is like in BASS.pas:

Code: [Select]
    commentText: Array[0..maxInt div 2 - 1] of Byte;

Yep, I was thinking of doing that, but then I thought what if someone wants to allocate a TAG_DSD_COMMENT record themselves? Then again, any fixed size definition will complicate that, so may as well use the maximum :) ... An updated BASSDSD.PAS file is now up in the BASSDSD package. The same thing has been applied to the TAG_BEXT record in BASS.PAS, as that structure also has a flexible array.

...missing the Android part:

Code: [Select]
{$IFDEF ANDROID}
  bassdll = 'libbass.so';
{$ENDIF}

Although not tested if changing these things will work with Delphi for Android, there is a linker error, when trying to build an empty app. with added this BASS.pas:

Quote
[DCC Error] E2597 D:\Users\Public\Documents\Embarcadero\Studio\15.0\PlatformSDKs\android-ndk-r9c\toolchains\arm-linux-androideabi-4.6\prebuilt\windows\bin\arm-linux-androideabi-ld.exe: error: cannot find -lbass

That looks like the linker can't find the BASS library (libbass.so). Is there an option to add a library search path, or perhaps a special location that you need to place libraries?

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #47 on: 23 Dec '14 - 22:57 »
Yes that was my thought too, but probably it's a better idea to be able to use the structure with BASS then to use for custom things.
BTW if the string is not null terminated, then the user needs to copy the string anyway, so it doesn't really matter how many chars the structure has, one have to use a pointer to the first char and that's all...

Regarding the linker error: copying the 'libbass.so' file to "D:\Users\Public\Documents\Embarcadero\Studio\15.0\PlatformSDKs\android-ndk-r9c\platforms\android-14\arch-arm\usr\lib\" solves the issue, the empty project compiles ok. Don't know where to specify the library path for the linker. It seems it's not enough to add the 'libbass.so' file in deployment options.
I'll check to create a little test app. to check if the whole thing works this way.

3delite

  • Posts: 895
Re: Direct Stream Digital (DSD) add-on
« Reply #48 on: 23 Dec '14 - 23:34 »
Can not get it to work. The app. starts and exits right away as soon as I add BASS the the uses list.
In deployment options tried adding 'libbass.so' to 'library\lib\armeabi' and 'library\lib\armeabi-v7a' but doesn't work.

Ian @ un4seen

  • Administrator
  • Posts: 20389
Re: Direct Stream Digital (DSD) add-on
« Reply #49 on: 24 Dec '14 - 13:39 »
Can you see the BASS library in the compiled app's APK file?

When using the Android NDK's build scripts, libraries can be included via the PREBUILT_SHARED_LIBRARY option. From the path in the error message you posted, it looks like Delphi is using the standard NDK, so perhaps it has some way of accessing the PREBUILT_SHARED_LIBRARY option?

   www.kandroid.org/ndk/docs/PREBUILTS.html