2017-03-10 21:26:02 +00:00
|
|
|
/*
|
|
|
|
** music_stream.cpp
|
2019-09-28 18:33:25 +00:00
|
|
|
** Plays a streaming song from a StreamSource
|
2017-03-10 21:26:02 +00:00
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2008 Randy Heit
|
2019-09-28 18:33:25 +00:00
|
|
|
** Copyright 2019 Christoph Oelckers
|
2017-03-10 21:26:02 +00:00
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2019-09-29 23:34:16 +00:00
|
|
|
#include "zmusic/musinfo.h"
|
2020-01-02 00:26:01 +00:00
|
|
|
#include "zmusic/zmusic_internal.h"
|
2019-09-29 11:33:09 +00:00
|
|
|
#include "streamsources/streamsource.h"
|
2017-03-10 21:26:02 +00:00
|
|
|
|
2019-09-28 18:33:25 +00:00
|
|
|
class StreamSong : public MusInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StreamSong (StreamSource *source);
|
|
|
|
~StreamSong ();
|
|
|
|
void Play (bool looping, int subsong) override;
|
|
|
|
void Pause () override;
|
|
|
|
void Resume () override;
|
|
|
|
void Stop () override;
|
|
|
|
bool IsPlaying () override;
|
2019-09-29 22:02:31 +00:00
|
|
|
bool IsValid () const override { return m_Source != nullptr; }
|
2019-09-28 18:33:25 +00:00
|
|
|
bool SetPosition (unsigned int pos) override;
|
|
|
|
bool SetSubsong (int subsong) override;
|
2019-09-29 23:34:16 +00:00
|
|
|
std::string GetStats() override;
|
2019-09-28 18:33:25 +00:00
|
|
|
void ChangeSettingInt(const char *name, int value) override { if (m_Source) m_Source->ChangeSettingInt(name, value); }
|
|
|
|
void ChangeSettingNum(const char *name, double value) override { if (m_Source) m_Source->ChangeSettingNum(name, value); }
|
|
|
|
void ChangeSettingString(const char *name, const char *value) override { if(m_Source) m_Source->ChangeSettingString(name, value); }
|
2019-09-29 22:02:31 +00:00
|
|
|
bool ServiceStream(void* buff, int len) override;
|
|
|
|
SoundStreamInfo GetStreamInfo() const override { return m_Source->GetFormat(); }
|
2019-09-28 18:33:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
StreamSource *m_Source = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-03-10 21:26:02 +00:00
|
|
|
void StreamSong::Play (bool looping, int subsong)
|
|
|
|
{
|
|
|
|
m_Status = STATE_Stopped;
|
|
|
|
m_Looping = looping;
|
|
|
|
|
2019-09-29 22:02:31 +00:00
|
|
|
if (m_Source != nullptr)
|
2017-03-10 21:26:02 +00:00
|
|
|
{
|
2019-09-28 18:33:25 +00:00
|
|
|
m_Source->SetPlayMode(looping);
|
|
|
|
m_Source->SetSubsong(subsong);
|
|
|
|
if (m_Source->Start())
|
2017-03-10 21:26:02 +00:00
|
|
|
{
|
2019-09-28 18:33:25 +00:00
|
|
|
m_Status = STATE_Playing;
|
2017-03-10 21:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamSong::Pause ()
|
|
|
|
{
|
2019-09-29 22:02:31 +00:00
|
|
|
m_Status = STATE_Paused;
|
2017-03-10 21:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamSong::Resume ()
|
|
|
|
{
|
2019-09-29 22:02:31 +00:00
|
|
|
m_Status = STATE_Playing;
|
2017-03-10 21:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamSong::Stop ()
|
|
|
|
{
|
|
|
|
m_Status = STATE_Stopped;
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamSong::~StreamSong ()
|
|
|
|
{
|
|
|
|
Stop ();
|
2019-09-28 18:33:25 +00:00
|
|
|
if (m_Source != nullptr)
|
|
|
|
{
|
|
|
|
delete m_Source;
|
|
|
|
m_Source = nullptr;
|
2017-03-10 21:26:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-28 18:33:25 +00:00
|
|
|
StreamSong::StreamSong (StreamSource *source)
|
2017-03-10 21:26:02 +00:00
|
|
|
{
|
2019-09-28 18:33:25 +00:00
|
|
|
m_Source = source;
|
2017-03-10 21:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StreamSong::IsPlaying ()
|
|
|
|
{
|
|
|
|
if (m_Status != STATE_Stopped)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// StreamSong :: SetPosition
|
|
|
|
//
|
|
|
|
// Sets the position in ms.
|
|
|
|
|
|
|
|
bool StreamSong::SetPosition(unsigned int pos)
|
|
|
|
{
|
2019-09-28 18:33:25 +00:00
|
|
|
if (m_Source != nullptr)
|
2017-03-10 21:26:02 +00:00
|
|
|
{
|
2019-09-28 18:33:25 +00:00
|
|
|
return m_Source->SetPosition(pos);
|
2017-03-10 21:26:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StreamSong::SetSubsong(int subsong)
|
|
|
|
{
|
2019-09-29 22:02:31 +00:00
|
|
|
return m_Source->SetSubsong(subsong);
|
2017-03-10 21:26:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 23:34:16 +00:00
|
|
|
std::string StreamSong::GetStats()
|
2017-03-10 21:26:02 +00:00
|
|
|
{
|
2019-09-29 23:34:16 +00:00
|
|
|
std::string s1, s2;
|
2019-09-28 18:33:25 +00:00
|
|
|
if (m_Source != NULL)
|
|
|
|
{
|
2019-09-28 22:41:13 +00:00
|
|
|
auto stat = m_Source->GetStats();
|
|
|
|
s2 = stat.c_str();
|
2019-09-28 18:33:25 +00:00
|
|
|
}
|
2019-09-29 23:34:16 +00:00
|
|
|
if (s1.empty() && s2.empty()) return "No song loaded\n";
|
|
|
|
if (s1.empty()) return s2;
|
|
|
|
if (s2.empty()) return s1;
|
|
|
|
return s1 + "\n" + s2;
|
2019-09-28 18:33:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-29 22:02:31 +00:00
|
|
|
bool StreamSong::ServiceStream (void *buff, int len)
|
2019-09-28 18:33:25 +00:00
|
|
|
{
|
2019-09-29 22:02:31 +00:00
|
|
|
bool written = m_Source->GetData(buff, len);
|
2019-09-28 18:33:25 +00:00
|
|
|
if (!written)
|
|
|
|
{
|
2020-02-08 09:25:44 +00:00
|
|
|
m_Status = STATE_Stopped;
|
2019-09-28 18:33:25 +00:00
|
|
|
memset((char*)buff, 0, len);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MusInfo *OpenStreamSong(StreamSource *source)
|
|
|
|
{
|
|
|
|
auto song = new StreamSong(source);
|
|
|
|
if (song->IsValid()) return song;
|
|
|
|
delete song;
|
|
|
|
return nullptr;
|
2017-03-10 21:26:02 +00:00
|
|
|
}
|
2019-09-28 18:33:25 +00:00
|
|
|
|