void BlockFileOutputStreamTestCase::testWriteFromInputStream()
{
    const static size_t MAX_TEST_DATA = 10000;
    for (size_t i = 1; i < MAX_TEST_DATA; i += 31)
    {
        OutputStreamPtr pOutput = m_pBlockFileSystem->createFile("testfile1");

        string answer;
        makeData(answer, *pOutput, i);
        pOutput.reset();
        
        InputStreamPtr pInput = m_pBlockFileSystem->openFile("testfile1");

        pOutput = m_pBlockFileSystem->createFile("testfile2");
        pOutput->write(*pInput);
        pOutput.reset();

        pInput = m_pBlockFileSystem->openFile("testfile2");
        
        string expStr;
        expStr.resize(i);
        pInput->read((void*)expStr.c_str(), i);
        assert(answer==expStr);        
        CPPUNIT_ASSERT_EQUAL(answer, expStr);
    }
}
void BlockFileInputStreamTestCase::testSeekInBuffer()
{
    OutputStreamPtr pOutput = m_pBlockFileSystem->createFile("testfile1");

    size_t dataSize = 10000;
    stringstream content;
    for (size_t i = 0; i < dataSize; ++i)
    {
        content << (i % 10);
    }
    pOutput->write((const void*)content.str().c_str(), content.str().size());
    pOutput.reset();

    InputStreamPtr pInput = m_pBlockFileSystem->openFile("testfile1");
    CPPUNIT_ASSERT(pInput != NULL);   

    CPPUNIT_ASSERT_EQUAL(dataSize, (size_t)pInput->getSize());   
    off_t off = 100;
    string readCont;
    readCont.resize((size_t)off);
    pInput->read((void*)readCont.c_str(), (size_t)off);
    pInput->seek(off);

    size_t readSize = 1000;
    readCont.resize(readSize);
    pInput->read((void*)readCont.c_str(), readSize);
    
    string expStr = content.str().substr((size_t)off, readSize);
    CPPUNIT_ASSERT_EQUAL(expStr, readCont);

    pInput.reset();
}
void AudiereMusicInterface::PlayMusic(int theSongId, int theOffset, bool noLoop)
{
    AudiereMusicMap::iterator anItr = mMusicMap.find(theSongId);
    if (anItr != mMusicMap.end())
    {
        AudiereMusicInfo* aMusicInfo = &anItr->second;
        aMusicInfo->mVolume = aMusicInfo->mVolumeCap;
        aMusicInfo->mVolumeAdd = 0.0f;

        if (aMusicInfo->mStream) {
            OutputStreamPtr aStream = aMusicInfo->mStream;
            aStream->setVolume(float(mMasterVolume * aMusicInfo->mVolume));
            aStream->setRepeat(!noLoop);
            if (theOffset != 0)
                aStream->setPosition(theOffset);
            aStream->play();
        }
        else if (aMusicInfo->mMIDIStream) {
            MIDIStreamPtr aStream = aMusicInfo->mMIDIStream;
            aStream->setRepeat(!noLoop);
            if (theOffset != 0)
                aStream->setPosition(theOffset);
            aStream->play();
        }
    }
}
Exemple #4
0
void 
Ice::Object::__write(const OutputStreamPtr& os) const
{
    os->startObject(0);
    __writeImpl(os);
    os->endObject();
}
Exemple #5
0
int main(int argc, const char** argv) {
  const char* device_name = "";
  if (argc >= 2) {
    device_name = argv[1];
  }

  AudioDevicePtr device(OpenDevice(device_name));
  if (!device) {
    cerr << "OpenDevice() failed" << endl;
    return EXIT_FAILURE;
  }

  OutputStreamPtr tone = device->openStream(CreateTone(440));
  if (!tone) {
    cerr << "openStream() failed" << endl;
    return EXIT_FAILURE;
  }
  tone->play();

  clock_t start = clock();
  for (int i = 0; i < 100000; ++i) {
    assert(tone->isPlaying());
  }
  clock_t end = clock();

  double duration = double(end - start) / CLOCKS_PER_SEC;
  cout << "Total time for 100,000 isPlaying() calls = "
       << duration << " seconds" << endl;

  cout << "That is " << duration / 100000 << " seconds per call." << endl;

  return EXIT_SUCCESS;
}
void BlockFileInputStreamTestCase::testSeekAndRead()
{
    OutputStreamPtr pOutput = m_pBlockFileSystem->createFile("testfile1");

    size_t dataSize = 10000;
    stringstream content;
    for (size_t i = 0; i < dataSize; ++i)
    {
        content << (i % 10);
    }
    pOutput->write((const void*)content.str().c_str(), content.str().size());
    pOutput.reset();

    InputStreamPtr pInput = m_pBlockFileSystem->openFile("testfile1");
    CPPUNIT_ASSERT(pInput != NULL);   

    CPPUNIT_ASSERT_EQUAL(dataSize, (size_t)pInput->getSize());   

    for (size_t i = 0; i < dataSize - 1; ++i)
    {
        string readCont;
        readCont.resize(dataSize - i);
        if (i % 147 == 0)
        {
            pInput->seek(i % 789);
        }
        pInput->seekAndRead(i, (void*)readCont.c_str(), dataSize - i);

        string expStr = content.str().substr(i);
//        assert(expStr == readCont);
        CPPUNIT_ASSERT_EQUAL(expStr, readCont);
    }
    pInput.reset();
}
void BlockFileInputStreamTestCase::testReadInt()
{
    OutputStreamPtr pOutput = m_pBlockFileSystem->createFile("testfile1");
    uint8_t ui8v =81;
    int32_t i32v = 321;
    int64_t i64v = ((int64_t)1 << 32) + 641;

    string str(129, '1');
    
    pOutput->writeByte(ui8v);
    pOutput->writeInt32(i32v);
    pOutput->write(str.c_str(), str.size());
    pOutput->writeVInt32(i32v);
    pOutput->writeInt64(i64v);
    pOutput->writeVInt64(i64v);
    pOutput.reset();

    InputStreamPtr pInput = m_pBlockFileSystem->openFile("testfile1");
    CPPUNIT_ASSERT(pInput);

    string expStr;
    expStr.resize(str.size());

    CPPUNIT_ASSERT_EQUAL(ui8v, pInput->readByte());
    CPPUNIT_ASSERT_EQUAL(i32v, pInput->readInt32());

    pInput->read((void*)expStr.c_str(), str.size());
    CPPUNIT_ASSERT_EQUAL(str, expStr);

    CPPUNIT_ASSERT_EQUAL(i32v, pInput->readVInt32());
    CPPUNIT_ASSERT_EQUAL(i64v, pInput->readInt64());
    CPPUNIT_ASSERT_EQUAL(i64v, pInput->readVInt64());
}
Exemple #8
0
void PlaySong(const std::string& filename)
{
	// Since this file is background music, we don't need to load the
	// whole thing into memory.
	std::string soundpath = FilePathMgr::Instance().GetSoundPath() + filename;
	g_currSong = OpenSound(g_device, soundpath.c_str(), true);
	if (!g_currSong)
	{
		assert(false);
	}

	// Great, we have some opened streams!  What do we do with them?
	// let's start the background music first
	g_currSong->setRepeat(true);
	g_currSong->setVolume(g_volume);
	g_currSong->play();	
}
Exemple #9
0
int main(int argc, const char** argv) {

  if (argc != 2 && argc != 3) {
    cerr << "usage: simple <filename> [<device>]" << endl;
    return EXIT_FAILURE;
  }

  cerr << "initializing..." << endl;

  const char* device_name = "";
  if (argc == 3) {
    device_name = argv[2];
  }

  AudioDevicePtr device = OpenDevice(device_name);
  if (!device) {
    cerr << "OpenDevice() failed" << endl;
    return EXIT_FAILURE;
  }

  cerr << "opened device" << endl;

  OutputStreamPtr sound = OpenSound(device, argv[1]);
  if (!sound) {
    cerr << "OpenSound() failed" << endl;
    return EXIT_FAILURE;
  }

  cerr << "opened sound" << endl;

  sound->play();

  cerr << "started playback" << endl;
  while (sound->isPlaying()) {
    sleepSecond();
    if (sound->isSeekable()) {
      cerr << "position: " << sound->getPosition() << endl;
    }
  }

  return EXIT_SUCCESS;
}
Exemple #10
0
void Config::SaveConfiguration(const URL& url) const {
	NEX_THREAD_LOCK_GUARD_MUTEX(contentLock);
	FileSystem& fileSys = FileSystem::Instance();
	OutputStreamPtr output = fileSys.OpenWrite(url);
	if (output) {
		String sectionName;
		for (SectionMap::const_iterator it = sections.begin();
				it != sections.end(); ++it) {
			const String& name = (*it).first;
			sectionName = "[" + name + "]\n";
			output->Write(sectionName.c_str(), sectionName.length());
			const NameValueMap& vnMap = (*it).second;
			for (NameValueMap::const_iterator nvIt = vnMap.begin();
					nvIt != vnMap.end(); ++nvIt) {
				String line = (*nvIt).first + " = " + (*nvIt).second + "\n";
				output->Write(line.c_str(), line.length());
			}
		}
	}
}
void BlockFileOutputStreamTestCase::testSeekAndWrite()
{
    size_t dataSize = 10000;
    stringstream content;
    for (size_t i = 0; i < dataSize; ++i)
    {
        content << (i % 10);
    }

    for (size_t i = 0; i < dataSize - 1; ++i)
    {
        OutputStreamPtr pOutput = m_pBlockFileSystem->createFile("testfile1");
        pOutput->write((const void*) content.str().c_str(), i);
        if (i % 137)
        {
            pOutput->write((const void*) content.str().c_str(), i % dataSize);
        }
        pOutput->seek(i);
        pOutput->write((const void*) (content.str().c_str() + i), dataSize - i);
        pOutput->close();

        InputStreamPtr pInput = m_pBlockFileSystem->openFile("testfile1");
        string readCont;
        readCont.resize(dataSize);
        pInput->read((void*)readCont.c_str(), dataSize);

        CPPUNIT_ASSERT_EQUAL(content.str(), readCont);
    }
}
void XMLConfigurator::save(const string& sCfgFile, FileSystemPtr& pFileSys)
{
    XMLDocumentWrapper xmlDoc;
    XMLNodeWrapperPtr pRoot = xmlDoc.appendNode(
            XMLDocumentWrapper::NODE_ELEMENT, CONFIGURE_TAG_NAME);
    Iterator it = iterator();
    while (it.hasNext())
    {
        Configurator::KeyValuePair kv = it.next();

        if (kv.second.getType() == typeid(Configurator::ConfMap))
        {
            XMLNodeWrapperPtr pNode = xmlDoc.allocateNode(
                    XMLDocumentWrapper::NODE_ELEMENT, kv.first.c_str());
            save(xmlDoc, pNode, AnyCast<Configurator::ConfMap>(kv.second));
            pRoot->appendNode(pNode);
        }
        else 
        {
            string str = AnyCast<string>(kv.second);
            XMLNodeWrapperPtr pNode = xmlDoc.allocateNode(
                    XMLDocumentWrapper::NODE_ELEMENT, kv.first.c_str(), str);
            pRoot->appendNode(pNode);
        }
    }

    OutputStreamPtr pOutStream = pFileSys->createFile(sCfgFile);
    if (pOutStream.isNull())
    {
        FIRTEX_THROW(FileIOException, "Save configure [%s] FAILED ",
                     sCfgFile.c_str());
    }
    string str;
    xmlDoc.addDeclarationNode("UTF-8");
    xmlDoc.print(str);
    pOutStream->write((const void*)str.c_str(), str.length());
}
Exemple #13
0
void PNGImageCodec::Save(OutputStreamPtr& file, const ImageParams& params,
		const ImageData& data) {
	OutputStream* writr = file.GetPtr();
	if (data.format != PixelFormat::BGRA8 && data.format != PixelFormat::RGBA8)
		// todo support formats like rgba16 etc.
		NEX_THROW_FatalError(EXCEPT_NOT_IMPLEMENTED);
	png_structp pngPtr = nullptr;
	png_infop infoPtr = nullptr;
	png_byte ** rowPointers = nullptr;

	// todo Fix for 64
	uint32 bitDepth = 32;
	size_t rowStride = data.width * bitDepth;
	bool flip = (data.format == PixelFormat::BGRA8);

	pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
	if (pngPtr == nullptr)
		goto failed;
	infoPtr = png_create_info_struct(pngPtr);
	if (infoPtr == nullptr)
		goto failed;

	png_set_error_fn(pngPtr, (void *) (writr), PngWarn, PngWarn);
	png_set_write_fn(pngPtr, (void *) (writr), PngWriteFile, PngFlushFile);

	png_set_IHDR(pngPtr, infoPtr, data.width, data.height, bitDepth, // assumed
			PNG_COLOR_TYPE_RGB_ALPHA,
			PNG_INTERLACE_NONE,
			PNG_COMPRESSION_TYPE_DEFAULT,
			PNG_FILTER_TYPE_DEFAULT);
	rowPointers = (png_byte**) NEX_ALLOC(sizeof(png_byte*) * data.height,
			MEMCAT_GENERAL);

	for (uint32 i = 0; i < data.height; ++i) {
		rowPointers[i] = (static_cast<uint8*>(data.data)) + i * rowStride;
	}
	png_set_rows(pngPtr, infoPtr, rowPointers);
	png_write_png(pngPtr, infoPtr,
			flip ? PNG_TRANSFORM_BGR : PNG_TRANSFORM_IDENTITY, NULL);

	failed: if (rowPointers)
		NEX_FREE(rowPointers, MEMCAT_GENERAL);
	if (pngPtr)
		png_destroy_write_struct(&pngPtr, &infoPtr);
}
void SmtpSendMailCommand::WriteEmail()
{
	try {
		switch (m_write_state) {
		case State_SendMailFrom:
		{
			MojLogInfo(m_log, "Sending MAIL FROM command");
			//MojLogInfo(m_log, "From address is %s\n", m_email.GetFrom()->GetAddress().c_str());

			std::string userCommand = std::string(FROM_COMMAND_STRING) + " <" + m_email.GetFrom()->GetAddress() + ">";
		
			// If SIZE extension is present, place message size on FROM line so that the server
			// can potentially reject it now, instead of after we transmit the data.
			if (m_session.GetSizeMax()) {
				char buf[64];
				memset(buf, '\0', sizeof(buf));
				snprintf(buf, sizeof(buf)-1, " SIZE=%d", m_bytesLeft);
				userCommand += buf;

				MojLogInfo(m_log, "...with SIZE");
			}
		
			SendCommand(userCommand, SmtpSession::TIMEOUT_MAIL_FROM);
			break;
		}
		case State_SendRcptTo:
		{
			MojLogInfo(m_log, "Sending RCPT TO command");
			//MojLogInfo(m_log, "To address is %s\n", m_toAddress[m_toIdx].c_str());

			std::string userCommand = std::string(TO_COMMAND_STRING) + " <" + m_toAddress[m_toIdx] + ">";
			SendCommand(userCommand, SmtpSession::TIMEOUT_RCPT_TO);
			break;
		}
		case State_SendData:
		{
			MojLogInfo(m_log, "Sending DATA command");
		
			std::string userCommand = DATA_COMMAND_STRING;
			SendCommand(userCommand, SmtpSession::TIMEOUT_DATA_INITIATION);
			break;
		}
		case State_SendBody:
		{
			MojLogInfo(m_log, "Sending BODY");
		
			assert(m_emailWriter.get());

			OutputStreamPtr outputStream = m_session.GetConnection()->GetOutputStream();
		
			// Wrap the mail writer in a CRLF fixer stream, which ensures that the stream ends with 
			// CRLF, so we can safely write out a DOT to end the body, without the risk that it'll end
			// up on the end of a body line.
			m_crlfTerminator.reset( new CRLFTerminatedOutputStream(outputStream) );

			// connect mail writer
			m_emailWriter->SetOutputStream(m_crlfTerminator);
		
			// FIXME: set up timeout on writing email body blocks
			//m_emailWriter->SetDataTimeout(SmtpSession::TIMEOUT_DATA_BLOCK);

			// Set up flow control
			AsyncOutputStream* asyncOs = dynamic_cast<AsyncOutputStream*>(outputStream.get());
			if(asyncOs) {
				m_flowControl.reset(new AsyncFlowControl(m_emailWriter, asyncOs));
			}
	
			// Send the DOT after the body is written.
			m_emailWriter->WriteEmail(m_writeDoneSlot);
			break;
		}
		case State_SendRset:
		{
			MojLogInfo(m_log, "Sending RSET");
		
			std::string userCommand = RESET_COMMAND_STRING;
			SendCommand(userCommand);
			break;
		}
		case State_SendErrorRset:
		{
			MojLogInfo(m_log, "Sending RSET after error");
		
			std::string userCommand = RESET_COMMAND_STRING;
			SendCommand(userCommand);
			break;
		}
		default:
		{
			MojLogInfo(m_log, "Unknown SmtpSendMailCommand::WriteEmail write mail state %d", m_write_state);
			HandleFailure("bad WriteEmail state");
			break;
		}
		}

	} catch(const std::exception& e) {
		HandleException(e, __func__, __FILE__, __LINE__);
	} catch(...) {
		HandleUnknownException();
	}
}
Exemple #15
0
void
Ice::ice_writeObject(const OutputStreamPtr& out, const ObjectPtr& p)
{
    out->write(p);
}
Exemple #16
0
namespace AudioMgr
{
using namespace audiere;	

//private vars: static globals, not accessible externally
static OutputStreamPtr g_currSong;
static SoundEffectPtr g_soundEffect;
static AudioDevicePtr g_device;

static float g_volume = 0.5f;

class GameSFX
{
public:
	GameSFX(const std::string& filename, const int delay = 0) :
		m_filename(filename),
		m_delay(delay),
		m_age(0),
		m_bExpired(false)
	{
		std::string soundpath = FilePathMgr::Instance().GetSoundPath() + filename;
		m_sfx = OpenSoundEffect(g_device, soundpath.c_str(), MULTIPLE);
		if (!m_sfx)
			assert(false);
		m_sfx->setVolume(g_volume);				
	}

