How can I set a sleep between loop using sdl-mixer? - c++

I'm trying to play music using SDL2_Mixer library in Ubuntu 18.04.
I succeed to play .wav file loop forever as below code.
#define ERROR_BEEP "../audio/beep.wav"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
void main()
{
SDL_AudioSpec wavSpec;
SDL_Init(SDL_INIT_AUDIO)
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) >= 0)
SDL_LoadWAV(ERROR_BEEP, &wavSpec, &wavBuffer, &wavLength);
Mix_Music *now_playing_= Mix_LoadMUS(ERROR_BEEP);
Mix_PlayMusic(now_playing_, -1);
SDL_Delay(10000);
Mix_HaltMusic();
Mix_FreeMusic(now_playing_);
now_playing_=NULL;
}
My beep.wav plays well repeatedly. However, the beep file is played immediately and the alarm sound is too loud. (ex: beep!beep!beep!)
Is there any way to put sleep between beep repeating loop? (ex: beep!...beep!...beep!...)

Related

How to include multiple directories in arduino library

I have my arduino libraries folder which holds one library called DHT_sensor_Library. In this folder, I have another folder called DHT_U. In this folder, I have DHT_U.ccp and DHT_U.h.
The problem is that when I include DHT_U.h in my arduino IDE:
#include "DHT_U.h"
The error says:
Tempreture_Humidity_Sensor:2:19: error: DHT_U.h: No such file or directory
compilation terminated.
exit status 1
DHT_U.h: No such file or directory
I have already tried
#include "DHT_U/DHT_U.h" ,
#include "DHT_U\DHT_U.h"
and
#include ..\DHT_U.h". None of these worked.
This is a snippet of my code:
#include "DHT.h"
#include "DHT_U.h"
#include "LiquidCrystal.h"
#include "DHT.h"
Full code can be shown here:
#include <DHT.h>
#include <DHT_U.h>
// include the library code:
#include <LiquidCrystal.h>
#include "DHT.h"
// set the DHT Pin
#define DHTPIN 8
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
dht.begin();
// Print a message to the LCD.
lcd.print("Temp: Humidity:");
}
void loop() {
delay(500);
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// read humidity
float h = dht.readHumidity();
//read temperature in Fahrenheit
float f = dht.readTemperature(true);
if (isnan(h) || isnan(f)) {
lcd.print("ERROR");
return;
}
lcd.print(f);
lcd.setCursor(7,1);
lcd.print(h);
}
How should I fix this?
Try including the hole path like "/home/your_username/arduino/lib/foo.h" or something like this. Are you sure it is a .h file and not a .hpp ?
One thing to consider is that you need to be careful when your #include methods.
If DHT_U.h is located in the same direction as your .ino file you can include it with this:
#include "DHT_U.h"
However, if you installed the library using the library manager from Arduino IDE, you should do:
#include <DHT_U.h>
If none of these works, make sure that you have installed correctly your library. You could try by testing the examples from the Arduino IDE with the library that you have installed.

How to play sound with 7.1 audio system sfml?

I am trying to record some sound with sfml and then play it back. I have previously done this successfully with my old headphones that i believe had a 5.1 sound system. But now when i try to do the same thing with my new headphones (7.1 sound). The code throws this error.
AL lib: (EE) SetChannelMap: Failed to match front-center channel (2)
in channel map.
I have tried restarting visual studio. Restarting my computer. Resetting the cache in visual studio.
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <thread>
#include <chrono>
int main()
{
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window;
window.create(sf::VideoMode(800, 500), "Audio check", sf::Style::Close | sf::Style::Resize);
if (!sf::SoundBufferRecorder::isAvailable())
{
// error: audio capture is not available on this system
std::cout << "Something went wrong" << std::endl;
}
// create the recorder
sf::SoundBufferRecorder recorder;
recorder.start(44100);
//record the audio for 5 sec
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
recorder.stop();
//get the buffer from the recorder and play it back
const sf::SoundBuffer& buffer = recorder.getBuffer();
sf::Sound sound(buffer);
sound.play();
sf::Event event;
while (window.isOpen()) {
while (window.pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
}
}
window.clear(sf::Color::Blue);
window.display();
}
return EXIT_SUCCESS;
}
Actually SFML doesn't support more than 2 channels recording. If we check documentation of the method setChannelCount() of the class SoundRecorder it only supports up to 2 (channels).
Edit
From openAL library, which sfml is based on (emphasis mine):
hexagon.ambdec
Specifies a flat-front hexagonal speaker setup for 7.1
Surround output. The front left and right speakers are placed at +30
and -30 degrees, the side speakers are placed at +90 and -90 degrees,
and the back speakers are placed at
+150 and -150 degrees. Although this is for 7.1 output, no front-center speaker is defined for the decoder, meaning that speaker
will be silent for 3D sound (however it may still be used with
AL_SOFT_direct_channels or ALC_EXT_DEDICATED output). A "proper" 7.1
decoder may be provided in the future, but due to the nature of the
speaker configuration will have trade-offs.
It seems the library doesn't count with a actual 7.1 decoder.

beaglebone black flash onboard USR0 led c++, code runs but led doesn't change

