10분만에 배우는 C와 C++

쉬어가는 페이지 C/C++를 배워보자

1. 처음하는 C/C++프로그래밍

Mymixer 예제는 C/C++ 를 배울때 가장 먼저 배우는 hello 프로그램을 변형한 것으로 printf출력문의 예제 사용법을 보여준다.

mymixer.c

#include <stdio.h>

void main()

{

// 나의 첫 프로그램

printf("My Mixer \n");

printf("Output = %d \n", 10);

}

- 소스 코드 중 “//” 는 주석을 의미하며 코드부분이 아니므로 컴파일되지 않는다.

- 프로그램의 라인 마지막은 항상 ; 를 넣어준다.

#include <stdio.h>

void main()

{

// Channel1

short channel1_input = 10;

short channel1_gain = 10;

short channel1_Volume_Fader = 5;

short channel1 = channel1_input * channel1_gain * channel1_Volume_Fader;

// Channel2

short channel2_input = 9;

short channel2_gain = 12;

short channel2_Volume_Fader = 4;

short channel2 = channel2_input * channel2_gain * channel2_Volume_Fader;

// Output

short output;

output = channel1 + channel2;

printf("output = %d \n",output);

}

쉬어가는 페이지 C/C++를 배워보자

2. 덧셈

믹서 부분을 공부하였다면 믹서는 여러 가지 입력의 합인 것을 알 수 있을 것이다. 다음 소개하는 예제는 두개의 입력, 3과 5를 가진 믹서와 그 출력 8을 만들어 내는 프로그램이다.

channel .c

#include <stdio.h>

void main()

{

short channel1 = 3;

short channel2 = 5;

short output =channel1 + channel2;

printf("mixerout = %d \n",output);

}

이 예제를 통하여 변수 선언과 변수의 합을 구하는 방법을 배웠다.

쉬어가는 페이지 C/C++를 배워보자

3. 곱셈

믹서의 한채널을 살펴보면 게인, Send, EQ, 그리고 볼륨페이더등 여러가지 증폭기를 보게 되는데 이러한 것들은 곱셈으로 표현될 수 있다..

multiply.c

#include <stdio.h>

void main()

{

short channel1_input = 10;

short channel1_gain = 10;

short channel1_Volume_Fader = 5;

short channel1 = channel1_input * channel1_gain * channel1_Volume_Fader;

printf("channel1 = %d \n",channel1);

}

이 소스코드에서 정의된 변수 channel1_input 은 오디오의 입력을 나타내며 게인과, 볼륨페이더 컨트롤을 가지고 있다.

CHANNEL1 INPUT * GAIN * VOLUME = CHANNEL1

쉬어가는 페이지 C/C++를 배워보자

4. Mixer

만일 3개의 입력 단을 가진 믹서를 곱셈 프로그램을 확장하여 프로그래밍 한다면 다음과 같이 할 수 있다.

#include <stdio.h>

void main()

{

// Channel1

short channel1_input = 10;

short channel1_gain = 10;

short channel1_Volume_Fader = 5;

short channel1 = channel1_input * channel1_gain * channel1_Volume_Fader;

// Channel2

short channel2_input = 9;

short channel2_gain = 12;

short channel2_Volume_Fader = 4;

short channel2 = channel2_input * channel2_gain * channel2_Volume_Fader;

// Output

short output;

output = channel1 + channel2;

printf("output = %d \n", output);

}

쉬어가는 페이지 C/C++를 배워보자

5. Structure

만일 16개의 입력을 가진 믹서를 전에 한 방법(4.Mixer)으로 만들어 본다면 적어도 48(3*16)의 변수를 선언하여 각각의 게인과 볼륨 컨트롤을 제어 하여야 할것이다. 그러나 이제 사용하게될 Structure를 사용하면 프로그램은 매우 간결하게 바뀌게 되는데 이것이 바로 객체 지향형 프로그램의 장점이 된다.

struct.c

#include <stdio.h>

typedef struct MYCHANNEL

{

shortinput;

shortgain;

shortvolume;

shortoutput;

}MYCHANNEL;

void main()

