Open source programming for beginners

Sometimes even experienced developers can be a bit overwhelmed by Linux’s extensive development capabilities. Sit back and soak up these tips to become a smarter and more productive Linux developer overnight…


Developing for the Text User Interface
While GUI is almost everywhere, the Text User Interface (TUI) is not dead. GUI interfaces need more system resources and some times may not even work if you have a faulty driver or misconfigured X server. In those cases, Text User Interface can come in handy. Text User Interface is also suitable for embedded devices where resources are very limited.

Note that a Text User Interface is not a command-line interface, but a GUI-like interface written for the Terminal, so that it can be executed without X server or over a remote shell. You can use the ncurses (new curses) library to develop Text User Interface applications. The ncurses runtime is included with almost all Linux distributions. However, you may need to install the developer package for ncurses. On Ubuntu/Debian you can use the following command to install ncurses:
[sourcecode language=”cpp”]$ sudo apt-get install libncurses5-dev
@code: hello_ncurses.c
[sourcecode language=”cpp”]#include <ncurses.h> /* ncurses.h includes stdio.h */
#include <string.h>

int main()
char mesg[]=”Hello Ncurses”;    /* message to be appeared on the screen */
int row,col; /* to store the number of rows and     the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(mesg))/2,”%s”,mesg); /* print the message at the center of the screen */
mvprintw(row-2,0,”This screen has %d rows and %d columnsn”,row,col);

return 0;
Building multimedia applications
Back in the old days, building multimedia applications for Linux (or other UNIX systems) was very difficult. Even if you could get a multimedia application working, support for popular multimedia formats was missing. With the introduction of FFmpeg, building multimedia applications for Linux (and almost any other OS) has become very easy. FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video. It includes libavcodec – which provides developers with an easy-to-use interface to develop multimedia applications. FFmpeg supports almost all the audio/video codecs and file formats. It is capable of transcoding, playing and demuxing for most of these formats/codecs.

The following example encodes a single-tone MP2 audio file using the FFmpeg library.
@example (FFmpeg API Example)
[sourcecode language=”cpp”]#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include “libavcodec/avcodec.h”
#include “libavutil/mathematics.h”
#define INBUF_SIZE 4096
#define AUDIO_INBUF_SIZE 20480
* Audio encoding example
static void audio_encode_example(const char *filename)
AVCodec *codec;
AVCodecContext *c= NULL;
int frame_size, i, j, out_size, outbuf_size;
FILE *f;
short *samples;
float t, tincr;
uint8_t *outbuf;
printf(“Encoding Single Tone Audio…n”);

/* find the MP2 encoder */
codec = avcodec_find_encoder(CODEC_ID_MP2);
if (!codec) {
fprintf(stderr, “codec not foundn”);
c= avcodec_alloc_context();

/* put sample parameters */
c->bit_rate = 64000;
c->sample_rate = 44100;
c->channels = 2;

/* open it */
if (avcodec_open(c, codec) < 0) {
fprintf(stderr, “could not open codecn”);

/* the codec gives us the frame size, in samples */
frame_size = c->frame_size;
samples = malloc(frame_size * 2 * c->channels);
outbuf_size = 10000;
outbuf = malloc(outbuf_size);
f = fopen(filename, “wb”);
if (!f) {
fprintf(stderr, “could not open %sn”, filename);

/* encode a single tone sound */
t = 0;
tincr = 2 * M_PI * 440.0 / c->sample_rate;
for(i=0;i<200;i++) {
for(j=0;j<frame_size;j++) {
samples[2*j] = (int)(sin(t) * 10000);
samples[2*j+1] = samples[2*j];
t += tincr;

/* encode the samples */
out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
fwrite(outbuf, 1, out_size, f);
printf(“%s audio file writtenn”,filename);

int main(int argc, char **argv)
const char *filename;

/* must be called before using avcodec lib */

/* register all the codecs */
if (argc <= 1) {
} else {
filename = argv[1];
return 0;
To build this code, you will need to have FFmpeg developer libraries installed in your system. Once you have it installed, perform the following command to compile the code.
[sourcecode language=”cpp”]$ gcc audiotone.c -o audiotone -lavcodec -lavutil -lm -lz
kunal@ubuntu-vm-kdeo:~$ ./api
Encoding Single Tone Audio…
tone.mp2 audio file written
To play the file:
[sourcecode language=”cpp”]$ ffplay tone.mp2
[twitter username=”linuxusermag”]