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

  • Home
  • SEARCH
  • 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 374973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:27:27+00:00 2026-05-12T14:27:27+00:00

hey there, just practising and I had a question. I have a program (source

  • 0

hey there, just practising and I had a question. I have a program (source below) that prints out a wave in text. when the wave hits the outside of the terminal I have it make a noise with a function called noise(). but when that function is called it pauses the animation until it completes making the noise, then the animation starts again.

I was wondering if anyone knew of a way for the two functions to happen at the same time. should I fork() it or is there a better way?

the code I’m refering to is the lattus function and the noise function.

bellow is the full source to my program:

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <ao/ao.h>
#include <math.h>

#define BUF_SIZE 4096

int main (int argc, char *argv[]) {     //check for whitch effect to print
    int i = argc;
    for(i > 0; i--;) {
        switch(*argv[i]) {
            case '1':
                lattus();
                break;
            case '2':
                normal();
                break;
            case '3':
                noise(50);
                break;
            default:
                break;
        }
    }
}


char *randstring (char *buffer, int length) {       //genertate a random number
    int i = length;
    for(i >= 0; i--;) {
        buffer[i] = (rand() % 2) ? '1' : '0';
    }
    buffer[length] = 0;
    return buffer;
}

int normal(){           // normal drawing of 1's and 0's
    struct winsize w;
    ioctl(0, TIOCGWINSZ, &w);
    int width = w.ws_col;
    int height = w.ws_row;      //get terminal width and height
    char buffer[width*height + 1];  //create a buffer big enough to hold one draw to the screen
    int i = 25;
    while(i-- >= 0) {
        printf("%s\n", randstring(buffer, width*height));   //draw to screen
        usleep(50000);
    }
    system("clear");        //clear screen
}

int noise(int pitch) {
    int second = 1;
    int freq = (second * pitch);
    ao_device *device;
    ao_sample_format format;
    int default_driver;
    char *buffer;
    int buf_size;
    int sample;
    ao_initialize();
    default_driver = ao_default_driver_id();    
    format.bits = 16;
    format.channels = 2;
    format.rate = 44100;
    format.byte_format = AO_FMT_LITTLE;
    buf_size = format.bits/8 * format.channels * format.rate;
    int b = 10;
    device = ao_open_live(default_driver, &format, NULL /* no options */);
    buffer = calloc(buf_size, sizeof(char));
    for (b = 0; b < format.rate; b++) {
        sample = (int)(1 * 532768.0 * sin(2 * M_PI * freq * ((float) b/format.rate)));
        /* Put the same stuff in left and right channel */
        buffer[2 * b] = buffer[2*b+2] = sample & 0xff;
        buffer[2*b+1] = buffer[2*b+3] = (sample >> 8) & 0xff;
    }
    ao_play(device, buffer, buf_size);
    buffer = 0;
    ao_shutdown();
}

int lattus (void) {
    struct winsize w;
    ioctl(0, TIOCGWINSZ, &w);
    int width = w.ws_col;       //get the terminal width
    char *buffer1 = malloc(sizeof(char) * (width + 1)); //create 3 buffers for each segment
    char *buffer2 = malloc(sizeof(char) * (width + 1)); //each big enough to hold the width of the terminal
    char *buffer3 = malloc(sizeof(char) * (width + 1));
    int first = 1;          //how many before the space
    int second = width - 8;     //how many in the middle of the space
    int third = 1;          //how many at the end of the space
    int i = 1000;           //draw 1000 lines
    int on = 0;         //switch for growing and shrinking
    while(i-- >= 0) {
        usleep(9000);
        if(first == 1 && third == 1 && second == width - 8 || second == width - 9) {    //is it at min?
            if(second % 2 == 0) {       //is it an even number (had problems with buffer if it was odd)
                second = second - 2;
            } else {
                second = second - 3;
            }
            first ++;
            third ++;
            on = 0;     //keep growing
            noise(10);  //make lower noise
        } else if(first == (width - 8) / 2 && third == (width - 8) / 2 && second == 2) {    //untill it gets to max
            if(second % 2 == 0) {
                second = second + 2;
            } else {
                second = second + 1;
            }
            third --;
            first --;
            on = 1;     //start shrinking
            noise(30);  //make higher noise
        } else if(on == 0) {    //else if suppost to grow, grow
            second = second - 2;
            third ++;
            first ++;
        } else if(on == 1) {    //else if suppost to shrink shrink
            second = second + 2;
            third --;
            first --;
        } else {
            break;
        }
        printf("%s   %s   %s\n", randstring(buffer1, first), randstring(buffer2, second), randstring(buffer3, third));  //print it out
        //wait();
    }
    system("clear"); //clear screen
}
  • 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-12T14:27:27+00:00Added an answer on May 12, 2026 at 2:27 pm

    Clearly ao_play is blocking, so you’d need a thread or forked process. I don’t know the latency of forking (it’s probably pretty low for your purposes), but threads are clearly a better fit.

    You’d eventually want to consider mixing if new “play sound” events can overlap with the active sound’s duration. Especially if overlapping sounds are possible, I’d have a dedicated fork or thread that is the only one to call ao_play, and send messages to it, rather than launching a new fork or thread each time.

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

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

Hey guys. I have a method that gets called each second which I want
Hey there, I have read the few posts here on when/how to use the
Hey there, I've got a block of HTML that I'm going to be using
My scenario: one server and some clients (though not many). The server can only

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.