{

//Declare Channel

MYCHANNEL channel1 ;

MYCHANNEL channel2 ;

short output;

//channel1

channel1.input =10;

channel1.gain = 10;

channel1.volume = 5;

channel1.output = channel1.input * channel1.gain * channel1.volume;

//channel2

channel2.input =9;

channel2.gain = 12;

channel2.volume = 4;

channel2.output = channel2.input * channel2.gain * channel2.volume;

//output

output = channel1.output + channel2.output;

printf("output = %d \n", output);

}

쉬어가는 페이지 C/C++를 배워보자

6. 서브 루틴

C/C++프로그램의 서브루틴은 믹서의 기능중 AUX SEND, 와 RETURN으로 이해 하면 된다. 메인 믹서 프로그램에서 앰프 기능(프리앰프)기능을 떼어 내어 AUX 센드, 리턴 한다고 생각하면 subroutine.c 는 이해가 가리라 믿는다.

subroutine.c

#include "stdio.h"

short amplify(int input, int value)

{

short output;

output = input*value;

return (output);

}

void main()

{

short sample[10] ={0,1,-2,1,2,-2,2,-1,-3,0};// = 0;

int i;

for (i=0;i<10;i++){// loop for entire sample

sample [i] = amplify(sample [i], 2);// amp subroutine.

printf("result = %d \n", sample [i]);

}

return(0);

}

쉬어가는 페이지 C/C++를 배워보자

7. 텍스트 표시

이번 프로그램 예제는 C/C++에서 어떻게 문자열(String)을 표시하는지 알아보도록 하자. 디지털 믹서 프로그램시 채널이름을 표기하는데 유용하게 쓰일것이다.

channel.c

#include <stdio.h>

void main()

{

short channel1 = 3;

short channel2 = 5;

char channel1_name[] = "Guitar";

char channel2_name[] = "Bass";

short output =channel1 + channel2;

printf(channel1_name);

printf(" = %d\n",channel1);

printf(channel2_name);

printf(" = %d\n",channel2);

printf("mixerout = %d \n",output);

}


쉬어가는 페이지 C/C++를 배워보자

8. 어레이

오디오 샘플은 시간의 흐름에 따른 소리의 세기 변화 데이터 모음이다. 만일 CD수준의 16bit 44.1Khz 샘플링 방식을 사용한다면 초당 441,000 어레이의 샘플이 필요하며 소리의 세기는 2^16(65,536단계: +32,767~-32,768). 이번 예제는 3bit, 10Hz 샘플링 방식을 사용하여 어레이의 사용법을 알아보도록 한다. 소리의 세기는 2^3(8단계: +3~-4)로 표현되고, 10Hz샘플링 방식의 사용으로 10개의 샘플 어레이를 가지게 된다.

Sample[0]=0

Sample[1]=1

Sample[2]=-2

Sample[3]=1

Sample[4]=2

Sample[5]=-2

Sample[6]=2

Sample[7]=-1

Sample[8]=-3

Sample[9]=0


 

캡션: 데이터 그레프

array.c

#include "stdio.h"

void main()

{

short sample[10] ={0,1,-2,1,2,-2,2,-1,-3,0};// = 0;

int i;

for (i=0;i<10;i++){// 전체 샘플

sample [i] = sample [i] * 2;// 두배 증폭

printf("result = %d \n", sample [i]);

}

return(0);

}

쉬어가는 페이지 C/C++를 배워보자

9. 헤더파일

#include 문을 사용하여 헤더파일을 메인 프로그램과 연결하여 주는데 이 헤더파일은 프로그래밍에 자주 사용되는 기능들을 모아 놓은 것쯤으로 이해하면 쉽다. 앞서 서브루틴을 사용하여 증폭하는 루틴을 만든 것을 상기해보며, 이 기능을 헤더파일로 만들어 보자. 증폭 기능을 헤더 파일로 만들어두면 이번 프로그램뿐 아니라 다른 프로그램에서도 프로그램의 상단에 #include "amplify.h" 문을 사용하여 amplify.h 를 프로그램에 쉽게 포함(include) 시킬수 있다.

main.c

#include "amplify.h"

void main()

{

short sample[10] ={0,1,-2,1,2,-2,2,-1,-3,0};// = 0;

int i;

for (i=0;i<10;i++){ // 전체 샘플

sample [i] = amplify(sample [i], 2);//2배로 증폭

printf("result = %d \n", sample [i]);

}

return(0);

}