I have a BeagleBone Black with Debian installed. I successfully ssh'd into it with MobaXterm and wrote the following code (have written/compiled/run this in both nano and gedit):
#include <iostream>
#include <stdio.h>
#include <unistd.h>
using namespace std;
int main(){
cout << "LED Flash Start" << endl;
FILE *LEDHandle = NULL;
const char *LEDBrightness="/sys/class/leds/beaglebone:green:usr0/brightness";
for(int i=0; i<10; i++){
if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
fwrite("1", sizeof(char), 1, LEDHandle);
fclose(LEDHandle);
}
usleep(1000000);
if((LEDHandle = fopen(LEDBrightness, "r+")) != NULL){
fwrite("0", sizeof(char), 1, LEDHandle);
fclose(LEDHandle);
}
usleep(1000000);
}
cout << "LED Flash End" << endl;
}
basically I followed the steps shown here: http://elinux.org/Beagleboard:C/C%2B%2B_Programming
The code compiles and runs. In the terminal it displays the expected output however on the BeagleBone Black the USR0 led never changes from its usual heartbeat pattern. Does anyone know why this could be?
Thanks in advance.
First check if it all works from command line
# cd beaglebone:green:usr0
# more trigger
none ...... mmc0 mmc1 timer oneshot [heartbeat] backlight gpio cpu0 default-on transient
# echo none > trigger
you will see that the led stops blinking. Now try this
# echo 1 > brightness
The first LED should go on
# echo 0 > brightness
The first LED should go off.
# echo heartbeat > trigger
Setting it Back to heartbeat.
If all of this works then may be your are doing something wrong. Do a proper error checking then it would be easier for you to debug it.

how to view stream sent by me libvlc C/C++

im on Ubuntu 14.04 and I'm trying to write a program that will stream my desktop, using the answer to this: libvlc stream part of screen as an example. However, I don't have another computer readily aviable to see that the stream is going along well, so how can I view that stream on my computer?
libvlc_vlm_add_broadcast(inst, "mybroad", "screen://",
"#transcode{vcodec=h264,vb=800,scale=1,acodec=mpga,ab=128,channels=2,samplerate=44100}:http{mux=ts,dst=:8080/stream}",
5, params, 1, 0)
My program throws no errors, and writes this
[0x7f0118000e18] x264 encoder: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[0x7f0118000e18] x264 encoder: profile High, level 3.0
[0x7f0118000e18] x264 encoder: final ratefactor: 25.54
[0x7f0118000e18] x264 encoder: using SAR=1/1
[0x7f0118000e18] x264 encoder: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[0x7f0118000e18] x264 encoder: profile High, level 2.2
So to me, everything seems ok. However, I don't know how to view that stream from my computer- if I open vlc and try to open a network stream, using http:// #:7777 (space on purpose, website does not allow to post such links) I get the invalid host error in its log. This probably is a silly mistake or error on my part, but any help would be greatly appreciated!
if anyone needs it, this is my entire code (I'm using QT 4.8.6):
#include <QCoreApplication>
#include <iostream>
#include <vlc/vlc.h>
#include <X11/Xlib.h>
// #include <QDebug>
using namespace std;
bool ended;
void playerEnded(const libvlc_event_t* event, void *ptr);
libvlc_media_list_t * subitems;
libvlc_instance_t * inst;
libvlc_media_player_t *mp;
libvlc_media_t *media;
libvlc_media_t * stream;
int main(int argc, char *argv[])
{
XInitThreads();
QCoreApplication::setAttribute(Qt::AA_X11InitThreads);
ended = false;
QCoreApplication a(argc, argv);
// the array with parameters
const char* params[] = {"screen-top=0",
"screen-left=0",
"screen-width=640",
"screen-height=480",
"screen-fps=10"};
// Load the VLC engine */
inst = libvlc_new (0, NULL);
if(!inst)
std::cout << "Can't load video player plugins" << std::endl;
cout<< "add broacast: " <<
libvlc_vlm_add_broadcast(inst, "mybroad",
"screen://",
"#transcode{vcodec=h264,vb=800,scale=1,acodec=mpga,ab=128,channels=2,samplerate=44100}:http{mux=ts,dst=:8080/stream}",
5, params, // <= 5 == sizeof(params) == count of parameters
1, 0)<< '\n';
cout<< "poczatek broacastu: " <<libvlc_vlm_play_media(inst, "mybroad")<< '\n';
media = libvlc_media_new_location(inst,http://#:8080/stream");
// Create a media player playing environment
mp = libvlc_media_player_new (inst);
libvlc_media_player_play (mp);
cout<<"szatan!!!"<<endl;
int e;
cin>>e;
/* Stop playing */
libvlc_media_player_stop (mp);
/* Free the media_player */
libvlc_media_player_release (mp);
libvlc_release (inst);
return a.exec();
}
so, i have found the answer- stack overflow wont let me post an answer because I'm new here, so its in the comments! I should have used my IP address when creating media: media = libvlc_media_new_location(inst, "http: //192.168.1.56:8080");(space on purpose so that forum does not hide link) works great! –

nanosecond Measurement in arduino using clock_gettime

I making a Localization project Using Arduino and Xbee Zg where i need to measure time in nano second resolution im using arduino due board with 84 Mhz clock and arduino 1.5.2 IDE
im trying to use clock_gettime function i already included time.h but i get the same
compiling error
clock_gettime
is not declared in this scope
this is just a part of my Distance_Measurement.c file
#include "Distance_Measurement.h"
#include "time.h"
struct timespec start, stop;
bool Start_Time()
{
if(clock_gettime(CLOCK_REALTIME,&start) == -1)
return false;
else
return true;
}
bool Stop_Time()
{
if(clock_gettime(CLOCK_REALTIME,&stop) == -1)
return false;
else
return true;
}
double Cal_Time_Nano()
{
return (stop_time.tv_nsec - start_time.tv_nsec);
}
please help me
i first used #include i got the same error i have found that visual studio have included anther time.h not time.h in arduino gcc so i copied the last one and pasted it to arduino libraries path with my distance measurement library – PrinceOfEgy