driver: Merge code with audio.cpp

The driver is intended to be a small class, so put it in the audio code
now and we'll clean it up in future patches.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2014-12-19 14:37:47 -05:00
parent 1d2b52cc98
commit f20898b79c
5 changed files with 94 additions and 117 deletions

View File

@ -17,6 +17,24 @@ static unsigned int _pause_count = 0;
static Track *cur_track = NULL;
static File f_cur_track("cur_track", 0);
static Driver *cur_driver = NULL;
Driver :: Driver()
{
cur_driver = this;
}
Driver :: ~Driver()
{
cur_driver = NULL;
}
Driver *driver :: get_driver()
{
return cur_driver;
}
static void save_state()
{

View File

@ -1,49 +0,0 @@
/**
* @file
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/driver.h>
static Driver *cur_driver = NULL;
Driver :: Driver()
{
cur_driver = this;
}
Driver :: ~Driver()
{
cur_driver = NULL;
}
#ifdef CONFIG_TEST
TestDriver :: TestDriver() : playing(false), cur_pos(0), cur_duration(0) {}
TestDriver :: ~TestDriver() {}
void TestDriver :: init(int *argc, char ***argv, void (*eos_cb)(), void (*error_cb)())
{ on_eos = eos_cb; on_error = error_cb; }
void TestDriver :: load(const std::string &file)
{ cur_file = file; playing = false; cur_pos = 0; }
bool TestDriver :: play() { playing = true; return true; }
bool TestDriver :: pause() { playing = false; return true; }
bool TestDriver :: is_playing() { return playing; }
void TestDriver :: seek_to(long pos) { cur_pos = pos; }
long TestDriver :: position() { return cur_pos; }
long TestDriver :: duration() { return cur_duration; }
void TestDriver :: eos() { on_eos(); }
void TestDriver :: error() { on_error(); }
#else /* CONFIG_TEST */
#endif /* CONFIG_TEST */
Driver *driver :: get_driver()
{
return cur_driver;
}

View File

@ -30,5 +30,4 @@ test_env.UsePackage("taglib")
test( "library" )
test( "playlist" )
test( "deck" )
test( "driver" )
test( "audio" )

View File

@ -8,6 +8,80 @@
#include <tests/test.h>
Track *TRACK_NULL = NULL;
static unsigned int eos_count = 0;
static unsigned int error_count = 0;
TestDriver :: TestDriver() : playing(false), cur_pos(0), cur_duration(0) {}
TestDriver :: ~TestDriver() {}
void TestDriver :: init(int *argc, char ***argv, void (*eos_cb)(), void (*error_cb)())
{ on_eos = eos_cb; on_error = error_cb; }
void TestDriver :: load(const std::string &file)
{ cur_file = file; playing = false; cur_pos = 0; }
bool TestDriver :: play() { playing = true; return true; }
bool TestDriver :: pause() { playing = false; return true; }
bool TestDriver :: is_playing() { return playing; }
void TestDriver :: seek_to(long pos) { cur_pos = pos; }
long TestDriver :: position() { return cur_pos; }
long TestDriver :: duration() { return cur_duration; }
void TestDriver :: eos() { on_eos(); }
void TestDriver :: error() { on_error(); }
void on_eos()
{
eos_count++;
}
void on_error()
{
error_count++;
}
void test_driver()
{
test_equal(driver :: get_driver(), (Driver *)NULL);
TestDriver driver;
const std::string file = "/home/Zelda/Music/Wind Waker/1 - Outset Isle.ogg";
test_not_equal(driver :: get_driver(), (Driver *)NULL);
test_equal((Driver *)&driver, driver :: get_driver());
driver.init(0, NULL, on_eos, on_error);
driver.load(file);
test_equal(driver.cur_file, file);
test_equal(driver.play(), true);
test_equal(driver.playing, true);
test_equal(driver.is_playing(), true);
test_equal(driver.pause(), true);
test_equal(driver.playing, false);
test_equal(driver.is_playing(), false);
driver.seek_to(4242);
test_equal(driver.cur_pos, (long)4242);
test_equal(driver.position(), (long)4242);
driver.cur_duration = 424242;
test_equal(driver.duration(), (long)424242);
driver.eos();
test_equal(eos_count, (unsigned)1);
driver.error();
test_equal(error_count, (unsigned)1);
driver.play();
driver.seek_to(4242);
driver.load(file);
test_equal(driver.is_playing(), false);
test_equal(driver.position(), (long)0);
}
void test_pre_init()
{
@ -162,6 +236,8 @@ void test_autopause()
int main(int argc, char **argv)
{
run_test("Test Audio Driver", test_driver);
TestDriver driver;
run_test("Test Audio Pre-Init", test_pre_init);
run_test("Test Audio Init", test_init, argc, argv);

View File

@ -1,67 +0,0 @@
/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <core/driver.h>
#include <tests/test.h>
static unsigned int eos_count = 0;
static unsigned int error_count = 0;
void on_eos()
{
eos_count++;
}
void on_error()
{
error_count++;
}
void test_driver()
{
test_equal(driver :: get_driver(), (Driver *)NULL);
TestDriver driver;
const std::string file = "/home/Zelda/Music/Wind Waker/1 - Outset Isle.ogg";
test_not_equal(driver :: get_driver(), (Driver *)NULL);
test_equal((Driver *)&driver, driver :: get_driver());
driver.init(0, NULL, on_eos, on_error);
driver.load(file);
test_equal(driver.cur_file, file);
test_equal(driver.play(), true);
test_equal(driver.playing, true);
test_equal(driver.is_playing(), true);
test_equal(driver.pause(), true);
test_equal(driver.playing, false);
test_equal(driver.is_playing(), false);
driver.seek_to(4242);
test_equal(driver.cur_pos, (long)4242);
test_equal(driver.position(), (long)4242);
driver.cur_duration = 424242;
test_equal(driver.duration(), (long)424242);
driver.eos();
test_equal(eos_count, (unsigned)1);
driver.error();
test_equal(error_count, (unsigned)1);
driver.play();
driver.seek_to(4242);
driver.load(file);
test_equal(driver.is_playing(), false);
test_equal(driver.position(), (long)0);
}
int main(int argc, char **argv)
{
run_test("Test Audio Driver", test_driver);
return 0;
}