void AudioThread::setSampleRate(int sampleRate) { if (deviceController[outputDevice.load()] == this) { deviceSampleRate[outputDevice.load()] = sampleRate; dac.stopStream(); dac.closeStream(); for (int j = 0; j < boundThreads.load()->size(); j++) { AudioThread *srcmix = (*(boundThreads.load()))[j]; srcmix->setSampleRate(sampleRate); } std::vector<DemodulatorInstance *>::iterator demod_i; std::vector<DemodulatorInstance *> *demodulators; demodulators = &wxGetApp().getDemodMgr().getDemodulators(); for (demod_i = demodulators->begin(); demod_i != demodulators->end(); demod_i++) { if ((*demod_i)->getOutputDevice() == outputDevice.load()) { (*demod_i)->setAudioSampleRate(sampleRate); } } dac.openStream(¶meters, NULL, RTAUDIO_FLOAT32, sampleRate, &nBufferFrames, &audioCallback, (void *) this, &opts); dac.startStream(); } this->sampleRate = sampleRate; }
int AudioThread::CallbackWrapper(void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData) { AudioThread * mySelf = (AudioThread *)userData; return mySelf->PortAudioCallBack(inputBuffer, outputBuffer, framesPerBuffer, outTime, userData); }
virtual void Run() { while ( !g_Audio.FInitialized ) {} clPtr<AudioSource> Src = new AudioSource(); Src->BindWaveform( new OggProvider( LoadFileAsBlob( "test.ogg" ) ) ); // TODO: try Src->BindWaveform( new ModPlugProvider( LoadFileAsBlob( "test.it" ) ) ); Src->Play(); FPendingExit = false; double Seconds = Env_GetSeconds(); while ( !IsPendingExit() ) { float DeltaSeconds = static_cast<float>( Env_GetSeconds() - Seconds ); Src->Update( DeltaSeconds ); Seconds = Env_GetSeconds(); } Src = NULL; g_Audio.Exit( true ); exit( 0 ); }
void OnStart( const std::string& RootPath ) { g_FrameBuffer = ( unsigned char* )malloc( ImageWidth * ImageHeight * 4 ); memset( g_FrameBuffer, 0xFF, ImageWidth * ImageHeight * 4 ); g_FS = new FileSystem(); g_FS->Mount( "." ); #if defined(ANDROID) g_FS->Mount( RootPath ); g_FS->AddAliasMountPoint( RootPath, "assets" ); #endif g_Audio.Start( iThread::Priority_Normal ); g_Sound.Start( iThread::Priority_Normal ); }
virtual void Run() { while ( !g_Audio.FInitialized ) {} clPtr<AudioSource> Src = new AudioSource(); Src->BindWaveform( new WavProvider( LoadFileAsBlob( "test.wav" ) ) ); Src->Play(); while ( Src->IsPlaying() ) {} Src = NULL; g_Audio.Exit( true ); exit( 0 ); }
int main(int argc, char **argv) { // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) signalhandlerthread = pthread_self(); signal(SIGINT, signal_handler); // parse and assign command line options parse_options(argc, argv); if (patch_format != patch_format_gig) { printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig to load a .gig file!\n"); printf("Use 'linuxsampler --help' to see all available options.\n"); return EXIT_FAILURE; } int error = 1; #if HAVE_JACK if (use_jack) { dmsg(1,("Initializing audio output (Jack)...")); pAudioIO = new JackIO(); error = ((JackIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, jack_playback); if (error) dmsg(1,("Trying Alsa output instead.\n")); } #endif // HAVE_JACK if (error) { dmsg(1,("Initializing audio output (Alsa)...")); pAudioIO = new AlsaIO(); int error = ((AlsaIO*)pAudioIO)->Initialize(AUDIO_CHANNELS, samplerate, num_fragments, fragmentsize, alsaout); if (error) return EXIT_FAILURE; } dmsg(1,("OK\n")); AudioThread* pEngine = new AudioThread(pAudioIO); MidiIn* pMidiInThread = new MidiIn(pEngine); // Loading gig file result_t result = pEngine->LoadInstrument(argv[argc - 1], instrument_index); if (result.type == result_type_error) return EXIT_FAILURE; pEngine->Volume = volume; dmsg(1,("Starting MIDI in thread...")); if (input_client.size() > 0) pMidiInThread->SubscribeToClient(input_client.c_str()); pMidiInThread->StartThread(); dmsg(1,("OK\n")); sleep(1); dmsg(1,("Starting audio thread...")); pAudioIO->AssignEngine(pEngine); pAudioIO->Activate(); dmsg(1,("OK\n")); if (run_server) { dmsg(1,("Starting network server...")); pLSCPServer = new LSCPServer(pEngine); pLSCPServer->StartThread(); dmsg(1,("OK\n")); } printf("LinuxSampler initialization completed.\n"); while(true) { printf("Voices: %3.3d (Max: %3.3d) Streams: %3.3d (Max: %3.3d, Unused: %3.3d)\r", pEngine->ActiveVoiceCount, pEngine->ActiveVoiceCountMax, pEngine->pDiskThread->ActiveStreamCount, pEngine->pDiskThread->ActiveStreamCountMax, Stream::GetUnusedStreams()); fflush(stdout); usleep(500000); } return EXIT_SUCCESS; }
static int audioCallback(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData) { AudioThread *src = (AudioThread *) userData; float *out = (float*) outputBuffer; memset(out, 0, nBufferFrames * 2 * sizeof(float)); if (src->isTerminated()) { return 1; } if (status) { std::cout << "Audio buffer underflow.." << (src->underflowCount++) << std::endl; } if (src->boundThreads.load()->empty()) { return 0; } float peak = 0.0; for (int j = 0; j < src->boundThreads.load()->size(); j++) { AudioThread *srcmix = (*(src->boundThreads.load()))[j]; if (srcmix->isTerminated() || !srcmix->inputQueue || srcmix->inputQueue->empty() || !srcmix->isActive()) { continue; } if (!srcmix->currentInput) { srcmix->audioQueuePtr = 0; if (srcmix->isTerminated() || srcmix->inputQueue->empty()) { continue; } srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->isTerminated()) { continue; } continue; } // std::lock_guard < std::mutex > lock(srcmix->currentInput->m_mutex); if (srcmix->currentInput->sampleRate != src->getSampleRate()) { while (srcmix->inputQueue->size()) { srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->currentInput) { if (srcmix->currentInput->sampleRate == src->getSampleRate()) { break; } srcmix->currentInput->decRefCount(); } srcmix->currentInput = NULL; } srcmix->audioQueuePtr = 0; if (!srcmix->currentInput) { continue; } } if (srcmix->currentInput->channels == 0 || !srcmix->currentInput->data.size()) { if (!srcmix->inputQueue->empty()) { srcmix->audioQueuePtr = 0; if (srcmix->currentInput) { srcmix->currentInput->decRefCount(); srcmix->currentInput = NULL; } if (srcmix->isTerminated() || srcmix->inputQueue->empty()) { continue; } srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->isTerminated()) { continue; } } continue; } float mixPeak = srcmix->currentInput->peak * srcmix->gain; if (srcmix->currentInput->channels == 1) { for (int i = 0; i < nBufferFrames; i++) { if (srcmix->audioQueuePtr >= srcmix->currentInput->data.size()) { srcmix->audioQueuePtr = 0; if (srcmix->currentInput) { srcmix->currentInput->decRefCount(); srcmix->currentInput = NULL; } if (srcmix->isTerminated() || srcmix->inputQueue->empty()) { break; } srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->isTerminated()) { break; } float srcPeak = srcmix->currentInput->peak * srcmix->gain; if (mixPeak < srcPeak) { mixPeak = srcPeak; } } if (srcmix->currentInput && srcmix->currentInput->data.size()) { float v = srcmix->currentInput->data[srcmix->audioQueuePtr] * srcmix->gain; out[i * 2] += v; out[i * 2 + 1] += v; } srcmix->audioQueuePtr++; } } else { for (int i = 0, iMax = srcmix->currentInput->channels * nBufferFrames; i < iMax; i++) { if (srcmix->audioQueuePtr >= srcmix->currentInput->data.size()) { srcmix->audioQueuePtr = 0; if (srcmix->currentInput) { srcmix->currentInput->decRefCount(); srcmix->currentInput = NULL; } if (srcmix->isTerminated() || srcmix->inputQueue->empty()) { break; } srcmix->inputQueue->pop(srcmix->currentInput); if (srcmix->isTerminated()) { break; } float srcPeak = srcmix->currentInput->peak * srcmix->gain; if (mixPeak < srcPeak) { mixPeak = srcPeak; } } if (srcmix->currentInput && srcmix->currentInput->data.size()) { out[i] = out[i] + srcmix->currentInput->data[srcmix->audioQueuePtr] * srcmix->gain; } srcmix->audioQueuePtr++; } } peak += mixPeak; } if (peak > 1.0) { for (int i = 0; i < nBufferFrames * 2; i++) { out[i] /= peak; } } return 0; }
int main(int argc, char **argv) { pAudioIO = NULL; pRIFF = NULL; pGig = NULL; // setting signal handler for catching SIGINT (thus e.g. <CTRL><C>) signal(SIGINT, signal_handler); patch_format = patch_format_unknown; midi_non_blocking = 1; num_fragments = AUDIO_FRAGMENTS; fragmentsize = AUDIO_FRAGMENTSIZE; strcpy(midi_device, "/dev/midi00"); // parse and assign command line options parse_options(argc, argv); if (patch_format != patch_format_gig) { printf("Sorry only Gigasampler loading migrated in LinuxSampler so far, use --gig\n"); printf("to load a .gig file!\n"); return EXIT_FAILURE; } dmsg(("Initializing audio output...")); pAudioIO = new AudioIO(); int error = pAudioIO->Initialize(AUDIO_CHANNELS, AUDIO_SAMPLERATE, num_fragments, fragmentsize); if (error) return EXIT_FAILURE; dmsg(("OK\n")); // Loading gig file try { printf("Loading gig file..."); fflush(stdout); pRIFF = new RIFF::File(argv[argc - 1]); pGig = new gig::File(pRIFF); pInstrument = pGig->GetFirstInstrument(); pGig->GetFirstSample(); // just to complete instrument loading before we enter the realtime part printf("OK\n"); fflush(stdout); } catch (RIFF::Exception e) { e.PrintMessage(); return EXIT_FAILURE; } catch (...) { printf("Unknown exception while trying to parse gig file.\n"); return EXIT_FAILURE; } DiskThread* pDiskThread = new DiskThread(((pAudioIO->FragmentSize << MAX_PITCH) << 1) + 3); //FIXME: assuming stereo AudioThread* pAudioThread = new AudioThread(pAudioIO, pDiskThread, pInstrument); MidiIn* pMidiInThread = new MidiIn(pAudioThread); dmsg(("Starting disk thread...")); pDiskThread->StartThread(); dmsg(("OK\n")); dmsg(("Starting MIDI in thread...")); pMidiInThread->StartThread(); dmsg(("OK\n")); sleep(1); dmsg(("Starting audio thread...")); pAudioThread->StartThread(); dmsg(("OK\n")); printf("LinuxSampler initialization completed.\n"); while(true) sleep(1000); return EXIT_SUCCESS; }