Add wav file playback code using ALSA

This commit is contained in:
Sanchayan Maity 2017-05-03 14:17:27 +05:30
parent 905f9be573
commit daa2bf8053
5 changed files with 256 additions and 0 deletions

64
alsa-audio/.cproject Normal file
View file

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="cdt.managedbuild.config.gnu.cross.exe.debug.2104091374">
<storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.config.gnu.cross.exe.debug.2104091374" moduleId="org.eclipse.cdt.core.settings" name="Debug">
<externalSettings/>
<extensions>
<extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
<extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
<extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
</extensions>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe,org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.debug" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.cross.exe.debug.2104091374" name="Debug" parent="cdt.managedbuild.config.gnu.cross.exe.debug">
<folderInfo id="cdt.managedbuild.config.gnu.cross.exe.debug.2104091374." name="/" resourcePath="">
<toolChain id="cdt.managedbuild.toolchain.gnu.cross.exe.debug.1343894582" name="Cross GCC" superClass="cdt.managedbuild.toolchain.gnu.cross.exe.debug">
<targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.targetPlatform.gnu.cross.916744984" isAbstract="false" osList="all" superClass="cdt.managedbuild.targetPlatform.gnu.cross"/>
<builder buildPath="${workspace_loc:/alsa-audio}/Debug" id="cdt.managedbuild.builder.gnu.cross.866785088" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.builder.gnu.cross"/>
<tool command="${CC}" id="cdt.managedbuild.tool.gnu.cross.c.compiler.1265550156" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler">
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.option.optimization.level.1104699262" name="Optimization Level" superClass="gnu.c.compiler.option.optimization.level" useByScannerDiscovery="false" valueType="enumerated"/>
<option id="gnu.c.compiler.option.debugging.level.644686818" name="Debug Level" superClass="gnu.c.compiler.option.debugging.level" useByScannerDiscovery="false" value="gnu.c.debugging.level.max" valueType="enumerated"/>
<option id="gnu.c.compiler.option.misc.other.1122560822" superClass="gnu.c.compiler.option.misc.other" useByScannerDiscovery="false" value="${CFLAGS} -c" valueType="string"/>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.73563471" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.874184357" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler">
<option id="gnu.cpp.compiler.option.optimization.level.1717062205" name="Optimization Level" superClass="gnu.cpp.compiler.option.optimization.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
<option id="gnu.cpp.compiler.option.debugging.level.1675362898" name="Debug Level" superClass="gnu.cpp.compiler.option.debugging.level" useByScannerDiscovery="false" value="gnu.cpp.compiler.debugging.level.max" valueType="enumerated"/>
</tool>
<tool command="${CC}" id="cdt.managedbuild.tool.gnu.cross.c.linker.1085007460" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker">
<option id="gnu.c.link.option.libs.1646850397" superClass="gnu.c.link.option.libs" useByScannerDiscovery="false" valueType="libs">
<listOptionValue builtIn="false" value="asound"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.c.linker.input.1700448547" superClass="cdt.managedbuild.tool.gnu.c.linker.input">
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
<additionalInput kind="additionalinput" paths="$(LIBS)"/>
</inputType>
</tool>
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.linker.414934536" name="Cross G++ Linker" superClass="cdt.managedbuild.tool.gnu.cross.cpp.linker"/>
<tool id="cdt.managedbuild.tool.gnu.cross.archiver.142861756" name="Cross GCC Archiver" superClass="cdt.managedbuild.tool.gnu.cross.archiver"/>
<tool command="${AS}" id="cdt.managedbuild.tool.gnu.cross.assembler.506338534" name="Cross GCC Assembler" superClass="cdt.managedbuild.tool.gnu.cross.assembler">
<inputType id="cdt.managedbuild.tool.gnu.assembler.input.1189854419" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
</tool>
</toolChain>
</folderInfo>
</configuration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="cdtBuildSystem" version="4.0.0">
<project id="alsa-audio.cdt.managedbuild.target.gnu.cross.exe.1389774208" name="Executable" projectType="cdt.managedbuild.target.gnu.cross.exe"/>
</storageModule>
<storageModule moduleId="scannerConfiguration">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.debug.2104091374;cdt.managedbuild.config.gnu.cross.exe.debug.2104091374.;cdt.managedbuild.tool.gnu.cross.c.compiler.1265550156;cdt.managedbuild.tool.gnu.c.compiler.input.73563471">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
</scannerConfigBuildInfo>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
</cproject>

1
alsa-audio/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/Debug/

