[英]Playing wav file with portaudio and sndfile
I have written a function to play a sound file using portaudio and sndfile. 我编写了一个使用portaudio和sndfile播放声音文件的功能。 Unfortunately the sound quality is terrible. 不幸的是,声音质量太差了。 The sound is more like a hiss. 声音更像嘶嘶声。 The following is the source code of the function I am using. 以下是我正在使用的函数的源代码。
#define _GLIBCXX_USE_C99_MATH 1
#include "PlaySound_config.h"
#include <boost/predef.h>
#if !defined(USE_PORTAUDIO) // {
# define USE_PORTAUDIO 0
# if (! BOOST_OS_CYGWIN && ! BOOST_OS_WINDOWS) // {
# undef USE_PORTAUDIO
# define USE_PORTAUDIO 1
# endif // }
#endif // }
#if (PLAY_SOUND_HAVE_PORTAUDIO_H && PLAY_SOUND_HAVE_SNDFILE_H && PLAY_SOUND_HAVE_SNDFILE_HH && USE_PORTAUDIO) // {
#if (PLAY_SOUND_HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <portaudio.h>
#include <sndfile.hh>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/format.hpp>
#include "PlaySound.h"
#include "PlaySoundStrings.h"
void SoundWarning(const std::string& message)
{
std::cerr << message << std::endl;
}
bool PlaySoundFile(const std::string& soundFile, unsigned long /* volume */)
{
const int MAX_CHANNELS = 1;
const double SAMPLE_RATE = 11025.0;
const unsigned long FRAMES_PER_BUFFER = 1024;
const size_t BUFFER_LEN = 1024;
using boost::format;
using boost::io::group;
std::string message;
if (soundFile.empty())
{
errno = EINVAL;
message = playSoundStrings[error_invalid_argument];
SoundWarning(message);
return false;
}
boost::filesystem::path soundFilePath(soundFile);
if (! boost::filesystem::exists(soundFilePath))
{
errno = EINVAL;
message = str(format(playSoundStrings[error_file_does_not_exist]) % soundFile.c_str());
SoundWarning(message);
return false;
}
PaError paError = Pa_Initialize();
if (paError != paNoError)
{
message = str(format(playSoundStrings[error_pa_initialize_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
return false;
}
SNDFILE* sndFile;
SF_INFO sfInfo;
sndFile = sf_open(soundFile.c_str(), SFM_READ, &sfInfo);
if (! sndFile)
{
message = str(format(playSoundStrings[error_sf_open_failed]) % soundFile.c_str() % sf_strerror(nullptr));
SoundWarning(message);
Pa_Terminate();
return false;
}
if (sfInfo.channels > MAX_CHANNELS)
{
message = str(format(playSoundStrings[error_too_many_channels]) % sfInfo.channels % MAX_CHANNELS);
SoundWarning(message);
Pa_Terminate();
return false;
}
PaStream* stream = nullptr;
PaStreamParameters paStreamParameters;
paStreamParameters.device = Pa_GetDefaultOutputDevice();
paStreamParameters.channelCount = sfInfo.channels;
paStreamParameters.sampleFormat = paInt16;
paStreamParameters.suggestedLatency = Pa_GetDeviceInfo(paStreamParameters.device)->defaultLowOutputLatency;
paStreamParameters.hostApiSpecificStreamInfo = nullptr;
paError = Pa_OpenStream(
&stream, nullptr, &paStreamParameters,
SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff,
nullptr, nullptr);
if (paError != paNoError || ! stream)
{
message = str(format(playSoundStrings[error_pa_open_stream_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
Pa_Terminate();
return false;
}
paError = Pa_StartStream(stream);
if (paError != paNoError)
{
message = str(format(playSoundStrings[error_pa_start_stream_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
Pa_Terminate();
return false;
}
sf_count_t readCount = 0;
double data[BUFFER_LEN];
while ((readCount = sf_read_double(sndFile, data, BUFFER_LEN)))
{
paError = Pa_WriteStream(stream, data, BUFFER_LEN);
if (paError != paNoError)
{
message = str(format(playSoundStrings[error_pa_write_stream_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
break;
}
}
paError = Pa_CloseStream(stream);
if (paError != paNoError)
{
message = str(format(playSoundStrings[error_pa_close_stream_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
Pa_Terminate();
return false;
}
Pa_Terminate();
return true;
}
I saw some sample code in the article What is a lightweight cross platform WAV playing library? 我在文章“ 什么是轻量级跨平台WAV播放库”中看到了一些示例代码? but the sample is incomplete. 但样本不完整。 It appears that it will only play the first five seconds of the file. 看来它只会播放文件的前五秒。 I want to play the entire file. 我想播放整个文件。
Any idea what I am doing wrong? 知道我在做什么错吗?
This code is part of my PlaySound project. 此代码是我的PlaySound项目的一部分。
I made several mistakes in the original version of my code. 我在代码的原始版本中犯了几个错误。 The first was in the line in which I initialized the sampleFormat member of the PaStreamParameters structure. 第一个是我初始化PaStreamParameters结构的sampleFormat成员的行。
In my original code I initialized this member as follows. 在我的原始代码中,我按如下方式初始化了该成员。
paStreamParameters.sampleFormat = paInt16;
I should have initialized it as follows. 我应该将其初始化如下。
paStreamParameters.sampleFormat = paInt32;
My next mistake was in the call to the Pa_OpenStream function. 我的下一个错误是在调用Pa_OpenStream函数。 I set the sampleRate parameter to a hard coded constant, in this case 11025.0. 我将sampleRate参数设置为硬编码常量,在这种情况下为11025.0。 I should have set it to the value of the samplerate member of the SF_INFO structure. 我应该将其设置为SF_INFO结构的samplerate成员的值。
My third mistake was to use the sf_read_double function to read from the sound file. 我的第三个错误是使用sf_read_double函数从声音文件中读取。 In several working samples I eventually found, including the sndfile-play application, the sf_read_float function is used instead. 我最终发现了几个工作示例,包括sndfile-play应用程序,而是使用sf_read_float函数。
My forth mistake is that I did not scale the data read from the sound file before passing it to the Pa_WriteStream function. 我的第四个错误是,在传递给Pa_WriteStream函数之前,我没有缩放从声音文件读取的数据。 I found the code to scale the data in the source code of the sndfile-play application. 我在sndfile-play应用程序的源代码中找到了用于缩放数据的代码。
For anyone who is interested, the final version of my source code is as follows. 对于任何有兴趣的人,我的源代码的最终版本如下。
#define _GLIBCXX_USE_C99_MATH 1
#include "PlaySound_config.h"
#include <boost/predef.h>
#if !defined(USE_PORTAUDIO) // {
# define USE_PORTAUDIO 0
# if (! BOOST_OS_CYGWIN && ! BOOST_OS_WINDOWS) // {
# undef USE_PORTAUDIO
# define USE_PORTAUDIO 1
# endif // }
#endif // }
#if (PLAY_SOUND_HAVE_PORTAUDIO_H && PLAY_SOUND_HAVE_SNDFILE_H && PLAY_SOUND_HAVE_SNDFILE_HH && USE_PORTAUDIO) // {
#if (PLAY_SOUND_HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <portaudio.h>
#include <sndfile.hh>
#include <cmath>
#include <fstream>
#include <iostream>
#include <vector>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/format.hpp>
#include "PlaySound.h"
#include "PlaySoundStrings.h"
void SoundWarning(const std::string& message)
{
std::cerr << message << std::endl;
}
bool PlaySoundFile(const std::string& soundFile, unsigned long /* volume */)
{
const int MAX_CHANNELS = 1;
const size_t BUFFER_LEN = 1024;
using boost::format;
using boost::io::group;
std::string message;
if (soundFile.empty())
{
errno = EINVAL;
message = playSoundStrings[error_invalid_argument];
SoundWarning(message);
return false;
}
boost::filesystem::path soundFilePath(soundFile);
if (! boost::filesystem::exists(soundFilePath))
{
errno = EINVAL;
message = str(format(playSoundStrings[error_file_does_not_exist]) % soundFile.c_str());
SoundWarning(message);
return false;
}
PaError paError = Pa_Initialize();
if (paError != paNoError)
{
message = str(format(playSoundStrings[error_pa_initialize_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
return false;
}
SNDFILE* sndFile = nullptr;
SF_INFO sfInfo;
::memset(&sfInfo, 0, sizeof(sfInfo));
sndFile = sf_open(soundFile.c_str(), SFM_READ, &sfInfo);
if (! sndFile)
{
message = str(format(playSoundStrings[error_sf_open_failed]) % soundFile.c_str() % sf_strerror(nullptr));
SoundWarning(message);
Pa_Terminate();
return false;
}
if (sfInfo.channels > MAX_CHANNELS)
{
message = str(format(playSoundStrings[error_too_many_channels]) % sfInfo.channels % MAX_CHANNELS);
SoundWarning(message);
Pa_Terminate();
return false;
}
PaStream* stream = nullptr;
PaStreamParameters paStreamParameters;
paStreamParameters.device = Pa_GetDefaultOutputDevice();
paStreamParameters.channelCount = sfInfo.channels;
paStreamParameters.sampleFormat = paInt32;
paStreamParameters.suggestedLatency = Pa_GetDeviceInfo(paStreamParameters.device)->defaultLowOutputLatency;
paStreamParameters.hostApiSpecificStreamInfo = nullptr;
paError = Pa_OpenStream(
&stream, nullptr, &paStreamParameters,
sfInfo.samplerate, paFramesPerBufferUnspecified, paClipOff,
nullptr, nullptr);
if (paError != paNoError || ! stream)
{
message = str(format(playSoundStrings[error_pa_open_stream_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
Pa_Terminate();
return false;
}
paError = Pa_StartStream(stream);
if (paError != paNoError)
{
message = str(format(playSoundStrings[error_pa_start_stream_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
Pa_Terminate();
return false;
}
int subFormat = sfInfo.format & SF_FORMAT_SUBMASK;
double scale = 1.0;
if (subFormat == SF_FORMAT_FLOAT || subFormat == SF_FORMAT_DOUBLE)
{
sf_command(sndFile, SFC_CALC_SIGNAL_MAX, &scale, sizeof(scale));
if (scale < 1e-10)
{
scale = 1.0;
}
else
{
scale = 32700.0 / scale;
}
}
sf_count_t readCount = 0;
float data[BUFFER_LEN];
::memset(data, 0, sizeof(data));
while ((readCount = sf_read_float(sndFile, data, BUFFER_LEN)))
{
if (subFormat == SF_FORMAT_FLOAT || subFormat == SF_FORMAT_DOUBLE)
{
int m = 0;
for (m = 0 ; m < readCount ; ++m)
{
data[m] *= scale;
}
}
paError = Pa_WriteStream(stream, data, BUFFER_LEN);
if (paError != paNoError)
{
message = str(format(playSoundStrings[error_pa_write_stream_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
break;
}
::memset(data, 0, sizeof(data));
}
paError = Pa_CloseStream(stream);
if (paError != paNoError)
{
message = str(format(playSoundStrings[error_pa_close_stream_failed]) % Pa_GetErrorText(paError));
SoundWarning(message);
Pa_Terminate();
return false;
}
Pa_Terminate();
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.