Use ReadFile to read opus sounds in editor

This commit is contained in:
Robert Müller 2022-06-14 21:13:25 +02:00
parent 0a463320cf
commit de2744cb2a
2 changed files with 12 additions and 48 deletions

View file

@ -3744,36 +3744,26 @@ void CEditor::AddSound(const char *pFileName, int StorageType, void *pUser)
}
// load external
IOHANDLE SoundFile = pEditor->Storage()->OpenFile(pFileName, IOFLAG_READ, StorageType);
if(!SoundFile)
void *pData;
unsigned DataSize;
if(!pEditor->Storage()->ReadFile(pFileName, StorageType, &pData, &DataSize))
{
dbg_msg("sound/opus", "failed to open file. filename='%s'", pFileName);
return;
}
// read the whole file into memory
int DataSize = io_length(SoundFile);
if(DataSize <= 0)
{
io_close(SoundFile);
dbg_msg("sound/opus", "failed to open file. filename='%s'", pFileName);
return;
}
void *pData = malloc((unsigned)DataSize);
io_read(SoundFile, pData, (unsigned)DataSize);
io_close(SoundFile);
// load sound
int SoundId = pEditor->Sound()->LoadOpusFromMem(pData, (unsigned)DataSize, true);
if(SoundId == -1)
{
free(pData);
return;
}
// add sound
CEditorSound *pSound = new CEditorSound(pEditor);
pSound->m_SoundID = SoundId;
pSound->m_DataSize = (unsigned)DataSize;
pSound->m_DataSize = DataSize;
pSound->m_pData = pData;
str_copy(pSound->m_aName, aBuf, sizeof(pSound->m_aName));
pEditor->m_Map.m_vpSounds.push_back(pSound);
@ -3796,27 +3786,14 @@ void CEditor::ReplaceSound(const char *pFileName, int StorageType, void *pUser)
CEditor *pEditor = (CEditor *)pUser;
// load external
IOHANDLE SoundFile = pEditor->Storage()->OpenFile(pFileName, IOFLAG_READ, StorageType);
if(!SoundFile)
void *pData;
unsigned DataSize;
if(!pEditor->Storage()->ReadFile(pFileName, StorageType, &pData, &DataSize))
{
dbg_msg("sound/opus", "failed to open file. filename='%s'", pFileName);
return;
}
// read the whole file into memory
int DataSize = io_length(SoundFile);
if(DataSize <= 0)
{
io_close(SoundFile);
dbg_msg("sound/opus", "failed to open file. filename='%s'", pFileName);
return;
}
void *pData = malloc((unsigned)DataSize);
io_read(SoundFile, pData, (unsigned)DataSize);
io_close(SoundFile);
CEditorSound *pSound = pEditor->m_Map.m_vpSounds[pEditor->m_SelectedSound];
// unload sample

View file

@ -545,24 +545,11 @@ int CEditorMap::Load(class IStorage *pStorage, const char *pFileName, int Storag
str_format(aBuf, sizeof(aBuf), "mapres/%s.opus", pName);
// load external
IOHANDLE SoundFile = pStorage->OpenFile(pName, IOFLAG_READ, IStorage::TYPE_ALL);
if(SoundFile)
{
// read the whole file into memory
pSound->m_DataSize = io_length(SoundFile);
if(pSound->m_DataSize > 0)
{
pSound->m_pData = malloc(pSound->m_DataSize);
io_read(SoundFile, pSound->m_pData, pSound->m_DataSize);
}
io_close(SoundFile);
if(pSound->m_DataSize > 0)
if(pStorage->ReadFile(pName, IStorage::TYPE_ALL, &pSound->m_pData, &pSound->m_DataSize))
{
pSound->m_SoundID = m_pEditor->Sound()->LoadOpusFromMem(pSound->m_pData, pSound->m_DataSize, true);
}
}
}
else
{
pSound->m_DataSize = pItem->m_SoundDataSize;