int is_in_list(struct list* lst, unsigned long content) { struct list_item *temp_item, *item; int res = 0; item = new_list_item(content); if (!item) return 0; if(lst->head==NULL) { free(item); return 0; } temp_item=lst->head; while(compare(temp_item, item) < 0) temp_item = temp_item->next; if (compare(temp_item, item)==0) { res = 1; } else { res = 0; } free(item); return res; };
int in_list(struct list* lst, unsigned long content) { struct list_item *temp_item, *item; item = new_list_item(content); if(lst->head==NULL) return 0; temp_item=lst->head; while(compare(temp_item, item) < 0) temp_item = temp_item->next; if (compare(temp_item, item)==0) return 1; return 0; };
int insert(struct list* lst, unsigned long content) { struct list_item *temp_item, *item; int cmp; item = new_list_item(content); if(!item) return ERROR; cmp = compare(lst->head, item); if(lst->head==NULL) { lst->head=item; return 1; } else if (cmp==1) { item->next=lst->head; lst->head = item; item->prev = NULL; return 1; } else if (cmp==0) { free(item); return 0; } else if (cmp==-1) { temp_item = lst->head; while(compare(temp_item->next, item)==-1) { temp_item = temp_item->next; } /* temp_item points to last list element less then item */ /* we shall insert item after temp_item */ if(compare(temp_item->next, item)==0) { free(item); return 0; } else if(compare(temp_item->next, item)==ERROR) { free(item); return ERROR; } else if(compare(temp_item->next, item)==1) { item->next=temp_item->next; item->prev=temp_item; if(temp_item->next) temp_item->next->prev = item; temp_item->next = item; return 1; }; } else if (compare(lst->head, item)==ERROR) { free(item); return ERROR; }; if(item) free(item); return ERROR; };
PyObject* play_os(Py_buffer buffer_obj, int len_samples, int num_channels, int bytes_per_chan, int sample_rate, play_item_t* play_list_head, int latency_us) { char err_msg_buf[SA_ERR_STR_LEN]; audio_blob_t* audio_blob; int bytes_per_frame = bytes_per_chan * num_channels; static char *device = "default"; snd_pcm_format_t sample_format; pthread_t play_thread; int result; snd_pcm_hw_params_t* hw_params; snd_pcm_uframes_t buffer_frames; /* not sure where the best place to do this is or if it matters */ snd_pcm_hw_params_alloca(&hw_params); DBG_PLAY_OS_CALL /* set that format appropriately */ if (bytes_per_chan == 1) { } else if (bytes_per_chan == 2) { sample_format = SND_PCM_FORMAT_S16_LE; } else { ALSA_EXCEPTION("Unsupported Sample Format.", 0, "", err_msg_buf); return NULL; } /* audio blob initial allocation and audio buffer copy */ audio_blob = create_audio_blob(); audio_blob->buffer_obj = buffer_obj; audio_blob->list_mutex = play_list_head->mutex; audio_blob->len_bytes = len_samples * bytes_per_frame; audio_blob->frame_size = bytes_per_frame; /* setup the linked list item for this playback buffer */ grab_mutex(play_list_head->mutex); audio_blob->play_list_item = new_list_item(play_list_head); release_mutex(play_list_head->mutex); /* open access to a PCM device (blocking mode) */ result = snd_pcm_open((snd_pcm_t**)&audio_blob->handle, device, SND_PCM_STREAM_PLAYBACK, 0); if (result < 0) { ALSA_EXCEPTION("Error opening PCM device.", result, snd_strerror(result), err_msg_buf); destroy_audio_blob(audio_blob); return NULL; } /* set the PCM params using ALSA's convenience function */ result = snd_pcm_set_params(audio_blob->handle, sample_format, SND_PCM_ACCESS_RW_INTERLEAVED, num_channels, sample_rate, RESAMPLE, latency_us); if (result < 0) { ALSA_EXCEPTION("Error setting parameters.", result, snd_strerror(result), err_msg_buf); snd_pcm_close(audio_blob->handle); destroy_audio_blob(audio_blob); return NULL; } /* get the HW params (needed for buffer size) */ result = snd_pcm_hw_params_current(audio_blob->handle, hw_params); if (result < 0) { ALSA_EXCEPTION("Error getting hardware parameters.", result, snd_strerror(result), err_msg_buf); snd_pcm_close(audio_blob->handle); destroy_audio_blob(audio_blob); return NULL; } /* get the buffer size */ result = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_frames); if (result < 0) { ALSA_EXCEPTION("Error getting buffer_size.", result, snd_strerror(result), err_msg_buf); snd_pcm_close(audio_blob->handle); destroy_audio_blob(audio_blob); return NULL; } audio_blob->buffer_size = buffer_frames * bytes_per_chan * num_channels; dbg1("ALSA says buffer size is %d bytes\n", audio_blob->buffer_size); /* fire off the playback thread */ result = pthread_create(&play_thread, NULL, playback_thread, (void*)audio_blob); if (result != 0) { ALSA_EXCEPTION("Could not create playback thread.", result, "", err_msg_buf); snd_pcm_close(audio_blob->handle); destroy_audio_blob(audio_blob); return NULL; } return PyLong_FromUnsignedLongLong(audio_blob->play_list_item->play_id); }