0

I am trying to load a file into a stream in BASS via ReadDirectoryChanges. When I use a testfunction using FileOpenDialog I get a working result, but when I use RDC it craps out on me all the time. I must be doing something wrong, but for the life of me I can't figure it out. Maybe someone here can point me in the right direction. Below is a snippet of the code I use:

void __fastcall TForm9::ToWav(FILE_NOTIFY_INFORMATION* File)
{
    String FullFileName = OpokaClient.ImportLocation + copyfname(File);
    int BassResult = 0;


    LogLine("File added to: " + FullFileName, apSYSTEM);
    HSTREAM convert = NULL;

    convert = MyBASS_StreamCreateFile(false, FullFileName.c_str(), 0, 0, BASS_STREAM_DECODE | BASS_UNICODE);
    BassResult = MyBASS_ErrorGetCode();

    LogLine("Bass Result: " + IntToStr(BassResult), apSYSTEM);

}
//---------------------------------------------------------------------------

void __fastcall TForm9::Button2Click(TObject *Sender)
{
    String FileName;
    int BassResult = 0;

    if(FileOpenDialog1->Execute()) {
        FileName = FileOpenDialog1->FileName;
    } else {
        LogLine("No file selected. FileName empty", apWARNING);
        return;
    }

    HSTREAM convert = NULL;

    LogLine("ConverterMain: ToWav: FileName: " + FileName, apSYSTEM);

    convert = MyBASS_StreamCreateFile(false, FileName.c_str(), 0, 0, BASS_STREAM_DECODE | BASS_UNICODE);
    BassResult = MyBASS_ErrorGetCode();

    LogLine("chan ErrorCode: " + IntToStr(BassResult), apSYSTEM);

}
//---------------------------------------------------------------------------

copyfname is in a different unit..

wchar_t* copyfname(FILE_NOTIFY_INFORMATION* pfi)
{

    wchar_t* pfn;

    pfn = new wchar_t[pfi->FileNameLength+1];

    ZeroMemory(pfn, sizeof(wchar_t)*(pfi->FileNameLength+1));
    memcpy(pfn, pfi->FileName, pfi->FileNameLength);
    return pfn;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • What is `OpokaClient.ImportLocation` declared as? You are leaking the memory from `copyfname()`, why not have it return a `String` instead of a `wchar_t*`? Did you verify that `FullFileName` is the correct path you are expecting? What does "craps out on me" mean, exactly? – Remy Lebeau Dec 15 '22 at 15:36
  • This is a Q&A site. Please do not edit your question to provide a solution. If you have a solution/explanation, post it as an actual answer instead. See [Can I answer my own question?](https://stackoverflow.com/help/self-answer) I have rolled back your edits – Remy Lebeau Dec 15 '22 at 15:39
  • Problem solved! When you load a stream via FileOpenDialog, the entire file is still sitting where you want to load it from, but.. Using ReadDirectoryChanges you are reacting to a call that something has changed in a directory. That does NOT mean the entire file is actually there (yet). With .mp3 files on a m.2 SSD you do not need to worry about it that much. A file of 3 MB takes a millisecond, but a .flac file is 10 times bigger.. I added a very dirty Sleep routine in my code for about 2 seconds. Lo and behold, no problems loading in the .flac stream! – Richard Harreman Dec 15 '22 at 16:04
  • `ReadDirectoryChanges()` can tell you not only when a file is created, but also when it is written to. You didn't show which event(s) you are actually catching, but perhaps use the latter event to (re)start a timer (not a sleep) and then process the file once it has been idle for a few seconds. – Remy Lebeau Dec 15 '22 at 17:06

0 Answers0