26
alsa-audio/.project Normal file
View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>alsa-audio</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
<triggers>full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
</natures>
</projectDescription>

View file

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project>
<configuration id="cdt.managedbuild.config.gnu.cross.exe.debug.2104091374" name="Debug">
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="643270697067650404" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
</extension>
</configuration>
</project>

View file

@ -0,0 +1,151 @@
#include <stdio.h>
#include <alsa/asoundlib.h>
static unsigned int rate = 44100;
int main(int argc, char *argv[])
{
FILE *fp;
int dir;
int err;
int size;
long loops;
unsigned int rrate, val;
char *buffer;
char devname[32] = {0};
snd_pcm_t *handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
if (argc < 3) {
printf("Usage: %s <wavefile_name> <module_name> <rate>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (argc >= 2) {
fp = fopen(argv[1], "r");
if (!fp) {
printf("Could not open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
}
if (argc == 4) {
rate = atoi(argv[3]);
printf("Requested playback rate: %d\n", rate);
}
printf("Module Name: %s\n", argv[2]);
if (!strcmp("colibri-vf", argv[2]))
strcpy(devname, "default:CARD=Audio");
else if (!strcmp("apalis-imx6", argv[2]))
strcpy(devname, "default:CARD=imx6qapalissgtl");
else if (!strcmp("colibri-imx6", argv[2]))
strcpy(devname, "default:CARD=imx6qcolibrisgtl");
else if (!strcmp("colibri-t20", argv[2]))
strcpy(devname, "default:CARD=colibrit20wm971");
else if (!strcmp("colibri-t30", argv[2]))
strcpy(devname, "default:CARD=colibrit30sgtl5");
err = snd_pcm_open(&handle, devname, SND_PCM_STREAM_PLAYBACK, 0);
if (err < 0) {
fprintf(stderr, "unable to open pcm device: %s\n", snd_strerror(err));
exit(EXIT_FAILURE);
}
snd_pcm_hw_params_alloca(&params);
err = snd_pcm_hw_params_any(handle, params);
if (err < 0) {
printf("Broken configuration for playback: no configurations available: %s\n", snd_strerror(err));
return err;
}
err = snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
if (err < 0) {
printf("Resampling setup failed for playback: %s\n", snd_strerror(err));
return err;
}
err = snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE);
if (err < 0) {
printf("Sample format not available for playback: %s\n", snd_strerror(err));
return err;
}
err = snd_pcm_hw_params_set_channels(handle, params, 1);
if (err < 0) {
printf("Channels count (%i) not available for playbacks: %s\n", 1, snd_strerror(err));
return err;
}
rrate = rate;
err = snd_pcm_hw_params_set_rate_near(handle, params, &rrate, 0);
if (err < 0) {
printf("Rate %iHz not available for playback: %s\n", rrate, snd_strerror(err));
return err;
}
if (rrate != rate) {
printf("Rate doesn't match (requested %iHz, get %iHz)\n", rate, err);
return -EINVAL;
}
err = snd_pcm_hw_params(handle, params);
if (err < 0) {
fprintf(stderr, "unable to set hw parameters: %s\n",
snd_strerror(err));
exit(EXIT_FAILURE);
}
/* Use a buffer large enough to hold one period */
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
//size = frames * 4; /* 2 bytes/sample, 2 channels */
size = frames * 2; /* 2 bytes/sample, 1 channel */
buffer = (char *) malloc(size);
if (!buffer) {
snd_pcm_drain(handle);
snd_pcm_close(handle);
fclose(fp);
return -ENOMEM;
}
/* We want to loop for 5 seconds */
err = snd_pcm_hw_params_get_period_time(params, &val, &dir);
if (err < 0) {
printf("Unable to get period time for playback: %s\n", snd_strerror(err));
return err;
}
/* 5 seconds in microseconds divided by period time */
loops = 5000000 / val;
while (1) {
loops--;
err = fread(buffer, 1, size, fp);
if (err == 0) {
fprintf(stderr, "end of file on input\n");
break;
} else if (err != size)
fprintf(stderr, "short read: read %d bytes\n", err);
err = snd_pcm_writei(handle, buffer, frames);
if (err == -EPIPE) {
/* EPIPE means underrun */
fprintf(stderr, "underrun occurred\n");
snd_pcm_prepare(handle);
}
else if (err < 0)
fprintf(stderr, "error from writei: %s\n", snd_strerror(err));
else if (err != (int)frames)
fprintf(stderr, "short write, write %d frames\n", err);
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buffer);
fclose(fp);
return 0;
}