amplify.h

#include "stdio.h"

short amplify(int input, int value)

{

short output;

output = input*value;

return (output);

}

쉬어가는 페이지 C/C++를 배워보자

10. 비교

C/C++ 비교문.

if( a == b ) ;

/* 비교 처리문 */

==같다if(a == b) a 와 b 가 같을때

!=같지 않다if(a != b) a 와 b 가 같지 않을때

>크다if(a > b) a 가 b 보다 클때

ㅡ작다if(a < b) a 가 b 보다 작을떄

>=크거나 같다if(a >= b) a 가 b 보타 크거나 같을때

<=작거나 같다.if(a <= b) a 가 b 보타 작거나 같을때


11. 포인터

포인터는 변수의 일종으로 정수(int), 부동소수점(float), 문자(char) 등과 는 달리 메모리내의 주소를 기억시키는 변수라고 이해하자.

포인터는 두 가지 연산자(Operator)를 가지고 있는데 & 는 주소 연산자, *는 참조 연산자라고 한다.

주소 연산자 &는 변수의 주소를 얻기 위해 사용한다. 그래서 포인터는 변수의 주소를 가리키게 됩니다.

pointer = &variable;

포인터 = &변수

참조 연산자 *는 포인터가 지정하는 주소의 값을 변수에 대입시킨다.

anothervariable = *pointer;

다른변수 = *포인터

그러므로 *pointer가 가리키는 어드레스의 값이 “10”이라면 anothervariable 는 10을 갖게 된다.

포인터가 지정하는 어드레스에 일정 값을 주고 싶을 때는 다음과 같이 한다.

*pointer = 12;

이때 *pointer 가 지정하는 번지수에 “12”를 대입하게 된다.

*pointer = 12; // 12를 *pointer 가 지정하는 어드레스에

// 데이터 12를 넣는다

anothervariable = *pointer; // 변수 anothervariable 에 *pointer 가지정한

// 어드레스의 값 12를 준다

12. 포인터예제

#include <iostream.h>

main()

{

int variable;

int *pointer;

variable = 2;// 정수 변수 variable에 2를 넣고

cout << "변수=" << variable << "\n";

pointer = &variable;// 포인터에 변수가 저장된

// 어드레스를 지정

*pointer = 3;// 포인터가 가리키는 번지에 “3”

// 을 넣는다.

cout << "변수=" << variable << "\n";

return(0);

}

그러므로 변수 variable은 초기값 2에서 포인터사용으로 인해 3으로 바뀌게 된다.

프로그램의 출력은

변수 =2

변수 =3

이 된다.

13. 파일 입출력

교재 내에서 설명된 Real Sound는 바이너리 데이터(텍스트가 아님)인 RAW데이터를 사용하여 사운드를 입, 출력한다.

이 바이너리 파일의 입력을 위하여 Real Sound는 C스타일의 가장 간단한 fread 그리고 바이너리 파일 출력을 위하여 fwrite를 사용한다.

fread와 Fwrite의 일반적인 사용법은 다음과 같다.

read_size=fread(data_ptr, count, size, file);

write_size=fwrite(data_ptr, count, size, file);

read_size, write_size읽어들인 데이터나 저장할 데이터의 크기,

data_pter파일의 입출력을 위한 포인터.

count입출력된 아이템의 최대 수

size읽거나 저장된 파일크기, 바이트(bytes)

file입출력 파일

// RAW파일 읽는 루틴

void openrawfile(char *filename)

{

FILE *openfile;

openfile = fopen(filename,"r");

int rawopen = fread(&sample, 2, total_samples ,openfile);

fclose(openfile);

}

// RAW파일 저장 루틴

void saverawfile(char *filename)

{

FILE *savefile;

savefile = fopen(filename,"wb");

int rawsave = fwrite(&sample,2,total_samples,savefile);

fclose(savefile);

}


이 글과 관련있는 글을 자동검색한 결과입니다 [?]

by loveletter | 2009/10/12 13:53 | 트랙백 | 덧글(0)

트랙백 주소 : http://lovencurse.egloos.com/tb/5093873
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