Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7187217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:47:45+00:00 2026-05-28T18:47:45+00:00

I was trying to PCM sound a little bit in Python, but all the

  • 0

I was trying to PCM sound a little bit in Python, but all the packages I’ve tried were too much or were undocumented or dead, so I decided to make a simple one with libao.

I used as start point this source code by xiph.org that plays 440Hz for 1 second, and I compiled it with gcc -o ao_example ao_example.c -lao -ldl -lm and I run successfully this code, immediately hearing a 440Hz sine for 1 second in both channels.

So far, so good.

So I $ cp ao_exemple.c mySoundAo.c and I edited mySoundAo.c to be compiled as Python module. The full code follows:

 #include <math.h>
 #include <stdio.h>
 #include <string.h>
 #include <ao/ao.h>
 #include <Python.h>
 #define BUF_SIZE 4096

 static PyObject* py_soundAo(PyObject* self, PyObject* args)
 {
     ao_device *device;
     ao_sample_format format;
     int default_driver;
     char *buffer;
     int buf_size;
     int sample;
     float freq = 440.0;
     int i;
     /* -- Initialize -- */
     fprintf(stderr, "libao example program\n");
     ao_initialize();
     /* -- Setup for default driver -- */
     default_driver = ao_default_driver_id();
     memset(&format, 0, sizeof(format));
     format.bits = 16;
     format.channels = 2;
     format.rate = 44100;
     format.byte_format = AO_FMT_LITTLE;
     /* -- Open driver -- */
     device = ao_open_live(default_driver, &format, NULL /* no options */);
     if (device == NULL) {
         fprintf(stderr, "Error opening device.\n");
         return Py_BuildValue("", 0);
     }
     /* -- Play some stuff -- */
     buf_size = format.bits/8 * format.channels * format.rate;
     buffer = calloc(buf_size,
             sizeof(char));
     for (i = 0; i < format.rate; i++) {
         sample = (int)(0.75 * 32768.0 * sin(2 * M_PI * freq * ((float) i/format.rate)));
         /* Put the same stuff in left and right channel */
         buffer[4*i] = buffer[4*i+2] = sample & 0xff;
         buffer[4*i+1] = buffer[4*i+3] = (sample >> 8) & 0xff;
     }
     ao_play(device, buffer, buf_size);
     /* -- Close and shutdown -- */
     ao_close(device);
     ao_shutdown();
  return Py_BuildValue("", 0);
 }

 static PyMethodDef mySoundAo_methods[] = {
    {"soundAo", py_soundAo, METH_VARARGS},
    {NULL, NULL}
 };

 void initmySoundAo()
 {
    (void) Py_InitModule("mySoundAo", mySoundAo_methods);
 }

So I compiled as gcc -shared -I/usr/include/python2.7/ -o mySoundAo.so mySoundAo.c -lpython2.7 -lm -lsndfile -lao -ldl and I had this warning:

In file included from /usr/include/python2.7/Python.h:8:0,
             from mySoundAo.c:5:
/usr/include/python2.7/pyconfig.h:1158:0: warning: "_POSIX_C_SOURCE" redefined [enabled by default]
/usr/include/features.h:214:0: note: this is the location of the previous definition

Doesn’t sound much dangerous, so I moved on.

In python, I did the following:

$ python
Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mySoundAo
>>> mySoundAo.soundAo()
libao example program
Error opening device.
>>> 

With no sound. Inspecting the code a little bit, I discovered the function ao_initialize(); hangs for about 4 second and the following line default_driver = ao_default_driver_id(); sets this variable to -1 (error).

This behavior is strange, since it’s almost the same code.

So, any ideas to make this work?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:47:47+00:00Added an answer on May 28, 2026 at 6:47 pm

    The warning you get is harmless, simply moving #include <Python.h> to the top should let the standard library correctly recognize the macro is already defined.

    The problem is probably caused by a miscompiled /usr/lib/ao/plugins-4/libalsa.so (this file is mentioned if you set debug in ~/.libao.conf). Since the ao’s alsa plugin fails to load, ao tries all other options, and exhausts the nas timeout of 4 seconds (that’s the cause for the delay).

    To check whether the miscompiled (or mislinked) libalsa.so is the problem, run

    $ ldd -r /usr/lib/ao/plugins-4/libalsa.so > /dev/null
    undefined symbol: ao_is_big_endian      (/usr/lib/ao/plugins-4/libalsa.so)
    

    The error in the output should points to a problem with the symbol. You can simply download libao yourself, and patch out the line in libao-*/src/plugins/alsa/ao_alsa.c, or copy the definition from ao_is_big_endian, or fix the linking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a PCM 32-bit float audio stream to ALAC. I found
Im trying to record a little endian PCM file with AudioQueue. According to the
Trying to keep all the presentation stuff in the xhtml on this project and
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
Trying to load a page fragment, or at least jump to a div but
Trying to read registry keys remotly (from a host on local intranet)... All the
I'm trying to encode raw pcm data as uLaw to save on the bandwidth
Trying to find some information on this but am unable to get any results
Trying to execute a Powershell cmdlet from a MVC 3 Controller using impersonation but
I'm having a hard time trying to record something other than linear PCM on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.