	bool Update(const double ticks)
	{
		m_age += static_cast<int>(ticks);
		if ((m_age >= m_delay) && (!m_bExpired))
		{
			m_sfx->play();
			m_bExpired = true;
		}
		return m_bExpired;
	}

	bool IsExpired()
	{
		return m_bExpired;
	}

private:
	std::string m_filename;
	double m_delay;
	double m_age;
	SoundEffectPtr m_sfx;
	bool m_bExpired;
};

static std::vector<GameSFX*> g_sfxVector;

void Init()
{
	g_device = OpenDevice();
	if (!g_device)
	{
		assert(false);
	}

	std::string soundpath = FilePathMgr::Instance().GetSoundPath() + "swish.ogg";
	g_soundEffect = OpenSoundEffect(g_device, soundpath.c_str(), MULTIPLE);
	if (!g_soundEffect) 
	{
		//failure
		assert(false);
	}
	g_soundEffect->setVolume(g_volume);
}

void Shutdown()
{
	g_sfxVector.clear();
}

void Update(const double ticks)
{
	// update everything front to back, play whatever is done waiting
	for (unsigned int i = 0; i < g_sfxVector.size(); ++i)
	{
		g_sfxVector[i]->Update(ticks);
	}

	// crawl back to front and delete anything that is expired
/*	for (unsigned int i = g_sfxVector.size(); i > 0; --i)
	{
		if (g_sfxVector[i]->IsExpired())
		{
			g_sfxVector.erase(i);
		}
	}
*/
}

void PlaySong(const std::string& filename)
{
	// Since this file is background music, we don't need to load the
	// whole thing into memory.
	std::string soundpath = FilePathMgr::Instance().GetSoundPath() + filename;
	g_currSong = OpenSound(g_device, soundpath.c_str(), true);
	if (!g_currSong)
	{
		assert(false);
	}

	// Great, we have some opened streams!  What do we do with them?
	// let's start the background music first
	g_currSong->setRepeat(true);
	g_currSong->setVolume(g_volume);
	g_currSong->play();	
}

void StopSong()
{
	g_currSong->stop();
}

void LoopSong(const bool loop)
{
	g_currSong->setRepeat(loop);
}

void PlaySoundEffect(const std::string& filename, const int delay)
{
	g_sfxVector.push_back(new GameSFX(filename, delay));
}

void PlaySwishSound()
{
	g_soundEffect->play();
}

}
Exemple #17
0
void LoopSong(const bool loop)
{
	g_currSong->setRepeat(loop);
}
Exemple #18
0
void StopSong()
{
	g_currSong->stop();
}