TEST_F(ReadAheadSampleBufferTest, emptyWithCapacity) { mixxx::ReadAheadSampleBuffer sampleBuffer(kCapacity); EXPECT_TRUE(sampleBuffer.empty()); sampleBuffer.clear(); EXPECT_TRUE(sampleBuffer.empty()); }
TEST_F(ReadAheadSampleBufferTest, shrink) { mixxx::ReadAheadSampleBuffer sampleBuffer(kCapacity); SINT writeCount1 = growAndWrite(&sampleBuffer, kCapacity - 10); EXPECT_EQ(writeCount1, kCapacity - 10); EXPECT_FALSE(sampleBuffer.empty()); SINT shrinkCount1 = shrinkForReadingAndVerify(&sampleBuffer, 10); EXPECT_EQ(shrinkCount1, 10); SINT readCount1 = shrinkAfterWritingAndVerify(&sampleBuffer, 10); EXPECT_EQ(readCount1, 10); EXPECT_FALSE(sampleBuffer.empty()); SINT readCount2 = shrinkAfterWritingAndVerify(&sampleBuffer, kCapacity - 40); EXPECT_EQ(readCount2, kCapacity - 40); EXPECT_FALSE(sampleBuffer.empty()); SINT readCount3 = shrinkAfterWritingAndVerify(&sampleBuffer, 20); EXPECT_EQ(readCount3, 10); EXPECT_TRUE(sampleBuffer.empty()); SINT writeCount2 = growAndWrite(&sampleBuffer, 20); EXPECT_EQ(writeCount2, 20); EXPECT_FALSE(sampleBuffer.empty()); SINT shrinkCount2 = shrinkForReadingAndVerify(&sampleBuffer, 21); EXPECT_EQ(shrinkCount2, 20); EXPECT_TRUE(sampleBuffer.empty()); }
bool WaveReader::DecodeAudioData() { MOZ_ASSERT(OnTaskQueue()); int64_t pos = GetPosition() - mWavePCMOffset; int64_t len = GetDataLength(); int64_t remaining = len - pos; NS_ASSERTION(remaining >= 0, "Current wave position is greater than wave file length"); static const int64_t BLOCK_SIZE = 4096; int64_t readSize = std::min(BLOCK_SIZE, remaining); int64_t frames = readSize / mFrameSize; static_assert(uint64_t(BLOCK_SIZE) < UINT_MAX / sizeof(AudioDataValue) / MAX_CHANNELS, "bufferSize calculation could overflow."); const size_t bufferSize = static_cast<size_t>(frames * mChannels); nsAutoArrayPtr<AudioDataValue> sampleBuffer(new AudioDataValue[bufferSize]); static_assert(uint64_t(BLOCK_SIZE) < UINT_MAX / sizeof(char), "BLOCK_SIZE too large for enumerator."); nsAutoArrayPtr<char> dataBuffer(new char[static_cast<size_t>(readSize)]); if (!ReadAll(dataBuffer, readSize)) { return false; } // convert data to samples const char* d = dataBuffer.get(); AudioDataValue* s = sampleBuffer.get(); for (int i = 0; i < frames; ++i) { for (unsigned int j = 0; j < mChannels; ++j) { if (mSampleFormat == FORMAT_U8) { uint8_t v = ReadUint8(&d); *s++ = UnsignedByteToAudioSample<AudioDataValue>(v); } else if (mSampleFormat == FORMAT_S16) { int16_t v = ReadInt16LE(&d); *s++ = SignedShortToAudioSample<AudioDataValue>(v); } } } double posTime = BytesToTime(pos); double readSizeTime = BytesToTime(readSize); NS_ASSERTION(posTime <= INT64_MAX / USECS_PER_S, "posTime overflow"); NS_ASSERTION(readSizeTime <= INT64_MAX / USECS_PER_S, "readSizeTime overflow"); NS_ASSERTION(frames < INT32_MAX, "frames overflow"); mAudioQueue.Push(new AudioData(pos, static_cast<int64_t>(posTime * USECS_PER_S), static_cast<int64_t>(readSizeTime * USECS_PER_S), static_cast<int32_t>(frames), sampleBuffer.forget(), mChannels, mSampleRate)); return true; }
QSampleBuffer QSampleBuffer::createMemoryAudioBuffer(int size) { if(size < 1) { size = 1; } QSampleBuffer sampleBuffer(AudioBuffer, size, (void*)new jack_default_audio_sample_t[size]); sampleBuffer._isMemoryBuffer = true; return sampleBuffer; }
TEST_F(ReadAheadSampleBufferTest, clear) { mixxx::ReadAheadSampleBuffer sampleBuffer(kCapacity); SINT writeCount = growAndWrite(&sampleBuffer, 10); EXPECT_EQ(writeCount, 10); EXPECT_FALSE(sampleBuffer.empty()); clear(&sampleBuffer); EXPECT_TRUE(sampleBuffer.empty()); SINT readCount = shrinkAfterWritingAndVerify(&sampleBuffer, 10); EXPECT_EQ(readCount, 0); EXPECT_TRUE(sampleBuffer.empty()); }
void GUI::loadSample(std::string str) { string filePath = Glib::build_filename( Glib::get_current_dir(), "samples", str ); SndfileHandle infile( filePath, SFM_READ ); if ( infile.frames() == 0 ) { cout << "Fileselector::on_button_press_event() Infile.frames == 0" << endl; return; } std::vector<float> sampleBuffer( infile.frames(), 0.0 ); top->getGUI().printTextView("Created vector buffer, reading data in..."); infile.read( &sampleBuffer.at(0) , infile.frames() ); top->getGUI().printTextView( "Creating Audiobuffer instance..." ); // Create a new AudioBuffer boost::shared_ptr<AudioBuffer> tmp = boost::shared_ptr<AudioBuffer>( new AudioBuffer() ); top->getGUI().printTextView( "Swapping buffers..." ); // swaps the contents of the vectors, audiobuffer will now have the // loaded sample data tmp->nonRtSetSample( sampleBuffer ); cout << "Adding instance to Shared..." << endl; Shared::get()->addAudio( tmp ); // update the waveform top->getGUI().getWaveview()->setPlaybackBuffer( tmp ); // send the Buffer to DSP: if( top->getGuiToDsp()->can_write() ) { top->getGuiToDsp()->write( boost::bind( &DSP::addBuffer, &top->getDSP(), tmp ) ); } // Manually destroy the local instance of the shared pointer. This is so that // the use count isn't one higher than it should be. Logically the local // scoped shared_ptr<> should -1 from the use_count when it goes out of scope, // but it doesn't unless we manually destroy it. // That is why we have the manual destructor call here. tmp.~shared_ptr<AudioBuffer>(); }
void SampleOntoNrrd::Sample(boost::shared_ptr<SceneView> view, const std::string& filePrefix, ElVisFloat targeth) { if( !Program.IsValid() ) { Program = view->AddRayGenerationProgram("SampleOntoNrrd"); } ElVisFloat3 h = MakeFloat3(targeth, targeth, targeth); int3 n; view->GetScene()->GetModel()->CalculateExtents(); WorldPoint minExtent = view->GetScene()->GetModel()->MinExtent(); WorldPoint maxExtent = view->GetScene()->GetModel()->MaxExtent(); ElVisFloat3 d = MakeFloat3(maxExtent.x() - minExtent.x(), maxExtent.y() - minExtent.y(), maxExtent.z() - minExtent.z()); n.x = d.x/h.x + 1; n.y = d.y/h.y + 1; n.z = d.z/h.z + 1; h.x = d.x/(n.x-1); h.y = d.y/(n.y-1); h.z = d.z/(n.z-1); std::cout << "Samples (" << h.x << ", " << h.y << ", " << h.z << ")" << std::endl; std::cout << "Samples per direction (" << n.x << ", " << n.y << ", " << n.z << ")" << std::endl; std::string nrrdHeaderFileName = filePrefix + ".nhdr"; std::string nrrdDataFileName = filePrefix + ".raw"; boost::filesystem::path p(nrrdDataFileName); std::ofstream nrrdHeaderFile(nrrdHeaderFileName.c_str(), std::ios::out); FILE* nrrdDataFile = fopen(nrrdDataFileName.c_str(), "wb"); nrrdHeaderFile << "NRRD0001" << std::endl; nrrdHeaderFile << "type: float" << std::endl; nrrdHeaderFile << "dimension: 3" << std::endl; nrrdHeaderFile << "sizes: " << n.x << " " << n.y << " " << n.z << std::endl; nrrdHeaderFile << "encoding: raw" << std::endl; nrrdHeaderFile << "datafile: ./" << p.filename().string() << std::endl; nrrdHeaderFile << "endian: little" << std::endl; nrrdHeaderFile << "space dimension: 3" << std::endl; nrrdHeaderFile << "space directions: " << "(" << h.x << ",0,0) " << "(0," << h.y << ",0) " << "(0,0," << h.z << ")" << std::endl; nrrdHeaderFile << "space origin: (" << minExtent.x() << ", " << minExtent.y() << ", " << minExtent.z() << ")" << std::endl; nrrdHeaderFile << std::endl; nrrdHeaderFile.close(); optixu::Context context = view->GetContext(); SetFloat(context["SampleOntoNrrdH"], h); int bufferSize = n.x*n.y; OptiXBuffer<ElVisFloat> sampleBuffer("SampleOntoNrrdSamples"); sampleBuffer.SetContext(context); sampleBuffer.SetDimensions(n.x, n.y); float* convertBuffer = new float[n.x*n.y]; SetFloat(context["SampleOntoNrrdMinExtent"], minExtent); SetFloat(context["SampleOntoNrrdMissValue"], std::numeric_limits<ElVisFloat>::signaling_NaN()); std::cout << "Validating and compiling." << std::endl; context->validate(); context->compile(); std::cout << "Done validating and compiling." << std::endl; try { for(int i = 0; i < n.z; ++i) { context["SampleOntoNrrdPlane"]->setInt(i); std::cout << "Sampling " << i << " of " << n.z-1 << std::endl; context->launch(Program.Index, n.x, n.y); std::cout << "Done sampling." << std::endl; BOOST_AUTO(data, sampleBuffer.Map()); for(unsigned int i = 0; i < n.x*n.y; ++i) { convertBuffer[i] = data[i]; } fwrite(convertBuffer, sizeof(float), bufferSize, nrrdDataFile); } } catch(optixu::Exception& e) { std::cout << "Exception encountered rendering primary rays." << std::endl; std::cerr << e.getErrorString() << std::endl; std::cout << e.getErrorString().c_str() << std::endl; } catch(std::exception& e) { std::cout << "Exception encountered rendering primary rays." << std::endl; std::cout << e.what() << std::endl; } catch(...) { std::cout << "Exception encountered rendering primary rays." << std::endl; } delete [] convertBuffer; fclose(nrrdDataFile); }
TEST_F(ReadAheadSampleBufferTest, readWriteTrim) { mixxx::ReadAheadSampleBuffer sampleBuffer(kCapacity); SINT writeCount1 = growAndWrite(&sampleBuffer, kCapacity + 10); // Buffer is completely filled with samples EXPECT_EQ(writeCount1, kCapacity); EXPECT_FALSE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), kCapacity); EXPECT_EQ(sampleBuffer.writableLength(), 0); SINT readCount1 = shrinkForReadingAndVerify(&sampleBuffer, kCapacity - 10); // Buffer contains the remaining 10 samples, but is still full EXPECT_EQ(readCount1, kCapacity - 10); EXPECT_FALSE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), kCapacity - readCount1); EXPECT_EQ(sampleBuffer.writableLength(), 0); SINT writeCount2 = growAndWrite(&sampleBuffer, kCapacity); // Impossible to write more samples EXPECT_EQ(writeCount2, 0); EXPECT_FALSE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), kCapacity - readCount1); EXPECT_EQ(sampleBuffer.writableLength(), 0); // Trim buffer contents by reallocation mixxx::ReadAheadSampleBuffer(sampleBuffer).swap(sampleBuffer); EXPECT_FALSE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), kCapacity - readCount1); EXPECT_EQ(sampleBuffer.writableLength(), readCount1); // Refill Buffer with samples SINT writeCount3 = growAndWrite(&sampleBuffer, kCapacity); EXPECT_EQ(writeCount3, readCount1); EXPECT_FALSE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), kCapacity); EXPECT_EQ(sampleBuffer.writableLength(), 0); // Read from the end SINT readCount2 = shrinkAfterWritingAndVerify(&sampleBuffer, readCount1); EXPECT_EQ(readCount2, readCount1); EXPECT_FALSE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), kCapacity - readCount1); EXPECT_EQ(sampleBuffer.writableLength(), readCount1); // Trim buffer contents by reallocation has no effect mixxx::ReadAheadSampleBuffer(sampleBuffer).swap(sampleBuffer); EXPECT_FALSE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), kCapacity - readCount1); EXPECT_EQ(sampleBuffer.writableLength(), readCount1); // Refill Buffer with samples SINT writeCount4 = growAndWrite(&sampleBuffer, kCapacity); EXPECT_EQ(writeCount4, readCount2); // buffer has been refilled EXPECT_FALSE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), kCapacity); EXPECT_EQ(sampleBuffer.writableLength(), 0); // Read whole buffer SINT readCount3 = shrinkAfterWritingAndVerify(&sampleBuffer, kCapacity + 10); EXPECT_EQ(readCount3, kCapacity); EXPECT_TRUE(sampleBuffer.empty()); EXPECT_EQ(sampleBuffer.readableLength(), 0); EXPECT_EQ(sampleBuffer.writableLength(), kCapacity); }
HRESULT CPGSSub::ParseSample(REFERENCE_TIME rtStart, REFERENCE_TIME rtStop, BYTE* pData, size_t nLen) { CheckPointer(pData, E_POINTER); CAutoLock cAutoLock(&m_csCritSec); CGolombBuffer sampleBuffer(pData, nLen); while (!sampleBuffer.IsEOF()) { if (m_nCurSegment == NO_SEGMENT) { HDMV_SEGMENT_TYPE nSegType = (HDMV_SEGMENT_TYPE)sampleBuffer.ReadByte(); unsigned short nUnitSize = sampleBuffer.ReadShort(); nLen -= 3; switch (nSegType) { case PALETTE: case OBJECT: case PRESENTATION_SEG: case END_OF_DISPLAY: m_nCurSegment = nSegType; AllocSegment(nUnitSize); break; case WINDOW_DEF: case INTERACTIVE_SEG: case HDMV_SUB1: case HDMV_SUB2: // Ignored stuff... sampleBuffer.SkipBytes(nUnitSize); break; default: return VFW_E_SAMPLE_REJECTED; } } if (m_nCurSegment != NO_SEGMENT) { if (m_nSegBufferPos < m_nSegSize) { size_t nSize = std::min(m_nSegSize - m_nSegBufferPos, nLen); sampleBuffer.ReadBuffer(m_pSegBuffer + m_nSegBufferPos, nSize); m_nSegBufferPos += nSize; } if (m_nSegBufferPos >= m_nSegSize) { CGolombBuffer SegmentBuffer(m_pSegBuffer, m_nSegSize); switch (m_nCurSegment) { case PALETTE: TRACE_PGSSUB(_T("CPGSSub:PALETTE %s\n"), ReftimeToString(rtStart)); ParsePalette(&SegmentBuffer, m_nSegSize); break; case OBJECT: TRACE_PGSSUB(_T("CPGSSub:OBJECT %s\n"), ReftimeToString(rtStart)); ParseObject(&SegmentBuffer, m_nSegSize); break; case PRESENTATION_SEG: TRACE_PGSSUB(_T("CPGSSub:PRESENTATION_SEG %s (size=%d)\n"), ReftimeToString(rtStart), m_nSegSize); if (rtStart == INVALID_TIME) { break; } // Update the timestamp for the previous segment UpdateTimeStamp(rtStart); // Parse the new presentation segment ParsePresentationSegment(rtStart, &SegmentBuffer); break; case WINDOW_DEF: //TRACE_PGSSUB(_T("CPGSSub:WINDOW_DEF %s\n"), ReftimeToString(rtStart)); break; case END_OF_DISPLAY: TRACE_PGSSUB(_T("CPGSSub:END_OF_DISPLAY %s\n"), ReftimeToString(rtStart)); // Enqueue the current presentation segment if any EnqueuePresentationSegment(); break; default: TRACE_PGSSUB(_T("CPGSSub:UNKNOWN Seg %d %s\n"), m_nCurSegment, ReftimeToString(rtStart)); } m_nCurSegment = NO_SEGMENT; } } } return S_OK; }
void BookWriter::write(const MultiSignal& signal, const MultiChannelResult& result) const { BookHeader headerStart; BookDataHeader headerData; BookDataSignalHeader headerSignal; BookDataAtomsHeader headerAtoms; BookDataAtomHeader headerAtom; FILE* file = fopen(pathToBookFile.c_str(), "wb"); if (!file) { throw Exception("couldNotCreateOutputFile"); } const int C = signal.channels.size(); const int N = C ? signal.channels.front().samples.size() : 0; setBE(headerStart.channelCount, C); setBE(headerStart.dictionarySize, 0); setBE(headerStart.energyPercent, 100.0); setBE(headerStart.iterationCount, 1); setBE(headerStart.pointsPerMicrovolt, 1.0); setBE(headerStart.freqSampling, C ? signal.channels.front().freqSampling : 0.0); fwrite(&headerStart, sizeof headerStart, 1, file); long headerDataPosition = ftell(file); setBE(headerData.epochNumber, 1); setBE(headerData.sampleCount, N); fwrite(&headerData, sizeof headerData, 1, file); std::vector<float> sampleBuffer(N); std::vector<float> paramsBuffer; for (int c=0; c<C; ++c) { setBE(headerSignal.channelNumber, c+1); setBE(headerSignal.len, sizeof headerSignal.channelNumber + sizeof(float) * N); fwrite(&headerSignal, sizeof headerSignal, 1, file); for (int i=0; i<N; ++i) { setBE(sampleBuffer[i], signal.channels[c].samples[i]); } fwrite(sampleBuffer.data(), sizeof(float), N, file); long headerAtomsPosition = ftell(file); setBE(headerAtoms.channelNumber, c+1); fwrite(&headerAtoms, sizeof headerAtoms, 1, file); for (const Atom& atom : result[c]) { const size_t P = atom.params.size(); setBE(headerAtom.len, sizeof(float) * P); setBE(headerAtom.type, atom.type); fwrite(&headerAtom, sizeof headerAtom, 1, file); paramsBuffer.resize(atom.params.size()); for (size_t p=0; p<P; ++p) { setBE(paramsBuffer[p], atom.params[p]); } fwrite(paramsBuffer.data(), sizeof(float), P, file); } long currentPosition = ftell(file); fseek(file, headerAtomsPosition, SEEK_SET); setBE(headerAtoms.len, currentPosition - headerAtomsPosition - 5); fwrite(&headerAtoms, sizeof headerAtoms, 1, file); fseek(file, currentPosition, SEEK_SET); } long currentPosition = ftell(file); fseek(file, headerDataPosition, SEEK_SET); setBE(headerData.len, currentPosition - headerDataPosition - 5); fwrite(&headerData, sizeof headerData, 1, file); fseek(file, currentPosition, SEEK_SET); fclose(file); }