Related
I am testing out cuda graphs. My graph is as follows.
the code for this is as follows
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
#define NumThreads 20
#define NumBlocks 1
template <typename PtrType>
__global__ void kernel1(PtrType *buffer, unsigned int numElems) {
int tid = threadIdx.x + blockIdx.x * blockDim.x;
buffer[tid] = (PtrType)tid;
}
template <typename PtrType>
__global__ void kernel2(PtrType *buffer, unsigned int numElems) {
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if(tid < numElems/2) buffer[tid] += 5;
}
template <typename PtrType>
__global__ void kernel3(PtrType *buffer, unsigned int numElems) {
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if(tid>=numElems/2) buffer[tid] *= 5;
}
template <typename PtrType>
void print(void *data) {
PtrType *buffer = (PtrType *)data;
std::cout << "[";
for (unsigned int i = 0; i < NumThreads; ++i) {
std::cout << buffer[i] << ",";
}
std::cout << "]\n";
}
void runCudaGraph(cudaGraph_t &Graph, cudaGraphExec_t &graphExec,
cudaStream_t &graphStream) {
cudaGraphInstantiate(&graphExec, Graph, nullptr, nullptr, 0);
cudaStreamCreateWithFlags(&graphStream, cudaStreamNonBlocking);
cudaGraphLaunch(graphExec, graphStream);
cudaStreamSynchronize(graphStream);
}
void destroyCudaGraph(cudaGraph_t &Graph, cudaGraphExec_t &graphExec,
cudaStream_t &graphStream) {
cudaCtxResetPersistingL2Cache();
cudaGraphExecDestroy(graphExec);
cudaGraphDestroy(Graph);
cudaStreamDestroy(graphStream);
cudaDeviceReset();
}
template <typename PtrType>
void createCudaGraph(cudaGraph_t &Graph, cudaGraphExec_t &graphExec,
cudaStream_t &graphStream, PtrType *buffer,
unsigned int numElems, PtrType *hostBuffer) {
cudaGraphCreate(&Graph, 0);
cudaGraphNode_t Kernel1;
cudaKernelNodeParams nodeParams = {0};
memset(&nodeParams, 0, sizeof(nodeParams));
nodeParams.func = (void *)kernel1<PtrType>;
nodeParams.gridDim = dim3(NumBlocks, 1, 1);
nodeParams.blockDim = dim3(NumThreads/NumBlocks, 1, 1);
nodeParams.sharedMemBytes = 0;
void *inputs[2];
inputs[0] = (void *)&buffer;
inputs[1] = (void *)&numElems;
nodeParams.kernelParams = inputs;
nodeParams.extra = nullptr;
cudaGraphAddKernelNode(&Kernel1, Graph, nullptr, 0, &nodeParams);
cudaGraphNode_t Kernel2;
memset(&nodeParams, 0, sizeof(nodeParams));
nodeParams.func = (void *)kernel2<PtrType>;
nodeParams.gridDim = dim3(NumBlocks, 1, 1);
nodeParams.blockDim = dim3(NumThreads/NumBlocks, 1, 1);
nodeParams.sharedMemBytes = 0;
inputs[0] = (void *)&buffer;
inputs[1] = (void *)&numElems;
nodeParams.kernelParams = inputs;
nodeParams.extra = NULL;
cudaGraphAddKernelNode(&Kernel2, Graph, &Kernel1, 1, &nodeParams);
cudaGraphNode_t Kernel3;
memset(&nodeParams, 0, sizeof(nodeParams));
nodeParams.func = (void *)kernel3<PtrType>;
nodeParams.gridDim = dim3(NumBlocks, 1, 1);
nodeParams.blockDim = dim3(NumThreads/NumBlocks, 1, 1);
nodeParams.sharedMemBytes = 0;
inputs[0] = (void *)&buffer;
inputs[1] = (void *)&numElems;
nodeParams.kernelParams = inputs;
nodeParams.extra = NULL;
cudaGraphAddKernelNode(&Kernel3, Graph, &Kernel1, 1, &nodeParams);
cudaGraphNode_t copyBuffer;
std::vector<cudaGraphNode_t> dependencies = {Kernel2, Kernel3};
cudaGraphAddMemcpyNode1D(©Buffer, Graph,dependencies.data(),dependencies.size(),hostBuffer, buffer, numElems*sizeof(PtrType), cudaMemcpyDeviceToHost);
cudaGraphNode_t Host1;
cudaHostNodeParams hostNodeParams;
memset(&hostNodeParams, 0, sizeof(hostNodeParams));
hostNodeParams.fn = print<PtrType>;
hostNodeParams.userData = (void *)&hostBuffer;
cudaGraphAddHostNode(&Host1, Graph, ©Buffer, 1,
&hostNodeParams);
}
int main() {
cudaGraph_t graph;
cudaGraphExec_t graphExec;
cudaStream_t graphStream;
unsigned int numElems = NumThreads;
unsigned int bufferSizeBytes = numElems * sizeof(unsigned int);
unsigned int hostBuffer[numElems];
memset(hostBuffer, 0, bufferSizeBytes);
unsigned int *deviceBuffer;
cudaMalloc(&deviceBuffer, bufferSizeBytes);
createCudaGraph(graph, graphExec, graphStream, deviceBuffer,numElems, hostBuffer);
runCudaGraph(graph, graphExec, graphStream);
destroyCudaGraph(graph, graphExec, graphStream);
std::cout << "graph example done!" << std::endl;
}
When I run this example I get a result of
[3593293488,22096,3561843129,22096,3561385808,22096,3593293488,22096,3598681264,22096,3561792984,22096,2687342880,0,0,0,3598597376,22096,3598599312,0,]
However I expect:
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
I can't figure out where I went wrong. I used cuda-gdb and it seems right on the GPU. However, somewhere in the memCpy and sending to host function it goes wrong. Any ideas?
You are not passing the correct pointer to the host callback.
void createCudaGraph(cudaGraph_t &Graph, cudaGraphExec_t &graphExec,
cudaStream_t &graphStream, PtrType *buffer,
unsigned int numElems, PtrType *hostBuffer) {
...
hostNodeParams.userData = (void *)&hostBuffer;
}
This takes the address of a stack variable which is no longer valid when the host function is called.
Since hostBuffer already points to the array you want to print, you can just pass this pointer directly.
hostNodeParams.userData = (void *)hostBuffer;
That aside, I would like to mention that there is a handy function
cudaGraphDebugDotPrint which can output a graph to file that can be converted to png to help with debugging. With your original code, it will show that the pointer used as memcpy destination and the pointer passed to the host function are different.
I am trying to play a sin wave sound with SDL2 by using the audio queue on C++. In order to do that, I have created a class "Speaker", which has a pushBeep function that is called every time a beep needs to be generated. I have created an AudioDevice successfully, and it is also successful when I do the QueueAudio to the device (I have checked on the debugger) but I can't seem to get any sound out of it.
I have tried changing the way I generate the samples in numerous ways, also, as I said previously, I have checked that the device is properly opened and the QueueAudio returns 0 for success.
This is the class
Speaker::Speaker()
{
SDL_AudioSpec ds;
ds.freq = Speaker::SPEAKER_FREQUENCY;
ds.format = AUDIO_F32;
ds.channels = 1;
ds.samples = 4096;
ds.callback = NULL;
ds.userdata = this;
SDL_AudioSpec os;
this->dev = SDL_OpenAudioDevice(NULL, 0, &ds, &os, NULL);
std::cout << "DEVICE: " << this->dev << std::endl;
SDL_PauseAudioDevice(this->dev, 0);
}
Speaker::~Speaker()
{
SDL_CloseAudioDevice(this->dev);
}
void Speaker::pushBeep(double freq, int duration) {
int nSamples = duration * Speaker::SPEAKER_FREQUENCY / 1000;
float* samples = new float[nSamples];
double v = 0.0;
for (int idx = 0; idx < nSamples; idx++) {
//float value = (float)Speaker::SPEAKER_AMPLITUDE * std::sin(v * 2 * M_PI / Speaker::SPEAKER_FREQUENCY);
float value = 440.0;
samples[idx] = value;
v += freq;
}
int a = SDL_QueueAudio(this->dev, (void*)samples, nSamples * sizeof(float));
std::cout << a << std::endl;
delete[] samples;
samples = NULL;
}
And this is how I call it
Speaker s;
s.pushBeep(440.0, 1000);
When I try with the sin wave generation code (commented) it gives me a "double to float loss of precision" error. When I use the fixed value (not commented) it does not give the error, but it still does not work.
I expect the program to output the sound.
Couple of things you are missing, or maybe you didn't add to your code snippet. You didn't specify an audio callback so when you call SDL_QueueAudio(); it didn't know what to do with the data I'm pretty sure. And you weren't calling SDL_PauseAudioDevice() in your example with the delay.
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <iostream>
namespace AudioGen
{
const int AMPLITUDE = 1;
const int SAMPLE_RATE = 44000;
// Globals
float *in_buffer;
SDL_atomic_t callback_sample_pos;
SDL_Event event;
SDL_bool running = SDL_TRUE;
/**
* Structure for holding audio metadata such as frequency
*/
struct AudioData
{
int sampleNum;
float frequency;
};
void audio_callback(void *user_data, Uint8 *raw_buffer, int bytes)
{
float *buffer = (float*)raw_buffer;
AudioData &audio_data(*static_cast<AudioData*>(user_data));
int nSamples = bytes / 4; // For F32
std::cout << nSamples << std::endl;
for(int i = 0; i < nSamples; i++, audio_data.sampleNum++)
{
double time = (double)audio_data.sampleNum / (double)SAMPLE_RATE;
buffer[i] = (float)(AMPLITUDE * sin(2.0f * M_PI * audio_data.frequency * time));
}
}
int buffer_length;
void callback(void *user_data, Uint8 *raw_buffer, int bytes)
{
float *buffer = (float*)raw_buffer;
int nSamples = bytes/4;
auto local_sample_pos = SDL_AtomicGet(&callback_sample_pos);
for(int i = 0; i < nSamples; ++i)
{
// Stop running audio if all samples are finished playing
if(buffer_length == local_sample_pos)
{
running = SDL_FALSE;
break;
}
buffer[i] = in_buffer[local_sample_pos];
++local_sample_pos;
}
SDL_AtomicSet(&callback_sample_pos, local_sample_pos);
}
class Speaker
{
public:
Speaker()
{
SDL_Init(SDL_INIT_AUDIO);
SDL_AudioSpec ds;
ds.freq = SAMPLE_RATE;
ds.format = AUDIO_F32;
ds.channels = 1;
ds.samples = 4096;
ds.callback = callback;
ds.userdata = &ad; // metadata for frequency
SDL_AudioSpec os;
dev = SDL_OpenAudioDevice(NULL, 0, &ds, &os, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
}
~Speaker()
{
SDL_CloseAudioDevice(dev);
SDL_Quit();
}
void pushBeep(float frequency, int duration)
{
ad.frequency = frequency; // set the frequency for the beep
SDL_PauseAudioDevice(dev, 0);
SDL_Delay(duration); // wait while sound is playing
SDL_PauseAudioDevice(dev, 1);
}
void pushBeep2(float frequency, int duration )
{
int nSamples = duration * SAMPLE_RATE / 1000;
in_buffer = new float[nSamples];
buffer_length = nSamples;
for (int idx = 0; idx < nSamples; idx++) {
double time = (double)idx / (double)SAMPLE_RATE;
in_buffer[idx] = (float)(AMPLITUDE * std::sin(2.0f * M_PI * frequency * time));
}
SDL_QueueAudio(dev, in_buffer, nSamples * sizeof(float));
SDL_PauseAudioDevice(dev, 0);
while(running){
while(SDL_PollEvent(&event)!=0);
}
delete[] in_buffer;
}
private:
SDL_AudioDeviceID dev;
AudioData ad;
int sampleNum = 0;
};
} // End of namespace AudioGen
int main(int argc, char *argv[])
{
AudioGen::Speaker speaker;
//speaker.pushBeep(440, 1000);
speaker.pushBeep2(440.0f, 1000);
return 0;
}
I was playing with C/'s fopen() and putc() functions and wanted to implement serialization/deserialization of a structure. However, here some "Segmentation fault" error occurs. I investigated similar questions and found, that it happens when I am trying to access some memory that doesn't belong to me.
What should I fix in my code to make it work properly?
struct Account {
string holder_name;
string holder_password;
double current_sum;
};
int save_to_file(Account * account);
Account * load_from_file(string holder_name);
int main(){
Account account = { "BEN", "password", 200.0 };
save_to_file(&account);
load_from_file("BEN");
return 0;
}
int save_to_file(Account * account){
const char * cstr = account->holder_name.c_str();
int size = sizeof(struct Account);
FILE * fp = fopen(cstr, "wb");
char * c;
c = (char *) account;
for(int i=0; i < size; i++)
{
putc(*c++, fp);
}
return fclose(fp);
}
Account * load_from_file(string holder_name)
{
FILE * fp;
char *c;
int i;
int size = sizeof(struct Account);
struct Account * ptr = (struct Account *) malloc(sizeof(struct Account));
if ((fp = fopen(holder_name.c_str(), "rb")) == NULL)
{
perror("Error occured while opening file");
return NULL;
}
c = (char *)ptr;
while ((i = getc(fp))!=EOF)
{
*c = i;
c++;
}
fclose(fp);
return ptr;
}
The string type doesn't belongs to C language, it's a object that comes from C++.
Since it may be implemented with pointers inside the object, you can't simply write their binary values to a file : the pointed memory won't be allocated when you'll read the file later.
A simple rule is that writing a pointer value to a file is a probably a mistake.
For your code to work, simply replace the string type with a char []. Here is the code with this only modification :
#include <stdlib.h>
#include <stdio.h>
struct Account {
char holder_name[100];
char holder_password[100];
double current_sum;
};
int save_to_file(struct Account * account);
struct Account * load_from_file(char *holder_name);
int main(){
struct Account account = { "BEN", "password", 200.0 };
save_to_file(&account);
load_from_file("BEN");
return 0;
}
int save_to_file(struct Account * account){
const char * cstr = account->holder_name;
int size = sizeof(struct Account);
FILE * fp = fopen(cstr, "wb");
char * c;
c = (char *) account;
for(int i=0; i < size; i++)
{
putc(*c++, fp);
}
return fclose(fp);
}
struct Account * load_from_file(char *holder_name)
{
FILE * fp;
char *c;
int i;
int size = sizeof(struct Account);
struct Account * ptr = (struct Account *) malloc(sizeof(struct Account));
if ((fp = fopen(holder_name, "rb")) == NULL)
{
perror("Error occured while opening file");
return NULL;
}
c = (char *)ptr;
while ((i = getc(fp))!=EOF)
{
*c = i;
c++;
}
fclose(fp);
return ptr;
}
Hy ,so my project to migrate to Visual Studio 2015. However I got stuck when I saw that receive 13 errors.
Error C2872 'data': ambiguous symbol Error C2872 'size': ambiguous symbol
Before you migrate to vss2015 everything work perfectly, I can not understand why we have moved the program after receiving this error.
Here is my Jpeg.cpp
#include "StdAfx.h"
#include "JpegFile.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <libjpeg-6b/jpeglib.h>
#include <libjpeg-6b/jpegLibLink.h>
#define OUTBUFFER_SIZE 0x8000
static FILE*fi;
static JOCTET * buffer;
static unsigned char*dest;
static int len;
static int destlen;
static unsigned char*data;
static int pos;
static int size;
static void file_init_destination(j_compress_ptr cinfo)
{
struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
buffer = (JOCTET*)malloc(OUTBUFFER_SIZE);
if(!buffer) {
perror("malloc");
printf("Out of memory!\n");
exit(1);
}
dmgr->next_output_byte = buffer;
dmgr->free_in_buffer = OUTBUFFER_SIZE;
}
static boolean file_empty_output_buffer(j_compress_ptr cinfo)
{
struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
if(fi)
fwrite(buffer, OUTBUFFER_SIZE, 1, fi);
dmgr->next_output_byte = buffer;
dmgr->free_in_buffer = OUTBUFFER_SIZE;
return 1;
}
static void file_term_destination(j_compress_ptr cinfo)
{ struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
if(fi)
fwrite(buffer, OUTBUFFER_SIZE-dmgr->free_in_buffer, 1, fi);
free(buffer);
buffer = 0;
dmgr->free_in_buffer = 0;
}
static void mem_init_destination(j_compress_ptr cinfo)
{
struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
dmgr->next_output_byte = dest;
dmgr->free_in_buffer = destlen;
}
static boolean mem_empty_output_buffer(j_compress_ptr cinfo)
{
printf("jpeg mem overflow!\n");
exit(1);
}
static void mem_term_destination(j_compress_ptr cinfo)
{
struct jpeg_destination_mgr*dmgr =
(struct jpeg_destination_mgr*)(cinfo->dest);
len = destlen - dmgr->free_in_buffer;
dmgr->free_in_buffer = 0;
}
int jpeg_save(unsigned char*data, int width, int height, int quality, const char*filename)
{
struct jpeg_destination_mgr mgr;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int t;
if(filename) {
fi = fopen(filename, "wb");
if(fi == NULL)
return 0;
} else
fi = NULL;
memset(&cinfo, 0, sizeof(cinfo));
memset(&jerr, 0, sizeof(jerr));
memset(&mgr, 0, sizeof(mgr));
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
mgr.init_destination = file_init_destination;
mgr.empty_output_buffer = file_empty_output_buffer;
mgr.term_destination = file_term_destination;
cinfo.dest = &mgr;
// init compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo,quality,TRUE);
//jpeg_write_tables(&cinfo);
//jpeg_suppress_tables(&cinfo, TRUE);
jpeg_start_compress(&cinfo, FALSE);
for(t=0;t<height;t++) {
unsigned char*data2 = &data[width*3*t];
jpeg_write_scanlines(&cinfo, &data2, 1);
}
jpeg_finish_compress(&cinfo);
if(fi)
fclose(fi);
jpeg_destroy_compress(&cinfo);
return 1;
}
int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*_fi)
{
struct jpeg_destination_mgr mgr;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int t;
fi = _fi;
memset(&cinfo, 0, sizeof(cinfo));
memset(&jerr, 0, sizeof(jerr));
memset(&mgr, 0, sizeof(mgr));
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
mgr.init_destination = file_init_destination;
mgr.empty_output_buffer = file_empty_output_buffer;
mgr.term_destination = file_term_destination;
cinfo.dest = &mgr;
// init compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
cinfo.dct_method = JDCT_IFAST;
jpeg_set_quality(&cinfo,quality,TRUE);
//jpeg_write_tables(&cinfo);
//jpeg_suppress_tables(&cinfo, TRUE);
jpeg_start_compress(&cinfo, FALSE);
for(t=0;t<height;t++) {
unsigned char*data2 = &data[width*3*t];
jpeg_write_scanlines(&cinfo, &data2, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return 1;
}
int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*_dest, int _destlen)
{
struct jpeg_destination_mgr mgr;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int t;
memset(&cinfo, 0, sizeof(cinfo));
memset(&jerr, 0, sizeof(jerr));
memset(&mgr, 0, sizeof(mgr));
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
dest = _dest;
len = 0;
destlen = _destlen;
mgr.init_destination = mem_init_destination;
mgr.empty_output_buffer = mem_empty_output_buffer;
mgr.term_destination = mem_term_destination;
cinfo.dest = &mgr;
// init compression
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
cinfo.dct_method = JDCT_IFAST;
jpeg_set_quality(&cinfo,quality,TRUE);
jpeg_start_compress(&cinfo, FALSE);
for(t=0;t<height;t++) {
unsigned char*data2 = &data[width*3*t];
jpeg_write_scanlines(&cinfo, &data2, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return len;
}
void mem_init_source (j_decompress_ptr cinfo)
{
struct jpeg_source_mgr* mgr = cinfo->src;
mgr->next_input_byte = data;
mgr->bytes_in_buffer = size;
//printf("init %d\n", size - mgr->bytes_in_buffer);
}
boolean mem_fill_input_buffer (j_decompress_ptr cinfo)
{
struct jpeg_source_mgr* mgr = cinfo->src;
printf("fill %d\n", size - mgr->bytes_in_buffer);
return 0;
}
void mem_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
struct jpeg_source_mgr* mgr = cinfo->src;
printf("skip %d +%d\n", size - mgr->bytes_in_buffer, num_bytes);
if(num_bytes<=0)
return;
mgr->next_input_byte += num_bytes;
mgr->bytes_in_buffer -= num_bytes;
}
boolean mem_resync_to_restart (j_decompress_ptr cinfo, int desired)
{
struct jpeg_source_mgr* mgr = cinfo->src;
printf("resync %d\n", size - mgr->bytes_in_buffer);
mgr->next_input_byte = data;
mgr->bytes_in_buffer = size;
return 1;
}
void mem_term_source (j_decompress_ptr cinfo)
{
struct jpeg_source_mgr* mgr = cinfo->src;
//printf("term %d\n", size - mgr->bytes_in_buffer);
}
int jpeg_load_from_mem(unsigned char*_data, int _size, unsigned char*dest, int width, int height)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
struct jpeg_source_mgr mgr;
int y;
//int x;
data = _data;
size = _size;
jpeg_create_decompress(&cinfo);
mgr.next_input_byte = data;
mgr.bytes_in_buffer = size;
mgr.init_source =mem_init_source ;
mgr.fill_input_buffer =mem_fill_input_buffer ;
mgr.skip_input_data =mem_skip_input_data ;
mgr.resync_to_restart =mem_resync_to_restart ;
mgr.term_source =mem_term_source ;
cinfo.err = jpeg_std_error(&jerr);
cinfo.src = &mgr;
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
for(y=0;y<height;y++) {
unsigned char*j = &dest[width*y*3];
jpeg_read_scanlines(&cinfo,&j,1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return 1;
}
typedef struct _RGBA {
unsigned char a,r,g,b;
} RGBA;
typedef unsigned char U8;
int jpeg_load(const char*filename, unsigned char**dest, int*_width, int*_height)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
//struct jpeg_source_mgr mgr;
FILE*fi = fopen(filename, "rb");
if(!fi) {
fprintf(stderr, "Couldn't open file %s\n", filename);
return 0;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, fi);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
U8*scanline = (U8 *)malloc(4 * cinfo.output_width);
int width = *_width = cinfo.output_width;
int height = *_height = cinfo.output_height;
*dest = (unsigned char*)malloc(width*height*4);
int y;
for (y=0;y<height;y++) {
int x;
U8 *js = scanline;
RGBA*line = &((RGBA*)(*dest))[y*width];
jpeg_read_scanlines(&cinfo, &js, 1);
if (cinfo.out_color_space == JCS_GRAYSCALE) {
for (x = 0; x < width; x++) {
line[x].a = 255;
line[x].r = line[x].g = line[x].b = js[x];
}
} else if (cinfo.out_color_space == JCS_RGB) {
for (x = width - 1; x >= 0; x--) {
line[x].a = 255;
line[x].r = js[x*3+0];
line[x].g = js[x*3+1];
line[x].b = js[x*3+2];
}
} else if (cinfo.out_color_space == JCS_YCCK) {
fprintf(stderr, "Error: Can't convert YCCK to RGB.\n");
return 0;
} else if (cinfo.out_color_space == JCS_YCbCr) {
for (x = 0; x < width; x++) {
int y = js[x * 3 + 0];
int u = js[x * 3 + 1];
int v = js[x * 3 + 1];
line[x].a = 255;
line[x].r = y + ((360 * (v - 128)) >> 8);
line[x].g = y - ((88 * (u - 128) + 183 * (v - 128)) >> 8);
line[x].b = y + ((455 * (u - 128)) >> 8);
}
} else if (cinfo.out_color_space == JCS_CMYK) {
for (x = 0; x < width; x++) {
int white = 255 - js[x * 4 + 3];
line[x].a = 255;
line[x].r = white - ((js[x * 4] * white) >> 8);
line[x].g = white - ((js[x * 4 + 1] * white) >> 8);
line[x].b = white - ((js[x * 4 + 2] * white) >> 8);
}
}
}
free(scanline);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(fi);
return 1;
}
And here is Jpeg.h
#include <stdio.h>
int jpeg_save(unsigned char*data, int width, int height, int quality, const char*filename);
int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*fi);
int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*dest, int destsize);
int jpeg_load(const char*filename, unsigned char**dest, int*width, int*height);
int jpeg_load_from_mem(unsigned char*_data, int size, unsigned char*dest, int width, int height);
I get the error for "data" and also for "size" symbol.
Best regards.
Why in visual studio 2013 i dont have that this error? And how to solve..
I get ambiguous symbol 10 times, in 10 sintax in jpeg.cpp
i am having problems understanding how the audio part of the sdl library works
now, i know that when you initialize it, you have to specify the frequency and a >>callback<< function, which i think is then called automatically at the given frequency.
can anyone who worked with the sdl library write a simple example that would use sdl_audio to generate a 440 hz square wave (since it is the simplest waveform) at a sampling frequency of 44000 hz?
The Introduction to SDL (2011 cached version: 2) has got a neat example of using SDL Sound library that should get you started: http://www.libsdl.org/intro.en/usingsound.html
EDIT: Here is a working program that does what you asked for. I modified a bit the code found here: http://www.dgames.org/beep-sound-with-sdl/
#include <SDL/SDL.h>
#include <SDL/SDL_audio.h>
#include <queue>
#include <cmath>
const int AMPLITUDE = 28000;
const int FREQUENCY = 44100;
struct BeepObject
{
double freq;
int samplesLeft;
};
class Beeper
{
private:
double v;
std::queue<BeepObject> beeps;
public:
Beeper();
~Beeper();
void beep(double freq, int duration);
void generateSamples(Sint16 *stream, int length);
void wait();
};
void audio_callback(void*, Uint8*, int);
Beeper::Beeper()
{
SDL_AudioSpec desiredSpec;
desiredSpec.freq = FREQUENCY;
desiredSpec.format = AUDIO_S16SYS;
desiredSpec.channels = 1;
desiredSpec.samples = 2048;
desiredSpec.callback = audio_callback;
desiredSpec.userdata = this;
SDL_AudioSpec obtainedSpec;
// you might want to look for errors here
SDL_OpenAudio(&desiredSpec, &obtainedSpec);
// start play audio
SDL_PauseAudio(0);
}
Beeper::~Beeper()
{
SDL_CloseAudio();
}
void Beeper::generateSamples(Sint16 *stream, int length)
{
int i = 0;
while (i < length) {
if (beeps.empty()) {
while (i < length) {
stream[i] = 0;
i++;
}
return;
}
BeepObject& bo = beeps.front();
int samplesToDo = std::min(i + bo.samplesLeft, length);
bo.samplesLeft -= samplesToDo - i;
while (i < samplesToDo) {
stream[i] = AMPLITUDE * std::sin(v * 2 * M_PI / FREQUENCY);
i++;
v += bo.freq;
}
if (bo.samplesLeft == 0) {
beeps.pop();
}
}
}
void Beeper::beep(double freq, int duration)
{
BeepObject bo;
bo.freq = freq;
bo.samplesLeft = duration * FREQUENCY / 1000;
SDL_LockAudio();
beeps.push(bo);
SDL_UnlockAudio();
}
void Beeper::wait()
{
int size;
do {
SDL_Delay(20);
SDL_LockAudio();
size = beeps.size();
SDL_UnlockAudio();
} while (size > 0);
}
void audio_callback(void *_beeper, Uint8 *_stream, int _length)
{
Sint16 *stream = (Sint16*) _stream;
int length = _length / 2;
Beeper* beeper = (Beeper*) _beeper;
beeper->generateSamples(stream, length);
}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_AUDIO);
int duration = 1000;
double Hz = 440;
Beeper b;
b.beep(Hz, duration);
b.wait();
return 0;
}
Good luck.
A boiled-down variant of the beeper-example, reduced to the bare minimum (with error-handling).
#include <math.h>
#include <SDL.h>
#include <SDL_audio.h>
const int AMPLITUDE = 28000;
const int SAMPLE_RATE = 44100;
void audio_callback(void *user_data, Uint8 *raw_buffer, int bytes)
{
Sint16 *buffer = (Sint16*)raw_buffer;
int length = bytes / 2; // 2 bytes per sample for AUDIO_S16SYS
int &sample_nr(*(int*)user_data);
for(int i = 0; i < length; i++, sample_nr++)
{
double time = (double)sample_nr / (double)SAMPLE_RATE;
buffer[i] = (Sint16)(AMPLITUDE * sin(2.0f * M_PI * 441.0f * time)); // render 441 HZ sine wave
}
}
int main(int argc, char *argv[])
{
if(SDL_Init(SDL_INIT_AUDIO) != 0) SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
int sample_nr = 0;
SDL_AudioSpec want;
want.freq = SAMPLE_RATE; // number of samples per second
want.format = AUDIO_S16SYS; // sample type (here: signed short i.e. 16 bit)
want.channels = 1; // only one channel
want.samples = 2048; // buffer-size
want.callback = audio_callback; // function SDL calls periodically to refill the buffer
want.userdata = &sample_nr; // counter, keeping track of current sample number
SDL_AudioSpec have;
if(SDL_OpenAudio(&want, &have) != 0) SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "Failed to open audio: %s", SDL_GetError());
if(want.format != have.format) SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "Failed to get the desired AudioSpec");
SDL_PauseAudio(0); // start playing sound
SDL_Delay(1000); // wait while sound is playing
SDL_PauseAudio(1); // stop playing sound
SDL_CloseAudio();
return 0;
}
SDL 2 C example
The following code produces a sinusoidal sound, it is adapted from: https://codereview.stackexchange.com/questions/41086/play-some-sine-waves-with-sdl2
main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL2/SDL.h>
const double ChromaticRatio = 1.059463094359295264562;
const double Tao = 6.283185307179586476925;
Uint32 sampleRate = 48000;
Uint32 frameRate = 60;
Uint32 floatStreamLength = 1024;
Uint32 samplesPerFrame;
Uint32 msPerFrame;
double practicallySilent = 0.001;
Uint32 audioBufferLength = 48000;
float *audioBuffer;
SDL_atomic_t audioCallbackLeftOff;
Sint32 audioMainLeftOff;
Uint8 audioMainAccumulator;
SDL_AudioDeviceID AudioDevice;
SDL_AudioSpec audioSpec;
SDL_Event event;
SDL_bool running = SDL_TRUE;
typedef struct {
float *waveform;
Uint32 waveformLength;
double volume;
double pan;
double frequency;
double phase;
} voice;
void speak(voice *v) {
float sample;
Uint32 sourceIndex;
double phaseIncrement = v->frequency/sampleRate;
Uint32 i;
if (v->volume > practicallySilent) {
for (i = 0; (i + 1) < samplesPerFrame; i += 2) {
v->phase += phaseIncrement;
if (v->phase > 1)
v->phase -= 1;
sourceIndex = v->phase*v->waveformLength;
sample = v->waveform[sourceIndex]*v->volume;
audioBuffer[audioMainLeftOff+i] += sample*(1-v->pan);
audioBuffer[audioMainLeftOff+i+1] += sample*v->pan;
}
}
else {
for (i=0; i<samplesPerFrame; i+=1)
audioBuffer[audioMainLeftOff+i] = 0;
}
audioMainAccumulator++;
}
double getFrequency(double pitch) {
return pow(ChromaticRatio, pitch-57)*440;
}
int getWaveformLength(double pitch) {
return sampleRate / getFrequency(pitch)+0.5f;
}
void buildSineWave(float *data, Uint32 length) {
Uint32 i;
for (i=0; i < length; i++)
data[i] = sin(i*(Tao/length));
}
void logSpec(SDL_AudioSpec *as) {
printf(
" freq______%5d\n"
" format____%5d\n"
" channels__%5d\n"
" silence___%5d\n"
" samples___%5d\n"
" size______%5d\n\n",
(int) as->freq,
(int) as->format,
(int) as->channels,
(int) as->silence,
(int) as->samples,
(int) as->size
);
}
void logVoice(voice *v) {
printf(
" waveformLength__%d\n"
" volume__________%f\n"
" pan_____________%f\n"
" frequency_______%f\n"
" phase___________%f\n",
v->waveformLength,
v->volume,
v->pan,
v->frequency,
v->phase
);
}
void logWavedata(float *floatStream, Uint32 floatStreamLength, Uint32 increment) {
printf("\n\nwaveform data:\n\n");
Uint32 i=0;
for (i = 0; i < floatStreamLength; i += increment)
printf("%4d:%2.16f\n", i, floatStream[i]);
printf("\n\n");
}
void audioCallback(void *unused, Uint8 *byteStream, int byteStreamLength) {
float* floatStream = (float*) byteStream;
Sint32 localAudioCallbackLeftOff = SDL_AtomicGet(&audioCallbackLeftOff);
Uint32 i;
for (i = 0; i < floatStreamLength; i++) {
floatStream[i] = audioBuffer[localAudioCallbackLeftOff];
localAudioCallbackLeftOff++;
if (localAudioCallbackLeftOff == audioBufferLength)
localAudioCallbackLeftOff = 0;
}
SDL_AtomicSet(&audioCallbackLeftOff, localAudioCallbackLeftOff);
}
int init(void) {
SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER);
SDL_AudioSpec want;
SDL_zero(want);
want.freq = sampleRate;
want.format = AUDIO_F32;
want.channels = 2;
want.samples = floatStreamLength;
want.callback = audioCallback;
AudioDevice = SDL_OpenAudioDevice(NULL, 0, &want, &audioSpec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
if (AudioDevice == 0) {
printf("\nFailed to open audio: %s\n", SDL_GetError());
return 1;
}
printf("want:\n");
logSpec(&want);
printf("audioSpec:\n");
logSpec(&audioSpec);
if (audioSpec.format != want.format) {
printf("\nCouldn't get Float32 audio format.\n");
return 2;
}
sampleRate = audioSpec.freq;
floatStreamLength = audioSpec.size / 4;
samplesPerFrame = sampleRate / frameRate;
msPerFrame = 1000 / frameRate;
audioMainLeftOff = samplesPerFrame * 8;
SDL_AtomicSet(&audioCallbackLeftOff, 0);
if (audioBufferLength % samplesPerFrame)
audioBufferLength += samplesPerFrame - (audioBufferLength % samplesPerFrame);
audioBuffer = malloc(sizeof(float) * audioBufferLength);
return 0;
}
int onExit(void) {
SDL_CloseAudioDevice(AudioDevice);
SDL_Quit();
return 0;
}
int main(int argc, char *argv[]) {
float syncCompensationFactor = 0.0016;
Sint32 mainAudioLead;
Uint32 i;
voice testVoiceA;
voice testVoiceB;
voice testVoiceC;
testVoiceA.volume = 1;
testVoiceB.volume = 1;
testVoiceC.volume = 1;
testVoiceA.pan = 0.5;
testVoiceB.pan = 0;
testVoiceC.pan = 1;
testVoiceA.phase = 0;
testVoiceB.phase = 0;
testVoiceC.phase = 0;
testVoiceA.frequency = getFrequency(45);
testVoiceB.frequency = getFrequency(49);
testVoiceC.frequency = getFrequency(52);
Uint16 C0waveformLength = getWaveformLength(0);
testVoiceA.waveformLength = C0waveformLength;
testVoiceB.waveformLength = C0waveformLength;
testVoiceC.waveformLength = C0waveformLength;
float sineWave[C0waveformLength];
buildSineWave(sineWave, C0waveformLength);
testVoiceA.waveform = sineWave;
testVoiceB.waveform = sineWave;
testVoiceC.waveform = sineWave;
if (init())
return 1;
SDL_Delay(42);
SDL_PauseAudioDevice(AudioDevice, 0);
while (running) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
running = SDL_FALSE;
}
}
for (i = 0; i < samplesPerFrame; i++)
audioBuffer[audioMainLeftOff+i] = 0;
speak(&testVoiceA);
speak(&testVoiceB);
speak(&testVoiceC);
if (audioMainAccumulator > 1) {
for (i=0; i<samplesPerFrame; i++) {
audioBuffer[audioMainLeftOff+i] /= audioMainAccumulator;
}
}
audioMainAccumulator = 0;
audioMainLeftOff += samplesPerFrame;
if (audioMainLeftOff == audioBufferLength)
audioMainLeftOff = 0;
mainAudioLead = audioMainLeftOff - SDL_AtomicGet(&audioCallbackLeftOff);
if (mainAudioLead < 0)
mainAudioLead += audioBufferLength;
if (mainAudioLead < floatStreamLength)
printf("An audio collision may have occured!\n");
SDL_Delay(mainAudioLead * syncCompensationFactor);
}
onExit();
return 0;
}
Compile and run:
gcc -ggdb3 -O3 -std=c99 -Wall -Wextra -pedantic -o main.out main.c -lSDL2 -lm
./main.out
Should be easy to turn this into a simple piano with: https://github.com/cirosantilli/cpp-cheat/blob/f734a2e76fbcfc67f707ae06be7a2a2ef5db47d1/c/interactive/audio_gen.c#L44
For wav manipulation, also check the official examples:
http://hg.libsdl.org/SDL/file/e12c38730512/test/testresample.c
http://hg.libsdl.org/SDL/file/e12c38730512/test/loopwave.c
Tested on Ubuntu 19.10, SDL 2.0.10.
This is a minimal example of how to play a sine wave in SDL2.
Make sure to call SDL_Init(SDL_INIT_AUDIO) before creating an instance of Sound.
Sound.h
#include <cstdint>
#include <SDL2/SDL.h>
class Sound
{
public:
Sound();
~Sound();
void play();
void stop();
const double m_sineFreq;
const double m_sampleFreq;
const double m_samplesPerSine;
uint32_t m_samplePos;
private:
static void SDLAudioCallback(void *data, Uint8 *buffer, int length);
SDL_AudioDeviceID m_device;
};
Sound.cpp
#include "Sound.h"
#include <cmath>
#include <iostream>
Sound::Sound()
: m_sineFreq(1000),
m_sampleFreq(44100),
m_samplesPerSine(m_sampleFreq / m_sineFreq),
m_samplePos(0)
{
SDL_AudioSpec wantSpec, haveSpec;
SDL_zero(wantSpec);
wantSpec.freq = m_sampleFreq;
wantSpec.format = AUDIO_U8;
wantSpec.channels = 1;
wantSpec.samples = 2048;
wantSpec.callback = SDLAudioCallback;
wantSpec.userdata = this;
m_device = SDL_OpenAudioDevice(NULL, 0, &wantSpec, &haveSpec, SDL_AUDIO_ALLOW_FORMAT_CHANGE);
if (m_device == 0)
{
std::cout << "Failed to open audio: " << SDL_GetError() << std::endl;
}
}
Sound::~Sound()
{
SDL_CloseAudioDevice(m_device);
}
void Sound::play()
{
SDL_PauseAudioDevice(m_device, 0);
}
void Sound::stop()
{
SDL_PauseAudioDevice(m_device, 1);
}
void Sound::SDLAudioCallback(void *data, Uint8 *buffer, int length)
{
Sound *sound = reinterpret_cast<Sound*>(data);
for(int i = 0; i < length; ++i)
{
buffer[i] = (std::sin(sound->m_samplePos / sound->m_samplesPerSine * M_PI * 2) + 1) * 127.5;
++sound->m_samplePos;
}
}